CMake入门教程:基础使用及2.8.4版本特点

版权申诉
0 下载量 114 浏览量 更新于2024-11-14 收藏 2.37MB GZ 举报
资源摘要信息:"Cmake学习_Cmake2.8.4_" CMake是一个开源的跨平台自动化构建系统。它使用一个名为CMakeLists.txt的配置文件,通过简单的文本编辑来描述项目的编译过程和相关配置。CMake广泛应用于Unix、Windows、Mac OS X等多个操作系统平台上,为各种编程语言提供构建和测试工具。 CMake 2.8.4作为其中一个较早期的稳定版本,为学习CMake提供了一个基础的平台。对于初学者来说,了解并掌握该版本能够帮助他们构建一个稳固的基础,从而更好地理解和使用后续的版本。 ### CMake基础使用说明: 1. **安装CMake**: - 下载CMake的安装包,根据操作系统的不同,选择适合的版本。 - 运行安装程序并按照指引完成安装。 - 确认安装成功,通常包括CMake的命令行工具以及GUI工具。 2. **编写CMakeLists.txt文件**: - 在项目根目录下创建一个名为CMakeLists.txt的文本文件。 - 该文件通常包含项目名、版本、编程语言、依赖关系、链接库和可执行文件的生成指令等。 3. **构建项目**: - 打开命令行工具,进入CMakeLists.txt文件所在的目录。 - 使用命令`cmake .`(点表示当前目录),此步骤会生成构建文件,例如Makefile(Unix)或Visual Studio项目文件(Windows)。 - 使用相应的构建工具或命令编译项目,例如`make`(Unix)、`nmake`(Windows)或者直接打开生成的IDE工程文件进行构建。 4. **运行程序**: - 编译无误后,可执行文件通常会在build目录下生成。 - 根据不同平台,运行编译生成的可执行文件即可。 ### 举例说明如何使用CMake: 假设有一个简单的C++项目,包含两个源文件main.cpp和hello.cpp,下面是一个简单的CMakeLists.txt文件示例: ```cmake # 声明CMake的最小版本要求 cmake_minimum_required(VERSION 2.8.4) # 设置项目名称 project(MyProject) # 设置C++标准 set(CMAKE_CXX_STANDARD 11) # 包含子目录(如果有的话) # add_subdirectory(src) # 添加可执行文件 add_executable(MyApp main.cpp hello.cpp) ``` 1. 在项目的根目录(包含CMakeLists.txt的目录)打开命令行工具。 2. 执行命令`cmake .`,这将根据CMakeLists.txt生成构建文件。 3. 构建过程会生成一个名为MyApp的可执行文件。 4. 在Unix系统下使用命令`./MyApp`运行程序,在Windows系统下直接运行MyApp.exe文件。 通过以上步骤,初学者可以构建并运行一个简单的C++项目。随着对CMake的进一步了解,可以通过变量、宏、模块等功能来编写更加复杂的构建脚本。 ### CMake高级知识点: 1. **变量**:可以用来存储值,类似于编程语言中的变量。 2. **宏和函数**:可以定义复用的代码块。 3. **模块**:CMake提供了许多模块,可以用来扩展CMake的功能。 4. **条件语句和循环**:可以使用if-else语句和foreach循环来控制构建流程。 5. **自定义命令和目标**:可以为构建过程添加自定义命令,或者创建自定义的目标类型。 通过学习CMake 2.8.4的基础使用,可以为深入研究CMake的强大功能和最新特性打下坚实的基础。随着CMake版本的更新,新特性的加入也会使得构建过程更为高效和强大。

