project command of common Cmake commands

Posted by wigz01 on Tue, 09 Nov 2021 08:49:47 +0100

2. project()

1.1 command format:

Form 1:
project(<PROJECT-NAME> [<language-name>...])
Form 2:
project(<PROJECT-NAME>
        [VERSION <major>[.<minor>[.<patch>[.<tweak>]]]]
        [DESCRIPTION <project-description-string>]
        [HOMEPAGE_URL <url-string>]
        [LANGUAGES <language-name>...])

The main function of this command is to specify the name of the camke project. In addition, it can also specify the version number, description, home page link and the language used to compile the project

1.2 parameter description

  • PROJECT_NAME: required - used to set the project name. After setting, the set value will be stored in cmake_ PROJECT_ In the name variable
  • **VERSION * *: optional, project VERSION number, including major VERSION number, minor VERSION number and patch VERSION number
  • **DESCRIPTION * *: a simple DESCRIPTION of the project
  • **HOMEPAGE_URL * *: Project homepage url
  • **LANGUAGES * *: the language used in the project. The default is C or CXX

1.3 example

  1. Specify only the project name (the most common form)
project(cmaketest )

In this way, the project name is specified as cmaketest. While calling project to specify the project name, camke will also assign values to some of the following variables:

 - PROJECT_NAME: Assign name to PROJECT_NAME,Namely ${PROJECT_NAME} = cmaketest
 - PROJECT_SOURCE_DIR: Source path of the current project
 - <PROJECT-NAME>_SOURCE_DIR: Specify the source code path of the project. If PROJECT_NAME Is the current project, and PROJECT_SOURCE_DIR Same.
 - PROJECT_BINARY_DIR: Binary path of the current project
 - <PROJECT-NAME>_BINARY_DIR: Specifies the binary path of the project. if PROJECT_NAME
 - CMAKE_PROJECT_NAME: Name of the top-level project. cmake Which command is called for the first time CMakeLists.txt Name of corresponding project
code:
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}")
Output results:
+++ 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
  1. Specify the version number
 - Specify the version number
project(cmaketest VERSION 10.2.1.3)

Specifying a version number assigns values to the following variables:

- PROJECT_VERSION: Project version number
- <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
 belt PROJECT_NAME Is the version number under the specified project name, without PROJECT_NAME Is the version number of the current project, CMAKE_PROJECT_VERSION The storage is the top level CMakeLists.txt in project The version number specified by the command
```cpp
 Example code:
# Set minimum Cmake version requirements
cmake_minimum_required(VERSION 3.5)

# Specify project name
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)
Output results
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
  1. Specify project description
Specify project description
project(cmaketest DESCRIPTION "This is a test project")

Specifying a project description assigns values to the following variables:

- PROJECT_DESCRIPTION
- <PROJECT-NAME>_DESCRIPTION
- CMAKE_PROJECT_DESCRIPTION
 belt PROJECT_NAME Is the description under the specified project name, without PROJECT_NAME  
This is the description of the current calling project. CMakeLists.txt In the top-level directory,  
CMAKE_PROJECT_DESCRIPTION The storage is the top level CMakeLists.txt  
in project The project description specified by the command will not change with the change of the calling project.
```cpp
 Example code:
# Set minimum Cmake version requirements
cmake_minimum_required(VERSION 3.5)

# Specify project name
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
  1. Specify project home page URL
Specify project description
project(cmaketest HOMEPAGE_URL "https://www.testxxx.com")

Specifying a URL assigns values to the following variables

PROJECT_HOMEPAGE_URL
<PROJECT-NAME>_HOMEPAGE_URL
CMAKE_PROJECT_HOMEPAGE_URL

The rules are basically the same as above

The local test reports an error. I don't know why. It's annoying~~~

  1. Specify the programming language required to build the project
Specify project description
project(cmaketest LANGUAGE "CXX")

The LANGUAGE option can be called in two ways: one is directly after the project name, and the LANGUAGE keyword can be omitted, such as project (cmaketest "CXX"), the other is after other keywords, and the LANGUAGE keyword cannot be omitted, such as project (camketest VERSION 10.2.1.3 LANGUAGES "CXX")

Languages selectively supported by LANGUAGE include C, cxx (i.e. C + +), CUDA, objc (i.e. Objective-C), OBJCXX, Fortran, HIP, ISPC and ASM. If the LANGUAGE option is not specified, it defaults to C and C + +.
Specifying LANGUAGE NONE or adding only the LANGUAGE option without listing any languages means skipping all languages. If ASM is enabled, put it last so that CMake can check whether compilers in other languages, such as C, are also suitable for assembly.

LANGUAGE The main purpose of this option is to check whether the language compiler required for project compilation exists. If it does not exist, an error will be prompted
 Example code:
# Set minimum Cmake version requirements
cmake_minimum_required(VERSION 3.5)
# Develop project name
project(cmaketest LANGUAGE "HIP")
add_executable(hello_cmake main.cpp)
Output:
No CMAKE_HIP_COMPILER could be found

1.4 precautions

  1. The project command is not required. If there is no project command, cmake will automatically generate a project named project.
Example code:
# Set minimum Cmake version requirements
cmake_minimum_required(VERSION 3.5)

message("ROJECT_NAME: ${PROJECT_NAME}")
add_executable(hello_cmake main.cpp)
Output results:
ROJECT_NAME: Project
  1. If the project command is called multiple times, CMAKE_PROJECT_NAME,CMAKE_ PROJECT_ NAME,CMAKE_PROJECT_DESCRIPTION,CMAKE_PROJECT_HOMEPAGE_URL and other variables are subject to the last project command called.
  2. The project command needs to be placed before other commands are called, in cmake_ minimum_ After the required command.

Topics: C++ cmake compiler