2

我很新Cmake,我正在尝试使用它作为这种方法的替代方法,但效果不佳。

我的结构其实很简单,我有:

App/src/
App/src/so1
App/src/so2
App/src/so3

在每个App/src/soN文件夹中,我都有一个.cpp文件,该文件将被编译成一个共享对象。我知道我必须CMakeLists.txt在上面列出的每个文件夹中添加一个文件,但我真的迷失了图书馆的链接和包含。

例如,假设在 下有以下文件App/src/so1

App/src/so1/so1.hpp
App/src/so1/so1.cpp

而共享对象so1.so需要-I /some/dir/include,也需要链接-L /some/other/dir/lib -l otherdirlib。什么是适当的指令来做这个 using CMake

4

1 回答 1

3

编辑:

使用文件和 GLOB_RECURSE:

GLOB_RECURSE 将生成一个类似于常规 GLOB 的列表,不同之处在于它将遍历匹配目录的所有子目录并匹配文件。

我建议你像这样重新排列你的文件:

app/
 |_ src/
 |_ include/
 |_ CMakeLists.txt
so1/
 |_ src/
 |_ include/
 |_ CMakeLists.txt
so2/
 |_ src/
 |_ include/
 |_ CMakeLists.txt
so3/
 |_ src/
 |_ include/
 |_ CMakeLists.txt
CMakeLists.txt

应用文件夹中的 CMakeLists.txt:

SET (APP_NAME app)

# Print variables to stdout
MESSAGE ( STATUS )
MESSAGE ( STATUS "Configuring ${APP_NAME}:" )
MESSAGE ( STATUS )

# Source code files
FILE (GLOB_RECURSE SOURCE_FILES src/*.cpp include/*.h include/*.inl)

# Compiler options
IF (CMAKE_COMPILER_IS_GNUCXX)
  ADD_DEFINITIONS (-ansi -Wall -Wextra -Werror -pthread)
ENDIF ()

# Build application
ADD_EXECUTABLE (${APP_NAME} ${SOURCE_FILES})

TARGET_LINK_LIBRARIES (${APP_NAME}
  ${SO1_LIB}
  ${SO2_LIB}
  ${SO3_LIB}
)

INSTALL (TARGETS ${APP_NAME} RUNTIME DESTINATION bin PERMISSIONS OWNER_READ OWNER_WRITE  OWNER_EXECUTE GROUP_READ GROUP_WRITE  GROUP_EXECUTE WORLD_READ WORLD_EXECUTE)

每个 lib 文件夹中的 CMakeLists.txt 将是这样的(相应地更改每个文件中的 LIB_OUTPUT_NAME):

SET (LIB_OUTPUT_NAME so1)

# Print variables to stdout
MESSAGE ( STATUS )
MESSAGE ( STATUS "Configuring ${LIB_OUTPUT_NAME}:" )
MESSAGE ( STATUS )

# Source code, headers and test files
FILE (GLOB_RECURSE HEADER_FILES include/*.h include/*.inl)
FILE (GLOB_RECURSE SOURCE_FILES src/*.cpp include/*.h include/*.inl)
FILE (GLOB_RECURSE TEST_SRC_FILES tests/*.cpp tests/*.h tests/*.inl)

# Compiler options
IF (CMAKE_COMPILER_IS_GNUCXX)
  ADD_DEFINITIONS (-ansi -Wall -Wextra -Werror -pthread)
ENDIF ()

# Build library
ADD_LIBRARY (${LIB_OUTPUT_NAME} SHARED ${SOURCE_FILES})

然后在主 CMakeLists.txt 中:

CMAKE_MINIMUM_REQUIRED (VERSION 2.6)
PROJECT (App CXX)

# Set common variables
IF ("${CMAKE_BUILD_TYPE}" STREQUAL "Release")
  SET (BUILD_DIR build/release)
ELSE ()
  SET (BUILD_DIR build/debug)
ENDIF ()

# Print variables to stdout
MESSAGE ( STATUS )
MESSAGE ( STATUS "Building App:" )
MESSAGE ( STATUS "change a configuration variable with: cmake -D<Variable>=<Value>" )
MESSAGE ( STATUS "CMAKE_INSTALL_PREFIX = ${CMAKE_INSTALL_PREFIX}" )
MESSAGE ( STATUS "CMAKE_BUILD_TYPE = ${CMAKE_BUILD_TYPE}" )
MESSAGE ( STATUS "CMAKE_PREFIX_PATH = ${CMAKE_PREFIX_PATH}" )
MESSAGE ( STATUS )

# Remove build directory on clean target
SET_DIRECTORY_PROPERTIES (PROPERTIES ADDITIONAL_MAKE_CLEAN_FILES ${BUILD_DIR})

# Include directories
INCLUDE_DIRECTORIES (
  "app/include"
  "so1/include"
  "so2/include"
  "so3/include"
)

# Add common compiler flags
IF (CMAKE_COMPILER_IS_GNUCXX)
  SET(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CPP_FLAGS_RELEASE} -O2")
  SET(CMAKE_CXX_FLAGS_DEBUG  "${CMAKE_CXX_FLAGS_DEBUG} -O0 -g)
ENDIF ()

# Build all dynamic Libraries
ADD_SUBDIRECTORY (smartmatic ${BUILD_DIR}/so1)
SET (SO1_LIB ${CMAKE_CURRENT_SOURCE_DIR}/${BUILD_DIR}/so1/so1.so)

ADD_SUBDIRECTORY (saes-runtime ${BUILD_DIR}/so2)
SET (SO2_LIB ${CMAKE_CURRENT_SOURCE_DIR}/${BUILD_DIR}/so2/so2.so)

ADD_SUBDIRECTORY (saes-devices ${BUILD_DIR}/so3)
SET (SO3_LIB ${CMAKE_CURRENT_SOURCE_DIR}/${BUILD_DIR}/so3/so3.so)

# Add dynamic lib to app build for linking.
ADD_SUBDIRECTORY (so1 ${BUILD_DIR}/so1)
SET (SO1_LIB  ${CMAKE_CURRENT_SOURCE_DIR}/${BUILD_DIR}/so1/so1.so)

ADD_SUBDIRECTORY (so2 ${BUILD_DIR}/so2)
SET (SO2_LIB  ${CMAKE_CURRENT_SOURCE_DIR}/${BUILD_DIR}/so2/so2.so)

ADD_SUBDIRECTORY (so3 ${BUILD_DIR}/so3)
SET (SO3_LIB  ${CMAKE_CURRENT_SOURCE_DIR}/${BUILD_DIR}/so3/so3.so)

# Build Application
ADD_SUBDIRECTORY (app ${CMAKE_CURRENT_SOURCE_DIR}/${BUILD_DIR}/app)

更多信息:

http://www.cmake.org/cmake/help/cmake2.6docs.html

于 2013-05-04T05:28:17.313 回答