몇 가지 연구 끝에 이제는 가장 간단하지만 완전한 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의 예를 들어 주시겠습니까?