我目前正在构建一个带有插件结构的项目。我正在使用CMake编译项目。这些插件在单独的目录中编译。我的问题是,CMake编译和保存二进制文件和插件,动态库,在源代码的目录结构。我如何让CMake保存文件在一个类似。/bin目录?
当前回答
cat CMakeLists.txt
project (hello)
set(CMAKE_BINARY_DIR "/bin")
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR})
add_executable (hello hello.c)
其他回答
正如Oleg的回答一样,我认为要设置的正确变量是CMAKE_RUNTIME_OUTPUT_DIRECTORY。我们在根目录CMakeLists.txt中使用了以下代码:
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)。
$ cat CMakeLists.txt
project (hello)
set(EXECUTABLE_OUTPUT_PATH "bin")
add_executable (hello hello.c)
对于我来说,我使用的是cmake 3.5,下面(设置变量)不起作用:
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_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/"
)
对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循环。
cat CMakeLists.txt
project (hello)
set(CMAKE_BINARY_DIR "/bin")
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR})
add_executable (hello hello.c)