经过研究,我现在有了自己的最简单但完整的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)
有效地相同。不存在的变量等效于一个空列表。列表内部只是一个带有分号的字符串,用于分隔元素。因此,根据定义,仅包含一个元素的列表就是该元素,不进行装箱。变量是全局的。内置函数会提供某种形式的命名参数,因为它们期望某些id PUBLIC
或DESTINATION
在其参数列表中对参数进行分组。但这不是语言功能,这些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)
For example I don't want to update my CMakeList.txt when I am adding a new folder in my src tree
您能举一个IDE自动收集源代码的示例吗?