Cmake构建程序学习笔记

Posted COCO_PEAK_NOODLE

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Cmake构建程序学习笔记相关的知识,希望对你有一定的参考价值。

最简单的main程序

# Set the minimum version of CMake that can be used
# To find the cmake version run
# $ cmake --version
cmake_minimum_required(VERSION 3.5)

# Set the project name
# 可有可无 
#project (hello_cmake)

# Add an executable
add_executable(hello_cmake main.cpp)

2-cmake 之 PUBLIC|PRIVATE|INTERFACE 关键字
https://www.ravenxrz.ink/archives/e40194d1.html,这篇文章讲的很好、

3-复杂一些, 使用set和target_include_directories

# Set the minimum version of CMake that can be used
# To find the cmake version run
# $ cmake --version
cmake_minimum_required(VERSION 3.5)

# Set the project name
project (hello_headers)

# Create a sources variable with a link to all cpp files to compile
set(SOURCES
    src/Hello.cpp
    src/main.cpp
)

# Add an executable with the above sources
add_executable(hello_headers $SOURCES)

# Set the directories that should be included in the build command for this target
# when running g++ these will be included as -I/directory/path/
target_include_directories(hello_headers
    PRIVATE 
    $PROJECT_SOURCE_DIR/include
)

4-生成静态库并使用

cmake_minimum_required(VERSION 3.5)

project(hello_library)

############################################################
# Create a library
############################################################

#Generate the static library from the library sources
add_library(hello_library STATIC 
    src/Hello.cpp
)

target_include_directories(hello_library
    PUBLIC 
        $PROJECT_SOURCE_DIR/include
)


############################################################
# Create an executable
############################################################

# Add an executable with the above sources
add_executable(hello_binary 
    src/main.cpp
)

# link the new hello_library target with the hello_binary target
target_link_libraries( hello_binary
    PRIVATE 
        hello_library
)

5-生成动态库并使用

cmake_minimum_required(VERSION 3.5)

project(hello_library)

############################################################
# Create a library
############################################################

#Generate the shared library from the library sources
add_library(hello_library SHARED 
    src/Hello.cpp
)
add_library(hello::library ALIAS hello_library)

target_include_directories(hello_library
    PUBLIC 
        $PROJECT_SOURCE_DIR/include
)

############################################################
# Create an executable
############################################################

# Add an executable with the above sources
add_executable(hello_binary
    src/main.cpp
)

# link the new hello_library target with the hello_binary target
target_link_libraries( hello_binary
    PRIVATE 
    hello::library
)

6-安装生成文件

cmake_minimum_required(VERSION 3.5)

project(cmake_examples_install)

############################################################
# Create a library
############################################################

#Generate the shared library from the library sources
add_library(cmake_examples_inst SHARED
    src/Hello.cpp
)

target_include_directories(cmake_examples_inst
    PUBLIC 
        $PROJECT_SOURCE_DIR/include
)

############################################################
# Create an executable
############################################################

# Add an executable with the above sources
add_executable(cmake_examples_inst_bin
    src/main.cpp
)

# link the new hello_library target with the hello_binary target
target_link_libraries( cmake_examples_inst_bin
    PRIVATE 
        cmake_examples_inst
)

############################################################
# Install
############################################################

# Binaries
install (TARGETS cmake_examples_inst_bin
    DESTINATION bin)

# Library
# Note: may not work on windows
install (TARGETS cmake_examples_inst
    LIBRARY DESTINATION lib)

# Header files
install(DIRECTORY $PROJECT_SOURCE_DIR/include/ 
    DESTINATION include)

# Config
install (FILES cmake-examples.conf
    DESTINATION etc)

7-设置CMAKE_BUILD_TYPE参数

# Set the minimum version of CMake that can be used
# To find the cmake version run
# $ cmake --version
cmake_minimum_required(VERSION 3.5)

# Set a default build type if none was specified
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
  message("Setting build type to 'RelWithDebInfo' as none was specified.")
  set(CMAKE_BUILD_TYPE RelWithDebInfo CACHE STRING "Choose the type of build." FORCE)
  # Set the possible values of build type for cmake-gui
  set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release"
    "MinSizeRel" "RelWithDebInfo")
endif()

# Set the project name
project (build_type)

# Add an executable
add_executable(cmake_examples_build_type main.cpp)

使用cmake-gui打开后会显示

8-定义宏 target_compile_definitions

cmake_minimum_required(VERSION 3.5)

# Set a default C++ compile flag
set (CMAKE_CXX_FLAGS "$CMAKE_CXX_FLAGS -DEX2" CACHE STRING "Set C++ Compiler Flags" FORCE)

# Set the project name
project (compile_flags)

# Add an executable
add_executable(cmake_examples_compile_flags main.cpp)

target_compile_definitions(cmake_examples_compile_flags 
    PRIVATE EX3
)

9-使用第3方库

cmake_minimum_required(VERSION 3.5)

# Set the project name
project (third_party_include)


# find a boost install with the libraries filesystem and system
find_package(Boost 1.46.1 REQUIRED COMPONENTS filesystem system)

# check if boost was found
if(Boost_FOUND)
    message ("boost found")
else()
    message (FATAL_ERROR "Cannot find Boost")
endif()

# Add an executable
add_executable(third_party_include main.cpp)

# link against the boost libraries
target_link_libraries( third_party_include
    PRIVATE
        Boost::filesystem
)

10-查看是否支持C++11

# Set the minimum version of CMake that can be used
# To find the cmake version run
# $ cmake --version
cmake_minimum_required(VERSION 2.8)

# Set the project name
project (hello_cpp11)

