CMake中project的使用

Posted fengbingchun

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CMake中project的使用相关的知识,希望对你有一定的参考价值。

      CMake中的命令project用于设置项目(project)的名称。其格式如下

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

      设置project的名称,并将其存储在变量PROJECT_NAME中。从顶层(top-level)CMakeLists.txt调用时,还将project名称存储在变量CMAKE_PROJECT_NAME中。
      还设置变量:
      (1).PROJECT_SOURCE_DIR, <PROJECT-NAME>_SOURCE_DIR: project源目录的绝对路径。
      (2).PROJECT_BINARY_DIR, <PROJECT-NAME>_BINARY_DIR: project二进制目录的绝对路径。
      (3).PROJECT_IS_TOP_LEVEL, <PROJECT-NAME>_IS_TOP_LEVEL: 3.21版本中引入,布尔值,指示project是否为top-level。

project(test_cmake)
message("project name: $PROJECT_NAME") # project name: test_cmake
message("cmake project name: $CMAKE_PROJECT_NAME") # cmake project name: test_cmake

message("project source dir: $PROJECT_SOURCE_DIR") # project source dir: /home/spring/GitHub/Linux_Code_Test/Samples_CMake/messy_usage
message("project name source dir: $$PROJECT_NAME_SOURCE_DIR") # project name source dir: /home/spring/GitHub/Linux_Code_Test/Samples_CMake/messy_usage

message("project binary dir: $PROJECT_BINARY_DIR") # project binary dir: /home/spring/GitHub/Linux_Code_Test/Samples_CMake/messy_usage/build
message("project name binary dir: $$PROJECT_NAME_BINARY_DIR") # project name binary dir: /home/spring/GitHub/Linux_Code_Test/Samples_CMake/messy_usage/build

message("project is top level: $PROJECT_IS_TOP_LEVEL") # project is top level: ON
message("project name is top level: $$PROJECT_NAME_IS_TOP_LEVEL") # project name is top level: ON

      更多的变量由下面描述的可选参数来设置,如果没有使用这些可选参数中的任何一个,则将相应的变量设置为空字符串
      (1).VERSION <version>:除非policy CMP0048设置为NEW,否则不能使用。采用由非负整数组成的<version>参数,即<major>[.<minor>[.<patch>[.<tweak>]]],并设置变量:

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

      3.12版本中引入,从顶层(top-level)CMakeLists.txt调用project命令时,版本也存储在变量CMAKE_PROJECT_VERSION中。

cmake_policy(GET CMP0048 var) # 3.0
message("var: $var") # var: NEW

project(test_cmake VERSION 1.2.3.4)
message("project version: $PROJECT_VERSION, $$PROJECT_NAME_VERSION") # project version: 1.2.3.4, 1.2.3.4
message("project version major: $PROJECT_VERSION_MAJOR, $$PROJECT_NAME_VERSION_MAJOR") # project version major: 1, 1
message("project version minor: $PROJECT_VERSION_MINOR, $$PROJECT_NAME_VERSION_MINOR") # project version minor: 2, 2
message("project version patch: $PROJECT_VERSION_PATCH, $$PROJECT_NAME_VERSION_PATCH") # project version patch: 3, 3
message("project version tweak: $PROJECT_VERSION_TWEAK, $$PROJECT_NAME_VERSION_TWEAK") # project version tweak: 4, 4
message("cmake project version: $CMAKE_PROJECT_VERSION") # cmake project version: 1.2.3.4

      (2).DESCRIPTION <project-description-string>:3.9版本中引入,设置变量PROJECT_DESCRIPTION, <PROJECT-NAME>_DESCRIPTION为<project-description-string>。建议:此描述是比较短的字符串,通常不超过几个单词。
      从顶层(top-level)CMakeLists.txt调用project命令时,描述也存储在变量CMAKE_PROJECT_DESCRIPTION中。在3.12版本中,添加了<PROJECT-NAME>_DESCRIPTION变量。

project(test_cmake DESCRIPTION "this is cmake test")
message("project description: $PROJECT_DESCRIPTION, $$PROJECT_NAME_DESCRIPTION") # project description: this is cmake test, this is cmake test
message("cmake project description: $CMAKE_PROJECT_DESCRIPTION") # cmake project description: this is cmake test
message("project name description: $$PROJECT_NAME_DESCRIPTION") # project name description: this is cmake test

      (3).HOMEPAGE_URL <url-string>:3.12版本中引入,设置变量PROJECT_HOMEPAGE_URL, <PROJECT-NAME>_HOMEPAGE_URL为<url-string>,它应该是project的规范主页URL。从顶层(top-level)CMakeLists.txt调用project命令时,URL也存储在变量CMAKE_PROJECT_HOMEPAGE_URL中。

