Cmake Language

本文档介绍了CMake的基础知识及如何使用CMakeLists.txt文件来组织项目。详细讲解了CMake的基本语法,如条件判断、循环等控制流程指令,以及如何创建可执行文件和库文件。此外还介绍了目标配置、依赖管理和自定义构建任务等方面的内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Cmake language

Cmake documentation:https://cmake.org/cmake/help/latest/manual/cmake-commands.7.html

Control flow:

if**() / elseif() / else() / endif()

foreach() / endforeach()

while() / endwhile()

break() / continue() / **return()

Comments starts with #

such as:

# I am a comment

Creating a CMake project

Cmake entry point:CMakeLists.txt

  • 1、put it at your peoject’s root
  • 2、check the CMake version and detects the compiler
  • 3、Recurse in sub-folders of each executable or library

the example of CMakeList.txt

cmake_minimun_required(VERSION x.y)
project(<name>)

option(SECRET_FEATYRE "Enable secert feature" OFF)

add_subdirectory(mylib)
add_subdirectory(myexy)

Executable

add_executable(<name>
    [WIN32] [MACOSX_BUNDLE]
    [source1] [source2 ...])

Library

add_library(<name>
    [WIN32] [MACOSX_BUNDLE]
    [source1] [source2 ...])

Library types:

  • SHARED:Shared library(.dll, .so, .dylib)
  • STATIC:Static Library(.lib, .a)
  • INTERFACE
    • Virtual target, usually used for header only libraries
    • Doesn’t show up in IDEs(no target or files)

Custom atrget

add_custom_target(<name>
    [COMMAND command] [args] 
    [DEPENDS depends] 
    [WORKING_DIRECTORY dir])
For both executable and libraries
target_include_directories(<target>
    [PUBLIC | INTERFACE | PRIVATE] 
    <DIR>)
target_compile_definition(<target>
    [PUBLIC | INTERFACE | PRIVATE] 
    <name> = <value>)
target_compile_options(<target>
    [PUBLIC | INTERFACE | PRIVATE] 
    <option>)
target_compile_definitions(mylib
  	PRIVATE FASTPATH =1)
target_compile_option(mylib
   	PRIVATE -Wall)
target_include_directories(<name>
    [PUBLIC] [INTERFACE][PRIVATE] 
    <DIR>
Adding a dependency on an another target
target_link_libraries(<target>
    [PUBLIC | INTERFACE | PRIVATE] 
    <lib>)

Example

target_link_library(myexe 
		PRIVATE mylib)

Three types of dependencies:

  • PRIVATE :Options are not propagated to users of the library, but users to build the current target.
  • INTERFACE :Options are propagated to users of the library, but not used to build the current target.
  • PUBLIC :Options are propagated to users of the library and used to build the current target.

Example
https://github.com/Wlain/Cmake-Language/tree/master

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值