CMake初学笔记(一)

news/2024/11/19 20:35:20/

CMake初学笔记(一)

  • CMake是什么
  • CMake怎么实现跨平台
  • CMake具体实践过程
  • CMakeLists.txt编写快速入门
    • 常见函数
    • 例子

CMake是什么

跨平台编译工具,为了实现“write once, run everywhere”。

CMake怎么实现跨平台

  1. 开发者编写与平台无关的编译过程文件,即编写CMakeLists.txt。
  2. 最终根据目标用户的平台,根据CMakeLists.txt生成本地化的Makefile和工程文件。

CMake具体实践过程

  1. 编写CMake配置文件CMakeLists.txt
  2. 在CMakeLists.txt所在目录下创建build文件夹,mkdir build,即build和CMakeLists.txt在同一文件目录 下。
  3. 执行cmake :
    cmake PATH。其中PATH是CMakeLists.txt所在目录,例如,当前目录就是CMakeLists.txt所在目录,则可输入命令:cmake .。cmake具体用法可参照下图:
    在这里插入图片描述
  4. make -j4,其中-j是指定CPU核心数,-j4表明分配4个CPU核心来进行编译。
  5. 生成的可执行文件在build/devel/lib/<project_name>下。

CMakeLists.txt编写快速入门

常见函数

  1. message()

例子

  1. OpenGL:Open Graphics Library的缩写,跨语言 、跨平台的图形程序接口,提供2D、3D图形渲染接口,操作在GPU之上,实现硬件加速渲染。
  2. QGLViewer:基于Qt的开源C++库,为了OpenGL的3D开发更加容易。
  3. PCL:Point Cloud Library
