自动化构建系统CMake怎么用

发布时间:2022-03-05 09:25:45 作者:小新
来源:亿速云 阅读:217

这篇文章主要为大家展示了“自动化构建系统CMake怎么用”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“自动化构建系统CMake怎么用”这篇文章吧。

CMake 是一个跨平台的自动化构建系统,它使用一个名为 CMakeLists.txt 的文件来描述构建过程,可以产生标准的构建文件,如 Unix 的 Makefile 或Windows Visual C++ 的 projects/workspaces 。

自动化构建系统CMake怎么用

例1:Hello World

源代码只有一个文件HelloWorld.cpp

#includeint main(int argc, char *argv[]){
  std::cout "Hello World!" return 0;
}
123456

CMakeLists.txt也只有三行而已(使用cmake管理项目的过程,也就是编写CMakeLists.txt的过程)

cmake_minimum_required(VERSION 2.8.9)
project (hello)
add_executable(hello helloworld.cpp)
123

第一行用于指定cmake最低版本 第二行指定项目名称(这个名称是任意的) 第三行指定编译一个可执行文件,hello是第一个参数,表示生成可执行文件的文件名(这个文件名也是任意的),第二个参数helloworld.cpp则用于指定源文件。

如果您电脑上已经安装了cmake,那么我们就已经完事具备了。 第一步,用cmake生成Makefile文件


例2: 包含目录结构的项目

在例1中完全体现不出cmake的任何优势,用g++一行可以解决的问题我们绕了一大圈。可是cmake本来的优势就是管理庞大的项目的。 这个例子用最小的程序来体现一个带目录结构的项目。其中有源文件目录,头文件目录。 自动化构建系统CMake怎么用

cmake_minimum_required(VERSION 2.8.9)
project(directory_test)#Bring the headers, such as Student.h into the projectinclude_directories(include)#Can manually add the sources using the set command as follows:#set(SOURCES src/mainapp.cpp src/Student.cpp)#However, the file(GLOB...) allows for wildcard additions:file(GLOB SOURCES "src/*.cpp")

add_executable(testStudent ${SOURCES})
12345678910111213

和第一个例子比起来,CMakelist.txt有如下改变:

  1. 使用include_directories() 包含头文件目录
  2. 使用set(SOURCES … ) 或GLOB (or GLOB_RECURSE) 设置源文件SOURCES
  3. add_executable 使用变量SOURCES ,而不是具体的文件名 接下来的步骤就和例子1一样了,不同之处是我们新建了一个build目录来存储编译中间文件,如下图: 自动化构建系统CMake怎么用
  4. 下一步make,然后运行结果如下:
  5. 自动化构建系统CMake怎么用

例3:动态库编译(.so)

有了前两个例子的基础,接下来的例子我们只需要看一下目录结构和CMakelist.txt.

  自动化构建系统CMake怎么用 

CMakelist.txt如下:

project(directory_test)set(CMAKE_BUILD_TYPE Release)#Bring the headers, such as Student.h into the projectinclude_directories(include)#However, the file(GLOB...) allows for wildcard additions:file(GLOB SOURCES "src/*.cpp")#Generate the shared library from the sourcesadd_library(testStudent SHARED ${SOURCES})#Set the location for library installation -- i.e., /usr/lib in this case# not really necessary in this example. Use "sudo make install" to applyinstall(TARGETS testStudent DESTINATION /usr/lib)
123456789101112131415

两个重要变化:

  1. 我们不再使用add_executable() 而是使用add_library()
  2. install 指定安装目录,执行sudo make install时动态库将被安装在/usr/lib目录 如前两个例子,我们依次执行,cmake make编译结果如下:
    自动化构建系统CMake怎么用

例4:静态库编译 (.a)

基于例3,我们编译一个静态库

  自动化构建系统CMake怎么用 

将CMakeList.txt修改为如下所示:

cmake_minimum_required(VERSION 2.8.9)
project(directory_test)set(CMAKE_BUILD_TYPE Release)#Bring the headers, such as Student.h into the projectinclude_directories(include)#However, the file(GLOB...) allows for wildcard additions:file(GLOB SOURCES "src/*.cpp")#Generate the static library from the sourcesadd_library(testStudent STATIC ${SOURCES})#Set the location for library installation -- i.e., /usr/lib in this case# not really necessary in this example. Use "sudo make install" to applyinstall(TARGETS testStudent DESTINATION /usr/li
12345678910111213141516

可以看出,只需将add_library中的shared改为static即可。 编译结果如下:

  自动化构建系统CMake怎么用


例5:使用静态库或动态库

下边我们来测试一下我们例3的结果,代码和CMakeList.txt如下:

#include"Student.h"int main(int argc, char *argv[]){
  Student s("Joe");
  s.display();
  return 0;
}
1234567
cmake_minimum_required(VERSION 2.8.9)
project (TestLibrary)#For the shared library:set ( PROJECT_LINK_LIBS libtestStudent.so )
link_directories( ~/exploringBB/extras/cmake/studentlib_shared/build )#For the static library:#set ( PROJECT_LINK_LIBS libtestStudent.a )#link_directories( ~/exploringBB/extras/cmake/studentlib_static/build )include_directories(~/exploringBB/extras/cmake/studentlib_shared/include)

add_executable(libtest libtest.cpp)
target_link_libraries(libtest ${PROJECT_LINK_LIBS} )
123456789101112131415

结果如下(CMakeList.txt中的目录要根据自己的情况改一下): 自动化构建系统CMake怎么用 

成功了!!

以上是“自动化构建系统CMake怎么用”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

推荐阅读:
  1. CMake快速入门
  2. OpenCV for Mac 编译 CMake Error at cmake/OpenCVUtils.cmake:42 (if):

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

cmake

上一篇:Apache Avro数据的示例分析

下一篇:Linux如何使用正则表达式

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》