project(test_cmake HOMEPAGE_URL "https://blog.csdn.net/fengbingchun/category_783053.html")
message("project homepage url: $PROJECT_HOMEPAGE_URL, $$PROJECT_NAME_HOMEPAGE_URL") # project homepage url: https://blog.csdn.net/fengbingchun/category_783053.html, https://blog.csdn.net/fengbingchun/category_783053.html
message("cmake project homepage url: $CMAKE_PROJECT_HOMEPAGE_URL") # cmake project homepage url: https://blog.csdn.net/fengbingchun/category_783053.html

      (4).LANGUAGES <language-name>...:也可以在没有LANGUAGES关键字的情况下指定第一个短签名(short signature)。选择构建(build)project所需的编程语言。支持的编程语言包括:C, CXX(i.e. C++), CUDA, OBJC(i.e. Objective-C), OBJCXX, Fortran, HIP, ISPC,和ASM。如果没有给出编程语言选项,默认情况下启用C和CXX。指定编程语言为NONE,或使用LANGUAGES关键字但不列出任何语言,则会跳过启用任何语言。

project(test_cmake)
project(test_cmake CXX)
project(test_cmake LANGUAGES)
project(test_cmake LANGUAGES NONE)
project(test_cmake LANGUAGES CXX ASM)

      3.8版本中添加对CUDA支持。3.16版本中添加对OBJC和OBJCXX支持。3.18版本中添加对ISPC支持。如果启用ASM,请将其列在最后,以便CMake可以检查其它编程语言(例如C)的编译器是否也适用于汇编(assembly)。

project(test_cmake1 VERSION 2.3.4.5 DESCRIPTION "test cmake1" HOMEPAGE_URL "https://blog.csdn.net/fengbingchun" LANGUAGES CXX C ASM)
message("cmake project name: $CMAKE_PROJECT_NAME") # cmake project name: test_cmake1
message("cmake project version: $CMAKE_PROJECT_VERSION") # cmake project version: 2.3.4.5
message("cmake project description: $CMAKE_PROJECT_DESCRIPTION") # cmake project description: test cmake1
message("cmake project homepage url: $CMAKE_PROJECT_HOMEPAGE_URL") # cmake project homepage url: https://blog.csdn.net/fengbingchun

project(test_cmake2 VERSION 6.7.8.9 DESCRIPTION "test cmake2" HOMEPAGE_URL "https://github.com/fengbingchun" LANGUAGES CXX)
message("cmake project name: $CMAKE_PROJECT_NAME") # cmake project name: test_cmake2
message("cmake project version: $CMAKE_PROJECT_VERSION") # cmake project version: 6.7.8.9
message("cmake project description: $CMAKE_PROJECT_DESCRIPTION") # cmake project description: test cmake2
message("cmake project homepage url: $CMAKE_PROJECT_HOMEPAGE_URL") # cmake project homepage url: https://github.com/fengbingchun

      执行上述测试代码需要3个文件:build.sh, CMakeLists.txt, test_project.cmake

     build.sh内容如下:

#! /bin/bash

# supported input parameters(cmake commands)
params=(function macro cmake_parse_arguments \\
		find_library find_path find_file find_program find_package \\
		cmake_policy cmake_minimum_required project include \\
		string)

usage()

	echo "Error: $0 needs to have an input parameter"

	echo "supported input parameters:"
	for param in $params[@]; do
		echo "  $0 $param"
	done

	exit -1


if [ $# != 1 ]; then
	usage
fi

flag=0
for param in $params[@]; do
	if [ $1 == $param ]; then
		flag=1
		break
	fi
done

if [ $flag == 0 ]; then
	echo "Error: parameter \\"$1\\" is not supported"
	usage
	exit -1
fi

if [[ ! -d "build" ]]; then
	mkdir build
	cd build
else
	cd build
fi

echo "==== test $1 ===="
cmake -DTEST_CMAKE_FEATURE=$1 ..

      CMakeLists.txt内容如下:

cmake_minimum_required(VERSION 3.22)
project(cmake_feature_usage)

message("#### current cmake version: $CMAKE_MAJOR_VERSION.$CMAKE_MINOR_VERSION.$CMAKE_PATCH_VERSION")
include(test_$TEST_CMAKE_FEATURE.cmake)
message("==== test finish ====")

      test_project.cmake:为上面的所有示例代码

      执行可能的结果如下图所示:

      GitHubhttps://github.com/fengbingchun/Linux_Code_Test

以上是关于CMake中project的使用的主要内容,如果未能解决你的问题,请参考以下文章

cmake 构建工程

使用 CMake 生成 Visual Studio 2015 Makefile Project (GDB)

CMake PROJECT_SOURCE_DIR变量(使用project()命令指定项目名称时,CMakeLists.txt所在路径)PROJECT_BINARY_DIR变量,根目录下build目录

cmake --- cmake基础

CMake Project in Visual Studio 2019

CMake Project in Visual Studio 2019