cmake_minimum_required(VERSION 3.8.0 FATAL_ERROR) project(mcu_kit VERSION 0.1.0 DESCRIPTION "mcu-kit SDK" LANGUAGES C ) set(PROJECT_BRIEF "vDiscovery") set(CMAKE_C_STANDARD 11) set(CMAKE_C_EXTENSIONS ON) set(CMAKE_C_STANDARD_REQUIRED ON) set(CMAKE_CXX_STANDARD 11) set(CMAKE_POSITION_INDEPENDENT_CODE ON) add_compile_options(-Wall) list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake") include(CMakePrintHelpers) include(ConfigureChecks) if(EXISTS "${PROJECT_SOURCE_DIR}/config.cmake") include(config.cmake) else() message(FATAL_ERROR "Can't find cmake.config file") endif() if(${CMAKE_CROSSCOMPILING}) set(LV_SIMULATOR_ON_PC OFF) else() set(LV_SIMULATOR_ON_PC ON) endif() configure_file( "${PROJECT_SOURCE_DIR}/base/base_sdk_version.h.in" "${PROJECT_SOURCE_DIR}/base/base_sdk_version.h" ) configure_file( "${PROJECT_SOURCE_DIR}/base/base_config.h.in" "${PROJECT_SOURCE_DIR}/base/base_config.h" ) set(CMAKE_INSTALL_BINDIR ${CMAKE_INSTALL_PREFIX}/bin) set(CMAKE_INSTALL_SBINDIR ${CMAKE_INSTALL_PREFIX}/sbin) set(CMAKE_INSTALL_LIBDIR ${CMAKE_INSTALL_PREFIX}/lib) set(CMAKE_INSTALL_INCLUDEDIR ${CMAKE_INSTALL_PREFIX}/include) set(CMAKE_INSTALL_SYSCONFDIR ${CMAKE_INSTALL_PREFIX}/etc) set(CMAKE_INSTALL_SHARESTATEDIR ${CMAKE_INSTALL_PREFIX}/com) set(CMAKE_INSTALL_LOCALSTATEDIR ${CMAKE_INSTALL_PREFIX}/var) set(CMAKE_INSTALL_INFODIR ${CMAKE_INSTALL_PREFIX}/info) set(CMAKE_INSTALL_LOCALEDIR ${CMAKE_INSTALL_PREFIX}/locate) set(CMAKE_INSTALL_MANDIR ${CMAKE_INSTALL_PREFIX}/man) set(CMAKE_INSTALL_DOCDIR ${CMAKE_INSTALL_PREFIX}/doc) set(CMAKE_INSTALL_RESDIR ${CMAKE_INSTALL_PREFIX}) include(PrepareInstall) set(KIT_ROOT_DIR ${CMAKE_SOURCE_DIR}) add_subdirectory("base") # add_subdirectory("third-party") add_subdirectory("lib") add_subdirectory("drivers") add_subdirectory("framework") add_subdirectory("sysapp") add_subdirectory("app") include(GeneratePkgConfig) include(Linux-kitInfo)详细注释一下这段代码

2023-05-25 上传

