When working with c or c++ in the command line I usually make scripts to help me with common tasks. Usually I will make a few scripts to activate different kinds of builds quickly
These scripts should be project independent so that we can re-use them in any given c/c++ project.
Note: My toolchain is cmake for builds, clang for formatting, and bash for scripting
The first step of this process is to create a scripts dir in the root of the project
We'll start with a script which moves us to the correct directory: scripts/move_to_base_project_dir.sh
#!/bin/bash
cd [your project directory]
Notes:
- we're making this a script because if we ever change our project dir we can still use these scripts from anywhere
- [your project directory] must be an absolute path, this is because we will run this script multiple times, if it was a relative path say "..", then we would continually be moving back a directory.
- Add an argument to each script which mentions if it's already in the project dir, if it is don't cd again, this will allow support for relative paths
With cmake we start by generating the builds system: scripts/generate_build_system.sh
#!/bin/bash
./move_to_base_project_dir.sh
cmake -S . -B build
Next we can build the project: scripts/build.sh
#!/bin/bash
./move_to_base_project_dir.sh
cmake --build build
If we want to generate the build system and build all at once: scripts/build_process.sh
#!/bin/bash
./move_to_project_dir.sh
./generate_build_system.sh && ./build.sh
Now that you've built your project run it: scripts/run.sh
#!/bin/bash
./move_to_project_dir.sh
./build/[executable name]
You've made some edits, and you want to build and run the program: scripts/build_and_run.sh
#!/bin/bash
./move_to_project_dir.sh
./build_process.sh && ./run.sh
Notice that the only parameters to these scripts were the executable name and the project directory, to complete our goal, we should store these in a file and load them in dynamically