Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
632 views
in Technique[技术] by (71.8m points)

cmake project build only one specific executable (and its dependencies)

I have a CMake Project with roughly this structure:

.
|-- library1
|   |-- CMakeLists.txt
|-- library2
|   |-- CMakeLists.txt
|-- executables
|   |-- CMakeLists.txt

in executables i generate my 2 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...