cmake_minimum_required()和project()使用的先后顺序有要求吗
时间: 2024-03-16 11:42:45 浏览: 201
在`CMakeLists.txt`文件中,`cmake_minimum_required()`和`project()`的使用顺序是有要求的。
首先,`cmake_minimum_required()`必须放在`CMakeLists.txt`文件的开头,用于指定所需的CMake最低版本。这是因为如果CMake的版本过低,可能会导致项目无法构建,而且这个命令只能设置一次。
其次,`project()`命令必须紧随其后,用于设置项目的名称、版本、描述等信息。`project()`命令用于定义构建项目,包括编译器选项、链接选项和其他构建系统选项等。如果在`project()`命令之前使用这些选项,它们将被忽略。
例如,以下是`CMakeLists.txt`文件的正确顺序:
```
cmake_minimum_required(VERSION 3.10)
project(MyProject VERSION 1.0 DESCRIPTION "My project description")
# 添加源代码目录
add_subdirectory(src)
```
总之,`cmake_minimum_required()`和`project()`的使用顺序是有要求的,必须按照上述顺序使用。
相关问题
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)详细注释一下这段代码
这段代码是一个 CMakeLists.txt 文件,用于构建一个名为 "mcu_kit" 的项目。下面是对每行代码的详细注释:
```cmake
cmake_minimum_required(VERSION 3.8.0 FATAL_ERROR)
```
指定最低的 CMake 版本,如果当前版本不符合要求,则提示错误。
```cmake
project(mcu_kit VERSION 0.1.0 DESCRIPTION "mcu-kit SDK" LANGUAGES C)
```
定义项目名称、版本、描述和使用的编程语言。
```cmake
set(PROJECT_BRIEF "vDiscovery")
```
设置一个变量,用于描述项目简介。
```cmake
set(CMAKE_C_STANDARD 11)
set(CMAKE_C_EXTENSIONS ON)
set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_CXX_STANDARD 11)
```
指定使用的 C 和 C++ 标准版本。
```cmake
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
```
启用位置独立代码(PIC)选项。
```cmake
add_compile_options(-Wall)
```
添加编译选项,这里添加了 -Wall 选项,表示开启所有警告。
```cmake
list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake")
```
将自定义的 CMake 模块路径添加到 CMAKE_MODULE_PATH 变量中。
```cmake
include(CMakePrintHelpers)
include(ConfigureChecks)
```
包含两个自定义的 CMake 模块,用于输出调试信息和配置检查。
```cmake
if(EXISTS "${PROJECT_SOURCE_DIR}/config.cmake")
include(config.cmake)
else()
message(FATAL_ERROR "Can't find cmake.config file")
endif()
```
如果存在 config.cmake 文件,则包含它,否则输出错误信息并停止构建。
```cmake
if(${CMAKE_CROSSCOMPILING})
set(LV_SIMULATOR_ON_PC OFF)
else()
set(LV_SIMULATOR_ON_PC ON)
endif()
```
根据交叉编译标志设置变量 LV_SIMULATOR_ON_PC。如果正在交叉编译,则设置为 OFF;否则设置为 ON。
```cmake
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"
)
```
配置文件模板,用于生成编译时需要的头文件。
```cmake
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})
```
设置安装目录的各个子目录。
```cmake
include(PrepareInstall)
```
包含一个自定义的 CMake 模块,用于准备安装目录。
```cmake
set(KIT_ROOT_DIR ${CMAKE_SOURCE_DIR})
```
设置变量 KIT_ROOT_DIR 的值为源代码目录。
```cmake
add_subdirectory("base")
add_subdirectory("lib")
add_subdirectory("drivers")
add_subdirectory("framework")
add_subdirectory("sysapp")
add_subdirectory("app")
```
添加子目录,用于构建项目的各个模块。每个子目录都包含了相应的 CMakeLists.txt 文件。
cmake_minimum_required(VERSION 3.4.1) project(jpg2Mosaic) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall") include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include) add_library(libjpg2Mosaic SHARED src/jpg2Mosaic.cpp) target_link_libraries(libjpg2Mosaic libcutils libEGL libGLESv2 libui libgui libutils libjpeg libprotobuf libTSDesensitizationAlgorithm libecarx_engine) target_include_directories(libjpg2Mosaic PUBLIC include) set_target_properties(libjpg2Mosaic PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/lib)有错误吗
这段代码本身并没有错误,但是要根据具体的情况来判断是否存在错误。
首先,这段代码使用了 CMake 构建系统,用于生成一个名为 libjpg2Mosaic 的共享库。它包含了一个名为 jpg2Mosaic.cpp 的源文件,并链接了一些系统库和第三方库,如 libcutils、libEGL、libGLESv2、libui、libgui、libutils、libjpeg、libprotobuf、libTSDesensitizationAlgorithm 和 libecarx_engine。
如果这段代码编译时没有任何错误,那么编译结果就会生成一个 libjpg2Mosaic.so 的共享库,放置在当前目录下的 lib 目录中。
如果你需要使用这个共享库,你需要在你的项目中添加以下语句:
```
include_directories(<path-to-jpg2Mosaic-include-dir>)
link_directories(<path-to-jpg2Mosaic-lib-dir>)
target_link_libraries(<your-target> libjpg2Mosaic)
```
其中,`<path-to-jpg2Mosaic-include-dir>` 是 jpg2Mosaic 库的头文件路径,`<path-to-jpg2Mosaic-lib-dir>` 是 jpg2Mosaic 库的链接库路径,`<your-target>` 是你的项目名称。
阅读全文