7

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.

John
  • 2,963
  • 11
  • 33
HFinch
  • 534
  • 1
  • 7
  • 20
  • 1
    To be clear, do you want to only *build* (i.e. compile) one executable? Or do you want to only *configure/generate* (at CMake-time) one executable? – Kevin Aug 08 '19 at 12:38
  • @kamil i read it here on stackoverflow but i lost the link. I tried `cmake --build .. --target target_name` but I got the error message `CMake Error: The source directory "/build/target_name" does not exist.` – HFinch Aug 08 '19 at 13:25
  • @squareskittles I want to compile the executable but only the one i name and its dependent libraries. – HFinch Aug 08 '19 at 13:26
  • 1
    Your command line looks correct syntactically, but is your build folder actually named `build`? And did CMake generate for the target with name `target_name`? – Kevin Aug 08 '19 at 13:30
  • How did you configure cmake ? The `--build` should point to the folder where you configured cmake. – KamilCuk Aug 08 '19 at 13:42
  • @squareskittles yes the build folder is called `build`. Yes cmake was generating for my whole project. What I was thinking is that `target` might only refer to the project name and not a specific target created with `add_executable`. – HFinch Aug 09 '19 at 07:12

1 Answers1

13

There are a couple potential issues here. First, the typical CMake workflow places the build folder as a sibling to the top-level CMake file. So your file hierarchy should look something like this:

.
|-- CMakeLists.txt
|-- library1
|   |-- CMakeLists.txt
|-- library2
|   |-- CMakeLists.txt
|-- executables
|   |-- CMakeLists.txt
|-- build    <------------ Run CMake commands from here.

This isolates all the CMake-generated files to the build folder. Secondly, you must to careful to run the CMake build stages in the proper location. We can run everything from within the build folder, for example:

  1. To generate the build system, run cmake .. from the build directory. This first step should point to the top-level CMake file.

  2. To build (or compile) a specific target, say it's called MyExecutable1, run this:

    cmake --build . --target MyExecutable1
    

    from the build directory. You must be sure to point the --build flag at the build folder, not the top-level CMake file this time. Also, the target name to specify in this command should match the target name used in add_executable(), not the project name or anything else.

  3. As always, when getting errors/issues while attempting to run CMake, it helps to clear the cache (delete build/CMakeCache.txt), or just delete the build folder altogether and start fresh.

Kevin
  • 16,549
  • 8
  • 60
  • 74
  • 2
    `cmake --build . --target MyExecutable1` seems not work. All targets still be configured. – John Oct 26 '21 at 11:44