如何通过CMakeLists母亲建立项目?

时间:2011-12-08 02:09:10

标签: c++ cmake

假设我的项目有几个子模块,架构是:

  • /CMakeLists.txt Base ) - >调用子目录
  • /projectName/CMakeLists.txt Child ) - >调用子目录moduleName并添加主项目源
  • /projectName/xxx.cpp
  • /projectName/moduleName/CMakeLists.txt (儿童之子) - >添加模块源
  • /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 个答案:

答案 0 :(得分:0)

那么,您只是希望所有CMake代码都驻留在根CMakeLists.txt中?没问题。

在根CMakeLists.txt中,将add_subdirectory()替换为projectName/CMakeLists.txt,并将${CMAKE_CURRENT_SOURCE_DIR}/lightupdater.cpp替换为lightupdater/lightupdater.cpp

最后,以同样的方式重写/projectName/moduleName/CMakeLists.txt