# try conditional compilation
include(CheckCXXCompilerFlag)
CHECK_CXX_COMPILER_FLAG("-std=c++11" COMPILER_SUPPORTS_CXX11)
CHECK_CXX_COMPILER_FLAG("-std=c++0x" COMPILER_SUPPORTS_CXX0X)

# check results and add flag
if(COMPILER_SUPPORTS_CXX11)#
    set(CMAKE_CXX_FLAGS "$CMAKE_CXX_FLAGS -std=c++11")
elseif(COMPILER_SUPPORTS_CXX0X)#
    set(CMAKE_CXX_FLAGS "$CMAKE_CXX_FLAGS -std=c++0x")
else()
    message(STATUS "The compiler $CMAKE_CXX_COMPILER has no C++11 support. Please use a different C++ compiler.")
endif()

# Add an executable
add_executable(hello_cpp11 main.cpp)

还可以这样写

# Set the minimum version of CMake that can be used
# To find the cmake version run
# $ cmake --version
cmake_minimum_required(VERSION 3.1)

# Set the project name
project (hello_cpp11)

# set the C++ standard to C++ 11
set(CMAKE_CXX_STANDARD 11)

# Add an executable
add_executable(hello_cpp11 main.cpp)

11-查看当前版本的支持特性

# Set the minimum version of CMake that can be used
# To find the cmake version run
# $ cmake --version
cmake_minimum_required(VERSION 3.1)

# Set the project name
project (hello_cpp11)

# Add an executable
add_executable(hello_cpp11 main.cpp)

# set the C++ standard to the appropriate standard for using auto
target_compile_features(hello_cpp11 PUBLIC cxx_auto_type)

# Print the list of known compile features for this version of CMake
message("List of compile features: $CMAKE_CXX_COMPILE_FEATURES")

12-构建各个子系统组成的软件架构

cmake_minimum_required (VERSION 3.5)

project(subprojects)

# Add sub directories
add_subdirectory(sublibrary1) //库
add_subdirectory(sublibrary2) //库
add_subdirectory(subbinary) //可执行文件

可执行部分,subbinary

project(subbinary)

# Create the executable
add_executable($PROJECT_NAME main.cpp)

# Link the static library from subproject1 using its alias sub::lib1
# Link the header only library from subproject2 using its alias sub::lib2
# This will cause the include directories for that target to be added to this project
target_link_libraries($PROJECT_NAME
    sub::lib1
    sub::lib2
)

库部分 sublibrary1

# Set the project name
project (sublibrary1)

# Add a library with the above sources
add_library($PROJECT_NAME src/sublib1.cpp)
add_library(sub::lib1 ALIAS $PROJECT_NAME)

target_include_directories( $PROJECT_NAME
    PUBLIC $PROJECT_SOURCE_DIR/include
)

只含有头文件的库部分 sublibrary2

# Set the project name
project (sublibrary2)

add_library($PROJECT_NAME INTERFACE)
add_library(sub::lib2 ALIAS $PROJECT_NAME)

target_include_directories($PROJECT_NAME
    INTERFACE
        $PROJECT_SOURCE_DIR/include
)

13- Cmake与ini文件的交互

cmake_minimum_required(VERSION 3.5)

# Set the project name
project (cf_example)

# set a project version
set (cf_example_VERSION_MAJOR 0)
set (cf_example_VERSION_MINOR 2)
set (cf_example_VERSION_PATCH 1)
set (cf_example_VERSION "$cf_example_VERSION_MAJOR.$cf_example_VERSION_MINOR.$cf_example_VERSION_PATCH")

# Call configure files on ver.h.in to set the version.
# Uses the standard $VARIABLE syntax in the file
configure_file(ver.h.ini $PROJECT_BINARY_DIR/ver.h)

# configure the path.h.in file.
# This file can only use the @VARIABLE@ syntax in the file
configure_file(path.h.ini $PROJECT_BINARY_DIR/path.h @ONLY)

# Add an executable
add_executable(cf_example
    main.cpp
)

# include the directory with the new files
target_include_directories( cf_example
    PUBLIC
        $CMAKE_BINARY_DIR
)

path.h.ini

#ifndef __PATH_H__
#define __PATH_H__

// version variable that will be substituted by cmake
// This shows an example using the @ variable type
const char* path = "@CMAKE_SOURCE_DIR@";

#endif

14-cmake使用protobuf

cmake_minimum_required(VERSION 3.5)

# Set the project name
project (protobuf_example)

# find the protobuf compiler and libraries
find_package(Protobuf REQUIRED)

# check if protobuf was found
if(PROTOBUF_FOUND)
    message ("protobuf found")
else()
    message (FATAL_ERROR "Cannot find Protobuf")
endif()

# Generate the .h and .cxx files
PROTOBUF_GENERATE_CPP(PROTO_SRCS PROTO_HDRS AddressBook.proto)

# Print path to generated files
message ("PROTO_SRCS = $PROTO_SRCS")
message ("PROTO_HDRS = $PROTO_HDRS")

# Add an executable
add_executable(protobuf_example
    main.cpp
    $PROTO_SRCS
    $PROTO_HDRS)

target_include_directories(protobuf_example
    PUBLIC
    $PROTOBUF_INCLUDE_DIRS
    $CMAKE_CURRENT_BINARY_DIR
)

# link the exe against the libraries
target_link_libraries(protobuf_example
    PUBLIC
    $PROTOBUF_LIBRARIES
)

以上是关于Cmake构建程序学习笔记的主要内容,如果未能解决你的问题,请参考以下文章

cmake 学习笔记

cmake 学习笔记

opencv的学习笔记3

CLion_CMake学习笔记

CLion_CMake学习笔记

PREACT学习笔记