形式1:
project(<PROJECT-NAME> [<language-name>...])
形式2:
project(<PROJECT-NAME>
[VERSION <major>[.<minor>[.<patch>[.<tweak>]]]]
[DESCRIPTION <project-description-string>]
[HOMEPAGE_URL <url-string>]
[LANGUAGES <language-name>...])
该命令最主要的功能是指定camke工程的名字,此外他还可以指定版本号、描述、主页链接以及编译工程所使用的的语言
CMAKE_PROJECT_NAME
变量中project(cmaketest )
这样就指定了工程名为cmaketest,并且在调用project指定工程名字的同时,camke内部还会为下边的一些变量赋值:
- PROJECT_NAME: 将名称赋值给PROJECT_NAME,即${PROJECT_NAME} = cmaketest
- PROJECT_SOURCE_DIR: 当前工程的源码路径
- <PROJECT-NAME>_SOURCE_DIR:指定工程的源码路径。如果PROJECT_NAME就是当前工程,则与PROJECT_SOURCE_DIR相同。
- PROJECT_BINARY_DIR:当前工程的二进制路径
- <PROJECT-NAME>_BINARY_DIR: 指定工程的二进制路径。若PROJECT_NAME
- CMAKE_PROJECT_NAME:顶层工程的名称。cmake命令首次调用那个CMakeLists.txt对应工程的名字
代码:
message ("+++ PROJECT_NAME: ${PROJECT_NAME}")
message ("+++ PROJECT_SOURCE_DIR: ${PROJECT_SOURCE_DIR}")
message ("+++ <PROJECT_NAME>_SOURCE_DIR: ${${SUB_LEVEL_PROJECT_NAME}_SOURCE_DIR}")
message ("+++ <PROJECT_NAME>_SOURCE_DIR(top level): ${${TOP_PROJECT_NAME}_SOURCE_DIR}")
message ("+++ PROJECT_BINARY_DIR: ${PROJECT_BINARY_DIR}")
message ("+++ <PROJECT_NAME>_BINARY_DIR: ${${SUB_LEVEL_PROJECT_NAME}_BINARY_DIR}")
message ("+++ <PROJECT_NAME>_BINARY_DIR(top level): ${${TOP_PROJECT_NAME}_BINARY_DIR}")
message ("+++ CMAKE_PROJECT_NAME: ${CMAKE_PROJECT_NAME}")
输出结果:
+++ PROJECT_NAME: cmaketest
+++ PROJECT_SOURCE_DIR: /myfile/cmakeStudy/01-Basic/A-hello-cmake
+++ <PROJECT_NAME>_SOURCE_DIR:
+++ <PROJECT_NAME>_SOURCE_DIR(top level):
+++ PROJECT_BINARY_DIR: /myfile/cmakeStudy/01-Basic/A-hello-cmake/build1
+++ <PROJECT_NAME>_BINARY_DIR:
+++ <PROJECT_NAME>_BINARY_DIR(top level):
+++ CMAKE_PROJECT_NAME: cmaketest
- 指定版本号
project(cmaketest VERSION 10.2.1.3)
指定版本号会为以下变量赋值:
- PROJECT_VERSION: 项目版本号 - <PROJECT_NAME>_VERSION - PROJECT_VERSION_MAJOR - <PROJECT_NAME>_VERSION_MAJOR - PROJECT_VERSION_MINOR - <PROJECT_NAME>_VERSION_MINOR - PROJECT_VERSION_PATCH - <PROJECT_NAME>_VERSION_PATCH - PROJECT_VERSION_TWEAK - <PROJECT_NAME>_VERSION_TWEAK - CMAKE_PROJECT_VERSION 带PROJECT_NAME的是指定工程名下的版本号,不带PROJECT_NAME的是当前工程的版本号,CMAKE_PROJECT_VERSION存储的是顶层CMakeLists.txt中project命令指定的版本号
```cpp
示例代码:
# 设置最低Cmake版本要求
cmake_minimum_required(VERSION 3.5)
# 指定项目名
project(cmaketest VERSION 10.2.1.3)
message("CMAKE_PROJECT_NAME = ${CMAKE_PROJECT_NAME}")
message("PROJECT_VERSION = ${PROJECT_VERSION}")
message("PROJECT_VERSION_MAJOR = ${PROJECT_VERSION_MAJOR}")
message("PROJECT_VERSION_MINOR = ${PROJECT_VERSION_MINOR}")
message("PROJECT_VERSION_PATCH = ${PROJECT_VERSION_PATCH}")
message("PROJECT_VERSION_TWEAK = ${PROJECT_VERSION_TWEAK}")
add_executable(hello_cmake main.cpp)
输出结果
CMAKE_PROJECT_NAME = cmaketest
PROJECT_VERSION = 10.2.1.3
PROJECT_VERSION_MAJOR = 10
PROJECT_VERSION_MINOR = 2
PROJECT_VERSION_PATCH = 1
PROJECT_VERSION_TWEAK = 3
指定工程描述
project(cmaketest DESCRIPTION "This is a test project")
指定工程描述会为以下变量赋值:
- PROJECT_DESCRIPTION - <PROJECT-NAME>_DESCRIPTION - CMAKE_PROJECT_DESCRIPTION 带PROJECT_NAME的是指定工程名下描述,不带PROJECT_NAME 的是当前调用工程的描述,当CMakeLists.txt位于顶层目录, CMAKE_PROJECT_DESCRIPTION存储的是顶层CMakeLists.txt 中project命令指定的工程描述,不会随着调用工程的变化而变化。
```cpp
示例代码:
# 设置最低Cmake版本要求
cmake_minimum_required(VERSION 3.5)
# 指定项目名
project(cmaketest DESCRIPTION "This is a test project")
message("CMAKE_PROJECT_NAME = ${CMAKE_PROJECT_NAME}")
message("PROJECT_DESCRIPTION = ${PROJECT_DESCRIPTION}")
add_executable(hello_cmake main.cpp)
CMAKE_PROJECT_NAME = cmaketest
PROJECT_DESCRIPTION = This is a test project
-- Configuring done
-- Generating done
指定工程描述
project(cmaketest HOMEPAGE_URL "https://www.testxxx.com")
指定URL会为以下变量赋值
PROJECT_HOMEPAGE_URL <PROJECT-NAME>_HOMEPAGE_URL CMAKE_PROJECT_HOMEPAGE_URL
规则基本同上
本地测试报错,不知道为啥,气人~~~
指定工程描述
project(cmaketest LANGUAGE "CXX")
LANGUAGE选项可以有两种调用方式,一种是直接跟在工程名后边,可以省略LANGUAGE关键字,如
project (cmaketest “CXX”)
另一种是跟在其他关键字后边,LANGUAGE关键字不能省略,如project (camketest VERSION 10.2.1.3 LANGUAGES “CXX”)
LANGUAGE选择性支持的语言包括
C
、CXX
(即C++
)、CUDA
、OBJC
(即Objective-C
)、OBJCXX
、Fortran
、HIP
、ISPC
和ASM
,如果没有指定LANGUAGE选项,则默认为C和C++。
指定LANGUAGE NONE
或者仅添加LANGUAGE选项但没有列出任何语言,表示跳过所有语言。如果启用ASM,则将其放在最后,以便 CMake 可以检查其他语言的编译器如C语言是否也适用于汇编。
LANGUAGE选项的主要目的是检测工程编译需要的语言编译器是否存在,如果不存在,则会提示报错
示例代码:
# 设置最低Cmake版本要求
cmake_minimum_required(VERSION 3.5)
# 制定项目名
project(cmaketest LANGUAGE "HIP")
add_executable(hello_cmake main.cpp)
输出:
No CMAKE_HIP_COMPILER could be found
示例代码:
# 设置最低Cmake版本要求
cmake_minimum_required(VERSION 3.5)
message("ROJECT_NAME: ${PROJECT_NAME}")
add_executable(hello_cmake main.cpp)
输出结果:
ROJECT_NAME: Project
CMAKE_PROJECT_NAME、CMAKE_PROJECT_NAME、CMAKE_PROJECT_DESCRIPTION、CMAKE_PROJECT_HOMEPAGE_URL
等变量是以最后一次调用的project命令为准。project
命令需要放置在其他命令调用之前,在cmake_minimum_required
命令之后。