CMake上手:学习和练习案例素材

CMake上手:学习和练习案例素材git clone https://github.com/ttroy50/cmake-examples.git素材分类目录准备01-basicA-hello-cmakeB-hello-headersC-static-libraryD-shared-libraryE-installingF-build-typeG-compile-flagsH-third-party-libraryI-compiling-with-clangJ-building-with-ninjaK-i
摘要由CSDN通过智能技术生成

CMake上手:学习和练习案例素材
git clone https://github.com/ttroy50/cmake-examples.git

准备

开始之前,检查一下自己的cmake版本是不错的选择,毕竟我们要编写的CMakeists文件都是要用cmake来执行的,而不同版本的cmake支持的语法可能是不同的。

$ cmake --version
cmake version 3.16.3

CMake suite maintained and supported by Kitware (kitware.com/cmake).

另外,使用CMake编译一个工程的简要过程:

mkdir build   #在工程文件夹里创建一个build文件夹并进入
cd build      
cmake ..    #对工程目录中的CMakeLists.txt文件,用cmake生成编译需要的材料
make      #编译连接生成可执行文件

然后,就可以运行可执行文件:

./XXXX 

01-basic

A-hello-cmake

任何CMakeLists.txt文件,都要在开头声明对cmake版本的要求。
对于只包含一个main函数文件的简单工程,
(1)定义工程名称
(2)为工程添加可执行文件。

# 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)

B-hello-headers

对于有include和src目录的工程结构(也是通常的应用程序工程),源码文件可能不止一个(其中有一个包含main入口函数),因此,
(1)定义SOURCES变量来列出所有的源码文件,并在为工程添加可执行文件时,用SOURCES变量代替。
(2)include文件夹里的头文件,是需要包含的内容,使用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
)

C-static-library

对于想要生成静态连接库的工程,
(1)把相应类的源码文件add_library为STATIC,并使用target_include_directories标明其所需要的头文件就可以了(注意是PUBLIC),这部分最后编译出来的就是*.a文件(在windows系统上就是我们常见的*.lib),是工程发布给别人的交付件。
(2)为了对这个静态文件进行测试,一个工程的main入口文件可以生成可执行文件,并将对应的静态连接库作为类似于外部依赖进行连接,以进行必要的测试,使用target_link_libraries声明可执行文件对于静态连接库的依赖。如果只是要制作和发布静态库,最终这部分可以不要,把.a和头文件发布就可以了,用户程序通过文档参考来使用静态库即可。

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
)

D-shared-library

对于想要生成动态链接库的工程,
(1)add_library的时候就要声明为SHARED方式,然后,把头文件声明出来(注意和静态连接库一样是PUBLIC),两句最后就可以编译出来*.so文件(在windows系统上就是我们常见的*.dll),这是动态链接库工程的交付件。
(2)也是为了能够进行必要测试,工程可以同时编译出带main入口函数的文件,生成可执行文件。
【备注】这里编译动态连接库中,有一句add_library(hello::library ALIAS hello_library),并非必要,只是对库的名称进行了重命名,所以,后面编译可执行文件时,target_link_libraries里连接的依赖库名称,就可以写成hello::library或者原来的名字hello_library都行,如果没有这句重命名,那么连接的依赖库名称,只能使用原来的工程名称hello_library(由于这是在一个工程里面,动态链接库和可执行文件都是这个名称,有时候可能让人迷惑,如果进行专门的测试,另外建立一个可执行工程,那么就不会容易迷惑了)

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
<
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值