# toplevel CMakeLists.txt for a catkin workspace # catkin/cmake/toplevel.cmake cmake_minimum_required(VERSION 3.0.2) project(Project) set(CATKIN_TOPLEVEL TRUE) # search for catkin within the workspace set(_cmd "catkin_find_pkg" "catkin" "${CMAKE_SOURCE_DIR}") execute_process(COMMAND ${_cmd} RESULT_VARIABLE _res OUTPUT_VARIABLE _out ERROR_VARIABLE _err OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_STRIP_TRAILING_WHITESPACE ) if(NOT _res EQUAL 0 AND NOT _res EQUAL 2) # searching fot catkin resulted in an error string(REPLACE ";" " " _cmd_str "${_cmd}") message(FATAL_ERROR "Search for 'catkin' in workspace failed (${_cmd_str}): ${_err}") endif() # include catkin from workspace or via find_package() if(_res EQUAL 0) set(catkin_EXTRAS_DIR "${CMAKE_SOURCE_DIR}/${_out}/cmake") # include all.cmake without add_subdirectory to let it operate in same scope include(${catkin_EXTRAS_DIR}/all.cmake NO_POLICY_SCOPE) add_subdirectory("${_out}") else() # use either CMAKE_PREFIX_PATH explicitly passed to CMake as a command line argument # or CMAKE_PREFIX_PATH from the environment if(NOT DEFINED CMAKE_PREFIX_PATH) if(NOT "$ENV{CMAKE_PREFIX_PATH}" STREQUAL "") if(NOT WIN32) string(REPLACE ":" ";" CMAKE_PREFIX_PATH $ENV{CMAKE_PREFIX_PATH}) else() set(CMAKE_PREFIX_PATH $ENV{CMAKE_PREFIX_PATH}) endif() endif() endif() # list of catkin workspaces set(catkin_search_path "") foreach(path ${CMAKE_PREFIX_PATH}) if(EXISTS "${path}/.catkin") list(FIND catkin_search_path ${path} _index) if(_index EQUAL -1) list(APPEND catkin_search_path ${path}) endif() endif() endforeach() # search for catkin in all workspaces set(CATKIN_TOPLEVEL_FIND_PACKAGE TRUE) find_package(catkin QUIET NO_POLICY_SCOPE PATHS ${catkin_search_path} NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH) unset(CATKIN_TOPLEVEL_FIND_PACKAGE) if(NOT catkin_FOUND) message(FATAL_ERROR "find_package(catkin) failed. catkin was neither found in the workspace nor in the CMAKE_PREFIX_PATH. One reason may be that no ROS setup.sh was sourced before.") endif() endif() catkin_workspace()在哪添加target_link_libraries(your_target_name avcodec avformat swscale)

2023-07-16 上传

解释根据给出的代码,可以将其转化为以下CMake代码: 复制 cmake_minimum_required(VERSION 3.5) project(UavRectifyLoadLIb LANGUAGES CXX) set(CMAKE_CXX_STANDARD 11) set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_AUTOMOC ON) set(CMAKE_AUTORCC ON) set(CMAKE_AUTOUIC ON) find_package(Qt5Core REQUIRED) add_executable(UavRectifyLoadLIb main.cpp ) target_link_libraries(UavRectifyLoadLIb PRIVATE Qt5::Core UAVAutoRectifyMt UAVAutoRectify UAVAutoRectifyFi DEMDriver Projection IImage_gC opencv_core opencv_highgui opencv_imgproc opencv_features2d opencv_imgcodecs ) target_include_directories(UavRectifyLoadLIb PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../../../include/gdal1101 ${CMAKE_CURRENT_SOURCE_DIR}/../include ${CMAKE_CURRENT_SOURCE_DIR}/../../../lib/opencvf249 ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../../usr/local/include ) if(UNIX AND NOT APPLE) target_link_directories(UavRectifyLoadLIb PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../../../product/release32 ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../../usr/local/lib ) endif() if(WIN32) if(CMAKE_BUILD_TYPE STREQUAL "Debug") set_target_properties(UavRectifyLoadLIb PROPERTIES RUNTIME_OUTPUT_DIRECTORY_DEBUG ${CMAKE_CURRENT_SOURCE_DIR}/../../../../RasterManager/bin/Debug ) else() set_target_properties(UavRectifyLoadLIb PROPERTIES RUNTIME_OUTPUT_DIRECTORY_RELEASE ${CMAKE_CURRENT_SOURCE_DIR}/../../../../RasterManager/bin/release ) endif() else() if(CMAKE_BUILD_TYPE STREQUAL "Debug") set_target_properties(UavRectifyLoadLIb PROPERTIES RUNTIME_OUTPUT_DIRECTORY_DEBUG ${CMAKE_CURRENT_SOURCE_DIR}/../../../product/release32 ) else() set_target_properties(UavRectifyLoadLIb PROPERTIES RUNTIME_OUTPUT_DIRECTORY_RELEASE ${CMAKE_CURRENT_SOURCE_DIR}/../../../product/release32 ) endif() endif()

2023-06-11 上传