我目前正在构建一个带有插件结构的项目。我正在使用CMake编译项目。这些插件在单独的目录中编译。我的问题是,CMake编译和保存二进制文件和插件,动态库,在源代码的目录结构。我如何使CMake保存文件在类似/ bin。目录?
使用EXECUTABLE_OUTPUT_PATH CMake变量来设置所需的路径。详细信息请参考CMake在线文档:
EXECUTABLE_OUTPUT_PATH
CMake 2.8.8文档 .
使用set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "/some/full/path/to/bin")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "/some/full/path/to/bin")
正如奥列格的回答,我认为正确的变量设置是CMAKE_RUNTIME_OUTPUT_DIRECTORY。我们在根目录CMakeLists.txt中使用了以下代码:
CMAKE_RUNTIME_OUTPUT_DIRECTORY
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
你也可以在每个目标的基础上指定输出目录:
set_target_properties( targets... PROPERTIES ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib" LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib" RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin" )
在这两种情况下,你都可以将_[CONFIG]附加到变量/属性名后,使输出目录应用于特定的配置(配置的标准值是DEBUG, RELEASE, MINSIZEREL和RELWITHDEBINFO)。
_[CONFIG]
DEBUG
RELEASE
MINSIZEREL
RELWITHDEBINFO
$ cat CMakeLists.txt project (hello) set(EXECUTABLE_OUTPUT_PATH "bin") add_executable (hello hello.c)
cat CMakeLists.txt project (hello) set(CMAKE_BINARY_DIR "/bin") set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}) add_executable (hello hello.c)
使用以下行配置:
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/build/)
这个${PROJECT_SOURCE_DIR}是你当前的源目录,项目所在的目录 如果想知道为什么是${EXECUTABLE_OUTPUT_PATH},检查这个文件CMakeCache.txt,然后搜索关键字output path,这里定义的所有变量,它将给出项目所有设置的完整解释
${PROJECT_SOURCE_DIR}
${EXECUTABLE_OUTPUT_PATH}
CMakeCache.txt
output path
对于我来说,我使用的是cmake 3.5,下面(set variable)不起作用:
set variable
set( ARCHIVE_OUTPUT_DIRECTORY "/home/xy/cmake_practice/lib/" LIBRARY_OUTPUT_DIRECTORY "/home/xy/cmake_practice/lib/" RUNTIME_OUTPUT_DIRECTORY "/home/xy/cmake_practice/bin/" )
但这是有效的(set set_target_properties):
set set_target_properties
set_target_properties(demo5 PROPERTIES ARCHIVE_OUTPUT_DIRECTORY "/home/xy/cmake_practice/lib/" LIBRARY_OUTPUT_DIRECTORY "/home/xy/cmake_practice/lib/" RUNTIME_OUTPUT_DIRECTORY "/home/xy/cmake_practice/bin/" )
再加上:
如果你正在使用CMAKE生成Visual Studio解决方案,并且你想要Visual Studio将编译好的文件输出到/bin中,Peter的答案需要稍微修改一下:
# set output directories for all builds (Debug, Release, etc.) foreach( OUTPUTCONFIG ${CMAKE_CONFIGURATION_TYPES} ) string( TOUPPER ${OUTPUTCONFIG} OUTPUTCONFIG ) set( CMAKE_ARCHIVE_OUTPUT_DIRECTORY_${OUTPUTCONFIG} ${CMAKE_SOURCE_DIR}/lib ) set( CMAKE_LIBRARY_OUTPUT_DIRECTORY_${OUTPUTCONFIG} ${CMAKE_SOURCE_DIR}/lib ) set( CMAKE_RUNTIME_OUTPUT_DIRECTORY_${OUTPUTCONFIG} ${CMAKE_SOURCE_DIR}/bin ) endforeach( OUTPUTCONFIG CMAKE_CONFIGURATION_TYPES )
对serg06的回答的进一步细化:
要强制所有生成器和所有构建配置都使用这三个路径,您可以添加一个空生成器表达式,如下所示:
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "$<0:>${CMAKE_BINARY_DIR}/bin") # .exe and .dll set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "$<0:>${CMAKE_BINARY_DIR}/lib") # .so and .dylib set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "$<0:>${CMAKE_BINARY_DIR}/lib") # .lib and .a
这样做的副作用是迫使Visual Studio生成器使用指定的路径,而不是附加特定于配置的子目录,而不需要foreach循环。
foreach