Where to put the binary in CMake?

2019-06-19 03:41发布

问题:

in my project, all source code resides in a folder named "src". There's a CMakeLists.txt file in my projects root (above "src"), but it merely declares the project and includes the "src" subdirectory. The CMakeLists.txt file under src does all the work, including "add_binary".

(Is that a common way of doing it, or should I put all the intelligence in the CMakeLists.txt file at the root level?)

If I build the project now, my binary is placed into the src folder, but this doesn't make a lot of sense, I'd rather have it in the root folder or a dedicated "bin" folder.

How do you do this?

回答1:

If you want to put all your executable files in a subdirectory called "bin", then you can use the following line in the top CMakeLists.txt file:

SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")

Just remove /bin and executables will be created in the root directory. A similar variable exists for libraries: CMAKE_LIBRARY_OUTPUT_DIRECTORY.

PS. Adding per-directory logic is fine. It seems to be the common way to do things and keeps things nicely organized.



标签: cmake