CMake的使用

学习资料:

https://github.com/ttroy50/cmake-examples

怎样使用:

在CMakeLists.txt的目录下创建build文件夹,在build 路径 执行cmake .. (.. 表示上级目录), 然后执行make, 生成的中间文件 和 target文件都在build路径下。

 

怎样定义target:可执行文件、静态库和动态库

add_library(hello_library STATIC /SHARED (默认是static)
    src/Hello.cpp
)

add_executable(hello_binary
    src/main.cpp
)

怎样包含源文件

modern cmake的写法是如上所示,直接在add_xxx(library/ executable) 函数中添加,并没有单独的函数取添加源文件,其中源文件的路径以 CMakeLists.txt 作为默认根路径

怎样包含头文件的搜索路径

说的头文件的搜索路径,并没有包含具体的头文件,因为头文件是通过源文件起作用的。

target_include_directories(hello_library
    PUBLIC                                                                                                                                                                                                  
        ${PROJECT_SOURCE_DIR}/include
)

public/ private/ interface说的 当链接该库时, 如果该库使用的是public 则,不需要再写target_include_directories,虽然引用库里使用了被链接库的头文件,这就是它的意义,其实并不神秘。

This will cause the included directory used in the following places:

* When compiling the library,
* When compiling any additional target that links the library.

The meaning of scopes are:

* +PRIVATE+ - the directory is added to this target's include directories
* +INTERFACE+ - the directory is added to the include directories for any targets that link this library.
* +PUBLIC+ - As above, it is included in this library and also any targets that link this library.

怎样链接库

target_link_libraries( hello_binary
    PRIVATE 
        hello_library
)

这里的private说的也是头文件的包含问题,没有其他的意思。

 

一个完整的CMakeLists.txt

cmake_minimum_required(VERSION 3.5)

project(hello_library)

############################################################
# Create a library
############################################################

#Generate the static library from the library sources
add_library(hello_library STATIC
    src/Hello.cpp
)

target_include_directories(hello_library
    PUBLIC
        ${PROJECT_SOURCE_DIR}/include
)


############################################################
# Create an executable
############################################################

# Add an executable with the above sources
add_library(hello_binary
    src/main.cpp
)

# link the new hello_library target with the hello_binary target
target_link_libraries( hello_binary
    PRIVATE
        hello_library

怎样设置工具链:xxx.toolchain.cmake

set(CMAKE_C_COMPILER "${HEXAGON_TOOLS}/Tools/bin/hexagon-clang")
set(CMAKE_CXX_COMPILER "${HEXAGON_TOOLS}/Tools/bin/hexagon-clang")
set(CMAKE_AR "${HEXAGON_TOOLS}/Tools/bin/hexagon-ar" CACHE FILEPATH "Archiver")
set(CMAKE_LINKER "${HEXAGON_TOOLS}/Tools/bin/hexagon-link")

set(CMAKE_FIND_ROOT_PATH "${HEXAGON_TOOLS}")
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)

怎样控制编译选项

add_compile_options(
    -Wall
    -Wpointer-arith
    -Wno-cast-align
    -Wstrict-prototypes
    -Wnested-externs
    -mv66
    -Uqdsp6                                                                                                                                    
    -Uq6sim
    -Uqdsp6r0
    -O2 
    -g  
    -fdata-sections
    -ffunction-sections
    -nostdlib
    -fno-exceptions
    -fno-strict-aliasing
    -G0 
    -Werror
    -Wno-error=deprecated-declarations
)

怎样控制宏定义

add_definitions("-D_HAS_C9X")

add_definitions(-DFIXED_POINT=16)

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值