## Copyright (C) 2020  I. Bogoslavskyi, C. Stachniss
##
## Permission is hereby granted, free of charge, to any person obtaining a
## copy of this software and associated documentation files (the "Software"),
## to deal in the Software without restriction, including without limitation
## the rights to use, copy, modify, merge, publish, distribute, sublicense,
## and/or sell copies of the Software, and to permit persons to whom the
## Software is furnished to do so, subject to the following conditions:
##
## The above copyright notice and this permission notice shall be included in
## all copies or substantial portions of the Software.
##
## THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
## IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
## FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
## AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
## LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
## FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
## DEALINGS IN THE SOFTWARE.cmake_minimum_required(VERSION 3.1)  # 对cmake的最低版本要求,== CMAKE_MINIMUM_REQUIRED(VERSION 3.1)
project(depth_clustering)  # 设置项目名称,== set(PROJECT_NAME depth_clustering)include(DistVersion.cmake)
system_info(DISTRO)
message(STATUS "DISTRO: ${DISTRO}")set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)if(NOT CMAKE_BUILD_TYPE)set(CMAKE_BUILD_TYPE Release)
endif()set(CMAKE_CXX_FLAGS "-Wall -Wextra -fPIC")
set(CMAKE_CXX_FLAGS_DEBUG "-g -O0")
set(CMAKE_CXX_FLAGS_RELEASE "-O3")cmake_policy(SET CMP0045 OLD)SET(CMAKE_MODULE_PATH${CMAKE_MODULE_PATH}"${PROJECT_SOURCE_DIR}/cmake_modules""/usr/share/cmake-2.8/Modules"  ## Hack for travis)set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/lib)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/bin)include(CTest)
IF(IS_DIRECTORY "/usr/src/gtest/")MESSAGE(STATUS "Found google test sources in /usr/src/gtest/")ADD_SUBDIRECTORY(/usr/src/gtest/ gtest)# mimick the behaviour of find_package(GTest)SET(GTEST_FOUND TRUE)SET(GTEST_BOTH_LIBRARIES gtest gtest_main)SET(GTEST_LIBRARIES gtest)SET(GTEST_MAIN_LIBRARIES gtest_main)
ELSE()find_package(GTest)
ENDIF()set(Boost_USE_STATIC_LIBS OFF)option(BUILD_WITH_COVERAGE "Enable compilation with coverage information." OFF)
if(BUILD_WITH_COVERAGE)message(STATUS "Enabled generation of code coverage information with gcc.")set(CMAKE_CXX_FLAGS "-fprofile-arcs -ftest-coverage ${CMAKE_CXX_FLAGS}")
endif()find_package(Threads REQUIRED)
find_package(Eigen3 REQUIRED)
find_package(OpenCV REQUIRED)
find_package(OpenGL REQUIRED)
find_package(Boost REQUIRED COMPONENTS system filesystem regex program_options)
find_package(PCL QUIET COMPONENTS common io)
find_package(QGLViewer REQUIRED)# find correct qt version
if(${DISTRO} MATCHES "14.04")find_package(Qt4 REQUIRED COMPONENTS QtCore QtXml QtOpenGL QtGui QtMultimedia)include(${QT_USE_FILE})include_directories(${QT_INCLUDES})set(MY_QT_LIBRARIESQt4::QtCoreQt4::QtXmlQt4::QtOpenGLQt4::QtGui)
elseif(${DISTRO} MATCHES "16.04" OR ${DISTRO} MATCHES "18.04")find_package(Qt5 REQUIRED COMPONENTS Core Xml OpenGL Gui Widgets)include_directories(${Qt5Core_INCLUDE_DIRS}${Qt5Xml_INCLUDE_DIRS}${Qt5Gui_INCLUDE_DIRS}${Qt5Widgets_INCLUDE_DIRS}${Qt5OpenGL_INCLUDE_DIRS})set(MY_QT_LIBRARIES${Qt5Widgets_LIBRARIES}${Qt5Core_LIBRARIES}${Qt5Gui_LIBRARIES}${Qt5Xml_LIBRARIES}${Qt5OpenGL_LIBRARIES})
endif()# some status messages
message(STATUS "Linking against Qt libs: ${MY_QT_LIBRARIES}")
message(STATUS "Linking against QGlViewer lib: ${QGLVIEWER_LIBRARY}")## Find catkin macros and libraries
## if COMPONENTS list like find_package(catkin REQUIRED COMPONENTS xyz)
## is used, also find other catkin packages
find_package(catkin COMPONENTSroscppsensor_msgsstd_msgsnav_msgsmessage_filterstfeigen_conversions
)# add some useful constants to configuration
configure_file(config/cmake_config.h.in${PROJECT_SOURCE_DIR}/config/cmake_config.h)
include_directories(config)if (${PCL_FOUND})message(STATUS "PCL is found. PCL-related code will be built.")if (${CMAKE_BUILD_TYPE} MATCHES "Debug" AND ${DISTRO} MATCHES "14.04")message(FATAL_ERROR"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n""You are building PCL in Debug mode with C++11 enabled.\n""This causes segmentation fault due to a bug in PCL.\n""Build in Release mode to avoid this bug.\n""!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")endif()add_definitions(-DPCL_FOUND)
else()message(WARNING "PCL NOT FOUND! PCL-related code WILL NOT be built")
endif()if(${roscpp_FOUND})message(STATUS "ROS found, building ROS related parts")set(ROS_FOUND YES)
else()message(WARNING "ROS NOT FOUND. NOT building ROS related parts!")set(ROS_FOUND NO)
endif()catkin_package(INCLUDE_DIRS srcLIBRARIEScloudidentifiableground_removedifferenceimage_labelerprojectionsros_bridgevelodyne_utilsfolder_readervisualizationCATKIN_DEPENDS roscpp sensor_msgs std_msgs nav_msgs message_filters tf
)# 加载头文件
include_directories(src# for QT moc files${PROJECT_BINARY_DIR}/src# dependencies we have no control overSYSTEM ${EIGEN3_INCLUDE_DIR}SYSTEM ${Boost_INCLUDE_DIRS}SYSTEM ${PCL_INCLUDE_DIRS}SYSTEM ${OpenCV_INCLUDE_DIRS}SYSTEM ${OpenGL_INCLUDE_DIRS}SYSTEM ${QGLVIEWER_INCLUDE_DIR}SYSTEM ${catkin_INCLUDE_DIRS}
)add_subdirectory(src)
add_subdirectory(examples)message(STATUS "BUILD_TESTING: ${BUILD_TESTING}")
if (${BUILD_TESTING})add_subdirectory(test)
endif()

未完待续


http://www.ppmy.cn/news/89151.html

相关文章

【小沐学NLP】Python实现聊天机器人(OpenAI,模型概述笔记)

&#x1f37a;NLP开发系列相关文章编写如下&#x1f37a;&#xff1a;1&#x1f388;【小沐学NLP】Python实现词云图&#x1f388;2&#x1f388;【小沐学NLP】Python实现图片文字识别&#x1f388;3&#x1f388;【小沐学NLP】Python实现中文、英文分词&#x1f388;4&#x1…

Seata AT模式源码解析三(AT模式工作机制)

文章目录 代码示例流程源码解析开启全局事务注册分支事务一阶段提交全局事务提交分支事务二阶段提交全局事务回滚分支事务二阶段回滚 代码示例 从一个微服务示例开始&#xff0c;案例采用Seata官方提供的Demo。 用户购买商品的业务逻辑。整个业务逻辑由3个微服务提供支持&…

Bits, Bytes and Integers——二进制unsigned以及Two-complement表示,十六进制

这篇文章梳理一下Bits, Bytes and Integers——二进制unsigned以及Two-complement表示&#xff0c;十六进制这些事儿。 计算机中所有数据都是用二进制的0和1组成的&#xff0c;直接上知识点。 二进制 Unsigned以及Two-complement 同样的一串二进制数&#xff0c;按照有符号…

0基础学习VR全景平台篇第31章:场景功能-嵌入图片

大家好&#xff0c;欢迎收看蛙色平台免费教程&#xff01; 功能位置示意 一、本功能将用在哪里&#xff1f; 嵌入功能可对VR全景作品嵌入【图片】【视频】【文字】【标尺】四种不同类型内容&#xff0c;本次主要带来图片类型的介绍&#xff0c;满足场景营销、重点标注、幻灯片…

docker移动默认地址

停止 Docker 服务&#xff1a;首先&#xff0c;停止正在运行的 Docker 服务。在 Linux 系统上&#xff0c;可以使用以下命令停止 Docker 服务&#xff1a; sudo systemctl stop docker 在 Windows 上&#xff0c;可以通过 Docker Desktop 的系统托盘图标或者任务管理器停止 Do…

HTTP/HTTPS协议详解

目录 一. HTTP详解 ✅1.1 概念 ✅1.2 HTTP的协议格式 1.2.1 HTTP请求体格式&#xff1a; 1.2.2 HTTP响应体格式&#xff1a; ✅1.3 HTTP请求方法 ✅1.4 认识请求报头 ✅1.5 HTTP请求过程 ✅1.6 认识状态码 二. HTTPS详解 ✅2.1 HTTPS简介 ✅2.2 HTTPS加密过程 TCP/UDP是位于传…

redis未授权访问漏洞利用+redis日志分析

redis未授权访问漏洞利用redis日志分析 redis未授权访问 远程连接redis kali: redis-cli -h IP redis常用语句 set key “value” 设置键值对 get key 获得值 incr intkey 数字类型值加1 keys * 获取当前数据库中所有的键 config set dir /home/test 设置工作目录 config set…

Linux-进程信号

Linux-进程信号 一&#xff0c;信号入门信号的概念生活中的信号技术应用角度的信号使用kill -l 查看信号列表信号的处理方式 二&#xff0c;信号产生通过终端按键产生信号Core Dumpcode dump标志位 通过系统调用向进程发信号killraiseabort 由软件条件产生信号SIGPIPESIGALRM 硬…