Answers:
就像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
)。
CMAKE_ARCHIVE_OUTPUT_DIRECTORY
考虑到命令install(TARGETS <target_name>)
仍然抱怨“给可执行目标没有RUNTIME DESTINATION” ,设置的目的是什么?该变量应该提供默认值,因此该install
命令不应抱怨缺少a RUNTIME DESTINATION
。
使用EXECUTABLE_OUTPUT_PATH
CMake变量设置所需的路径。有关详细信息,请参阅在线CMake文档:
对于我来说,我使用的是cmake 3.5,但below(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_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/"
)
cat CMakeLists.txt
project (hello)
set(CMAKE_BINARY_DIR "/bin")
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR})
add_executable (hello hello.c)
要添加到此:
如果您使用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 )