最简单但完整的CMake示例

问题描述 投票:100回答:2

不知何故,我对CMake的工作原理感到困惑。每当我认为我越来越了解CMake是如何写的时候,它就会在我读到的下一个例子中消失。我想知道的是,我应该如何构建我的项目,以便我的CMake在将来需要最少的维护。例如,当我在src树中添加一个新文件夹时,我不想更新我的CMakeList.txt,这与所有其他src文件夹完全一样。

这就是我想象我的项目结构的方式,但请这只是一个例子。如果推荐的方式不同,请告诉我,告诉我该怎么做。

myProject
    src/
        module1/
            module1.h
            module1.cpp
        module2/
            [...]
        main.cpp
    test/
        test1.cpp
    resources/
        file.png
    bin
        [execute cmake ..]

顺便说一下,我的程序知道资源在哪里是很重要的。我想知道推荐的资源管理方式。我不想用“../resources/file.png”访问我的资源

c++ cmake project-setup
2个回答
80
投票

经过一些研究后,我现在拥有了自己最简单但完整的cmake示例。在这里,它试图涵盖大部分基础知识,包括资源和包装。

它非标准的一件事是资源处理。默认情况下,cmake希望将它们放在/ usr / share /,/ usr / local / share /和Windows上等效的东西上。我想要一个简单的zip / tar.gz,你可以在任何地方提取并运行。因此,相对于可执行文件加载资源。

理解cmake命令的基本规则是以下语法:<function-name>(<arg1> [<arg2> ...])没有逗号或半色调。每个参数都是一个字符串。 foobar(3.0)foobar("3.0")是一样的。你可以用set(args arg1 arg2)设置列表/变量。有了这个变量集,foobar(${args})foobar(arg1 arg2)实际上是相同的。不存在的变量等同于空列表。列表在内部只是一个字符串,用分号分隔元素。因此,只有一个元素的列表只是定义该元素,不会发生装箱。变量是全球性的。内置函数提供某种形式的命名参数,因为它们期望在参数列表中使用PUBLICDESTINATION等一些id来对参数进行分组。但这不是语言特性,那些ID也只是字符串,并由函数实现解析。

你可以克隆来自github的一切

cmake_minimum_required(VERSION 3.0)
project(example_project)

###############################################################################
## file globbing ##############################################################
###############################################################################

# these instructions search the directory tree when cmake is
# invoked and put all files that match the pattern in the variables 
# `sources` and `data`
file(GLOB_RECURSE sources      src/main/*.cpp src/main/*.h)
file(GLOB_RECURSE sources_test src/test/*.cpp)
file(GLOB_RECURSE data resources/*)
# you can use set(sources src/main.cpp) etc if you don't want to
# use globing to find files automatically

###############################################################################
## target definitions #########################################################
###############################################################################

# add the data to the target, so it becomes visible in some IDE
add_executable(example ${sources} ${data})

# just for example add some compiler flags
target_compile_options(example PUBLIC -std=c++1y -Wall -Wfloat-conversion)

# this lets me include files relative to the root src dir with a <> pair
target_include_directories(example PUBLIC src/main)

# this copies all resource files in the build directory
# we need this, because we want to work with paths relative to the executable
file(COPY ${data} DESTINATION resources)

###############################################################################
## dependencies ###############################################################
###############################################################################

# this defines the variables Boost_LIBRARIES that contain all library names
# that we need to link to
find_package(Boost 1.36.0 COMPONENTS filesystem system REQUIRED)

target_link_libraries(example PUBLIC
  ${Boost_LIBRARIES}
  # here you can add any library dependencies
)

###############################################################################
## testing ####################################################################
###############################################################################

# this is for our testing framework
# we don't add REQUIRED because it's just for testing
find_package(GTest)

if(GTEST_FOUND)
  add_executable(unit_tests ${sources_test} ${sources})

  # we add this define to prevent collision with the main
  # this might be better solved by not adding the source with the main to the
  # testing target
  target_compile_definitions(unit_tests PUBLIC UNIT_TESTS)

  # this allows us to use our executable as a link library
  # therefore we can inherit all compiler options and library dependencies
  set_target_properties(example PROPERTIES ENABLE_EXPORTS on)

  target_link_libraries(unit_tests PUBLIC
    ${GTEST_BOTH_LIBRARIES}
    example
  )

  target_include_directories(unit_tests PUBLIC
    ${GTEST_INCLUDE_DIRS} # doesn't do anything on Linux
  )
endif()

###############################################################################
## packaging ##################################################################
###############################################################################

# all install commands get the same destination. this allows us to use paths
# relative to the executable.
install(TARGETS example DESTINATION example_destination)
# this is basically a repeat of the file copy instruction that copies the
# resources in the build directory, but here we tell cmake that we want it
# in the package
install(DIRECTORY resources DESTINATION example_destination)

# now comes everything we need, to create a package
# there are a lot more variables you can set, and some
# you need to set for some package types, but we want to
# be minimal here
set(CPACK_PACKAGE_NAME "MyExample")
set(CPACK_PACKAGE_VERSION "1.0.0")

# we don't want to split our program up into several things
set(CPACK_MONOLITHIC_INSTALL 1)

# This must be last
include(CPack)

36
投票

最基本但完整的例子可以在cmake tutorial中找到:

cmake_minimum_required (VERSION 2.6)
project (Tutorial)
add_executable(Tutorial tutorial.cxx)

对于您的项目示例,您可能有:

cmake_minimum_required (VERSION 2.6)
project (MyProject)
add_executable(myexec src/module1/module1.cpp src/module2/module2.cpp src/main.cpp)
add_executable(mytest test1.cpp)

对于您的其他问题,本教程中还有一种方法:创建一个包含在代码中的可配置头文件。为此,请使用以下内容创建文件configuration.h.in

#define RESOURCES_PATH "@RESOURCES_PATH@"

然后在你的CMakeLists.txt添加:

set(RESOURCES_PATH "${PROJECT_SOURCE_DIR}/resources/"
# configure a header file to pass some of the CMake settings
# to the source code
configure_file (
  "${PROJECT_SOURCE_DIR}/configuration.h.in"
  "${PROJECT_BINARY_DIR}/configuration.h"
)

# add the binary tree to the search path for include files
# so that we will find TutorialConfig.h
include_directories("${PROJECT_BINARY_DIR}")

最后,您需要代码中的路径,您可以:

#include "configuration.h"

...

string resourcePath = string(RESOURCE_PATH) + "file.png";
© www.soinside.com 2019 - 2024. All rights reserved.