父CMakeLists.txt将所有库设置为/ lib目录.
但是,我的一个库需要放入/ python库中.我在Windows上运行的设置.意思是,除了我的python特定库之外的所有库都放在/ lib文件夹中,python lib放在/ python文件夹中.
当我在Linux上构建时出现问题.所有库,包括python特定库,都放在/ lib文件夹中. FORCE选项什么都不做.
如果我只为一个平台构建,我可以处理任一目录布局.但我真的希望跨平台保持相同的布局.
CMakeLists.txt如下:
-Parent CMakeLists.txt
cmake_minimum_required(VERSION 2.6) project(renderer2d) #enable debug symbols by default if(CMAKE_BUILD_TYPE STREQUAL "") set(CMAKE_BUILD_TYPE Debug) endif() #(you can also set on cl: -D CMAKE_BUILD_TYPE=Release) #place outside of Debug/Release folders SET(OUTPUT_BINDIR ${PROJECT_BINARY_DIR}/bin) MAKE_DIRECTORY(${OUTPUT_BINDIR}) SET(OUTPUT_LIBDIR ${PROJECT_BINARY_DIR}/lib) MAKE_DIRECTORY(${OUTPUT_LIBDIR}) SET (CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${OUTPUT_LIBDIR} CACHE PATH "build directory") SET (CMAKE_RUNTIME_OUTPUT_DIRECTORY ${OUTPUT_BINDIR} CACHE PATH "build directory") IF(WIN32) SET (CMAKE_LIBRARY_OUTPUT_DIRECTORY ${OUTPUT_BINDIR} CACHE PATH "build directory") ELSE(WIN32) SET(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${OUTPUT_LIBDIR} CACHE PATH "build directory") ENDIF(WIN32) # For each configuration (Debug,Release,MinSizeRel... and/or anything the user chooses) FOREACH(CONF ${CMAKE_CONFIGURATION_TYPES}) # Go uppercase (DEBUG,RELEASE...) STRING(TOUPPER "${CONF}" CONF) SET("CMAKE_ARCHIVE_OUTPUT_DIRECTORY_${CONF}" "${OUTPUT_LIBDIR}") SET("CMAKE_RUNTIME_OUTPUT_DIRECTORY_${CONF}" "${OUTPUT_BINDIR}") IF(WIN32) SET("CMAKE_LIBRARY_OUTPUT_DIRECTORY_${CONF}" "${OUTPUT_BINDIR}") ELSE() SET("CMAKE_LIBRARY_OUTPUT_DIRECTORY_${CONF}" "${OUTPUT_LIBDIR}") ENDIF() ENDFOREACH() set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") #set the source directory file(GLOB SOURCES src/*.cpp) add_subdirectory(shape) add_subdirectory(py_shape) add_subdirectory(scripts) #define sources and executable set(EXECUTABLE_NAME "renderer2d") add_executable(${EXECUTABLE_NAME} ${SOURCES}) #find python find_package(PythonInterp) find_package(PythonLibs 2.7 required) include_directories(${PYTHON_INCLUDE_DIRS}) #detect and add SFML #this line checks a cmake file for hints on where to find cmake set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake_modules" ${CMAKE_MODULE_PATH}) #find any version 2.x of SFML #see the FindSFML.cmake file for additional details and instructions find_package(SFML 2 required system window graphics network audio) include_directories(${SFML_INCLUDE_DIR}) #find and include Boost python libraries set(Boost_USE_STATIC_LIBS OFF) find_package(Boost COMPONENTS python system filesystem required) include_directories(${Boost_INCLUDE_DIR}) #link all found libraries to the executable if(WIN32) target_compile_definitions(${EXECUTABLE_NAME} PRIVATE $<$<BOOL:${MSVC}>:BOOST_ALL_NO_LIB>) endif(WIN32) target_link_libraries(${EXECUTABLE_NAME} ${PYTHON_LIBRARIES} ${SFML_LIBRARIES} ${Boost_LIBRARIES} shape)
-Child CMakeLists.txt
cmake_minimum_required(VERSION 2.8) project(py_shape CXX) #set file variables file(GLOB SOURCE src/*.cpp) file(GLOB HEADERS inc/*.hpp) #place outside of Debug/Release folders SET(OUTPUT_BINDIR ${CMAKE_BINARY_DIR}/python) MAKE_DIRECTORY(${OUTPUT_BINDIR}) set(OUTPUT_LIBDIR ${CMAKEK_BINARY_DIR}/python) MAKE_DIRECTORY(${OUTPUT_LIBDIR}) SET(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${OUTPUT_LIBDIR} CACHE PATH "build directory") SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${OUTPUT_BINDIR} CACHE PATH "build directory") IF(WIN32) SET(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${OUTPUT_BINDIR} CACHE PATH "build directory") ELSE(WIN32) SET(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${OUTPUT_LIBDIR} CACHE PATH "build directory") ENDIF(WIN32) #for each configuration FOREACH(CONF ${CMAKE_CONFIGURATION_TYPES}) #Go uppercase {DEBUG,RELEASE...) SET("CMAKE_ARCHIVE_OUTPUT_DIRECTORY_${CONF}" "${OUTPUT_LIBDIR}") SET("CMAKE_RUNTIME_OUTPUT_DIRECTORY_${CONF}" "${OUTPUT_BINDIR}") IF(WIN32) SET("CMAKE_LIBRARY_OUTPUT_DIRECTORY_${CONF}" "${OUTPUT_BINDIR}") ELSE() SET("CMAKE_LIBRARY_OUTPUT_DIRECTORY_${CONF}" "${OUTPUT_LIBDIR}") ENDIF() ENDFOREACH() #find packages find_package(PythonInterp) find_package(PythonLibs 2.7 required) include_directories(${PYTHON_INCLUDE_DIRS}) find_package(Boost COMPONENTS python required) include_directories(${Boost_INCLUDE_DIR}) set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake_modules" ${CMAKE_MODULE_PATH}) find_package(SFML 2 required system window graphics network audio) include_directories(${SFML_INCLUDE_DIR}) #build the library add_library(python_shape MODULE ${SOURCE}) #enable C++11 if available target_compile_features(python_shape PRIVATE cxx_range_for) #link library target_link_libraries(python_shape shape ${Boost_LIBRARIES} ${PYTHON_LIBRARIES} ${SFML_LIBRARIES}) #drop "lib" from the library name set_target_properties(python_shape PROPERTIES PREFIX "") if(WIN32) #set extension to ".pyd" set_target_properties(python_shape PROPERTIES SUFFIX ".pyd") endif(WIN32)
我使用MinGW和CMake 3.3.0成功测试了以下内容(我将示例缩小了一点,专注于输出目录):
的CMakeLists.txt
cmake_minimum_required(VERSION 2.6) project(renderer2d C CXX) # TODO: Remove,this is just for testing file(WRITE "src/renderer2d.cpp" "int main(void) {}") file(WRITE "py_shape/src/py_shape.cpp" "") #enable debug symbols by default if(CMAKE_BUILD_TYPE STREQUAL "") set(CMAKE_BUILD_TYPE Debug) endif() #(you can also set on cl: -D CMAKE_BUILD_TYPE=Release) #place outside of Debug/Release folders #see http://www.cmake.org/Wiki/CMake_Useful_Variables set(EXECUTABLE_OUTPUT_PATH "${PROJECT_BINARY_DIR}/bin") if(WIN32) set(LIBRARY_OUTPUT_PATH "${EXECUTABLE_OUTPUT_PATH}") else() set(LIBRARY_OUTPUT_PATH "${PROJECT_BINARY_DIR}/lib") endif() #see https://stackoverflow.com/questions/10851247/how-to-activate-c-11-in-cmake if (CMAKE_VERSION VERSION_LESS "3.1") if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU") set(CMAKE_CXX_FLAGS "--std=gnu++11 ${CMAKE_CXX_FLAGS}") endif() else() set(CMAKE_CXX_STANDARD 11) endif() #set the source directory file(GLOB SOURCES src/*.cpp) add_subdirectory(py_shape) #define sources and executable add_executable(${PROJECT_NAME} ${SOURCES}) add_dependencies(${PROJECT_NAME} python_shape)
py_shape /的CMakeLists.txt
#set file variables file(GLOB SOURCE src/*.cpp) file(GLOB HEADERS inc/*.hpp) #build the library add_library(python_shape MODULE ${SOURCE}) set_target_properties(python_shape PROPERTIES LIBRARY_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/python") #drop "lib" from the library name set_target_properties(python_shape PROPERTIES PREFIX "") if(WIN32) #set extension to ".pyd" set_target_properties(python_shape PROPERTIES SUFFIX ".pyd") endif()
现在python_shape.pyd在python子目录中创建.
我改变/删除了什么:
>在子CMakeLists.txt中设置…_ OUTPUT_DIRECTORY全局变量不是必需的和/或不起作用
>添加了LIBRARY_OUTPUT_DIRECTORY
以覆盖python_shape MODULE库目标的输出目录(另请参见例如Custom Directory for CMake Library Output)
>删除了“按配置”设置,因为我认为这是一个功能,多配置使像Visual Studio这样的环境将不同的配置二进制文件放入同名的子文件夹中.
>在-std = c 11周围添加了一些if语句.它也只能在主CMakeLists.txt中设置一次
>您不需要“手动”创建输出目录,它们由make环境自动创建
我不建议使用file(GLOB ...)
来收集源文件(参见例如Why is cmake file GLOB evil?或CMake/Ninja attempting to compile deleted `.cpp` file).
备择方案
与像CMake这样的所有语言/框架一样,有多种方法可以做.
例如.您还可以使用POST_BUILD步骤将二进制文件复制到公共输出目录:
>见Cmake: use add_custom_command to copy binary to specific location failed when location doesn’t exist
>和 – 使用“生成器表达式”更新一点 – CMake: how to specify different steps for different build configurations for Visual Studio?