如何通过CMakeLists母建项目?
Posted
技术标签:
【中文标题】如何通过CMakeLists母建项目?【英文标题】:How to build a project through CMakeLists mother? 【发布时间】:2011-12-08 02:09:09 【问题描述】:假设我的项目有几个子模块,架构是:
/CMakeLists.txt (Base) -> 调用子目录 /projectName/CMakeLists.txt (Child) -> 调用子目录moduleName并添加主项目源 /projectName/xxx.cpp /projectName/moduleName/CMakeLists.txt (Child of Child) -> 添加模块源 /projectName/moduleName/xxx.cpp我可以只用一个 CMakeLists 添加许多源,但我认为 CMake 已经以这种方式工作。
我的 CMakeLists.txt:
主 CMakeLists.txt:
# CMake Minimum Version Required ( >= 2.8 )
cmake_minimum_required ( VERSION 2.8 )
# CMake Project Name and Languages
project ( LightUpdater C CXX )
# Add Compiler Definitions
add_definitions ( -Wall -Werror )
# Include source headers
include_directories ( lightupdater )
# Add custom module folder
set( CMAKE_MODULE_PATH "$CMAKE_SOURCE_DIR/CMakeModules;$CMAKE_MODULE_PATH" )
set ( EXECUTABLE_OUTPUT_PATH $PROJECT_BINARY_DIR ) # Binary output folder
# Setup project variables
set ( LIGHTUPDATER_OUTPUT lightupdater )
set ( LIGHTUPDATER_SOURCES "" )
# Add Source Subdirectory
add_subdirectory ( lightupdater )
/projectName/CMakeLists.txt
# Setup the source code files
list ( APPEND LIGHTUPDATER_SOURCES $CMAKE_CURRENT_SOURCE_DIR/lightupdater.cpp )
# Add Modules
add_subdirectory ( interface )
/projectName/moduleName/CMakeLists.txt
list ( APPEND LIGHTUPDATER_SOURCES $CMAKE_CURRENT_SOURCE_DIR/gui.cpp )
# Qt4
# ----------------------------------------------------------------------------------
# Find Qt4 (REQUIRED)
find_package( Qt4 REQUIRED )
# Compiler setup
include ( $QT_USE_FILE )
add_definitions ( $QT_DEFINITIONS )
include_directories( $QT_INCLUDE_DIR )
# Setup the interface sources
set( INTERFACE_HEADER $CMAKE_CURRENT_SOURCE_DIR/gui.hpp )
# Process headers
QT4_WRAP_CPP( INTERFACE_HEADER_MOC $INTERFACE_HEADER )
list( APPEND LIGHTUPDATER_SOURCES $INTERFACE_HEADER_MOC )
# ----------------------------------------------------------------------------------
# Define Qt Operating System
# Created by Renato Utsch
# ----------------------------------------------------------------------------------
set( GUI_TYPE "" )
if( APPLE )
set( GUI_TYPE MACOSX_BUNDLE )
elseif( WIN32 )
set( GUI_TYPE WIN32 )
endif()
# Compiler call
add_executable( $LIGHTUPDATER_OUTPUT $GUI_TYPE $LIGHTUPDATER_SOURCES )
# Linking the 3rd party librarys
target_link_libraries( $LIGHTUPDATER_OUTPUT $QT_LIBRARIES )
我希望调用过程发生在编译器 CMakeLists.txt 母体中,而不是现在的模块中。
【问题讨论】:
【参考方案1】:所以,您只是希望所有 CMake 代码都驻留在根 CMakeLists.txt 中吗?没问题。
在根 CMakeLists.txt 中,将 add_subdirectory()
替换为 projectName/CMakeLists.txt
,并将 $CMAKE_CURRENT_SOURCE_DIR/lightupdater.cpp
替换为 lightupdater/lightupdater.cpp
。
最后,用同样的方法重写/projectName/moduleName/CMakeLists.txt
。
【讨论】:
用显式路径替换所有使用$CMAKE_CURRENT_SOURCE_DIR
。
但是我需要在模块路径中定义源,然后将变量恢复到根 CMakeLists.txt 并编译。
嗯。那时我没有得到你想要的。可能是 set() 命令中的 PARENT_SCOPE 或 CACHE 说明符会对您有所帮助。
但是在我添加了一个 sub_directory 之后,CMake 不会返回到 Main CMakeLists
将 SOURCE var 放在子 CMakeLists.txt 中不要返回以上是关于如何通过CMakeLists母建项目?的主要内容,如果未能解决你的问题,请参考以下文章