I have a CMake Project with roughly this structure:
.
|-- library1
| |-- CMakeLists.txt
|-- library2
| |-- CMakeLists.txt
|-- executables
| |-- CMakeLists.txt
I generate two executables in the folder named executables. I wonder if it was possible just generating one executable and its dependencies instead of all. I heard something about the cmake --target option but I cant get it to work with cmake/3.13.4.
There are a couple potential issues here. First, the typical CMake workflow places the
buildfolder as a sibling to the top-level CMake file. So your file hierarchy should look something like this:This isolates all the CMake-generated files to the
buildfolder. Secondly, you must to careful to run the CMake build stages in the proper location. We can run everything from within thebuildfolder, for example:To generate the build system, run
cmake ..from thebuilddirectory. This first step should point to the top-level CMake file.To build (or compile) a specific target, say it's called
MyExecutable1, run this:from the
builddirectory. You must be sure to point the--buildflag at thebuildfolder, not the top-level CMake file this time. Also, the target name to specify in this command should match the target name used inadd_executable(), not the project name or anything else.As always, when getting errors/issues while attempting to run CMake, it helps to clear the cache (delete
build/CMakeCache.txt), or just delete thebuildfolder altogether and start fresh.