学习资料:
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)