cmake简单案例

# cmake 最低版本需求
cmake_minimum_required(VERSION 3.13)

# 工程名称
project(cmake_study LANGUAGES CXX)

# 设置
set(CMAKE_CXX_STANDARD 11)

# cmake subdirectory
# add_subdirectory(lib)

# cmake build library
# add_library(fun1 SHARED lib/fun1.cpp)
# target_include_directories(fun1 PUBLIC inc)


# 编译源码生成目标
add_executable(main src/main.cpp)

target_include_directories(main PUBLIC inc)
target_link_libraries(
	main
	PUBLIC 
	/mnt/d/linux_codes/cpp/lib/libfun1.so)

添加头文件和库依赖

# third party include and library
include_directories(
    /mnt/d/linux_codes/cpp/inc
)
link_directories(
    /mnt/d/linux_codes/cpp/lib
)
target_link_libraries(main PUBLIC libfun1.so)

 

 

example 2

# cmake 最低版本需求
cmake_minimum_required(VERSION 2.8)

# export CC=/usr/bin/gcc
# export CXX=/usr/bin/g++

set(CMAKE_CXX_COMPILER "/usr/local/bin/g++")
set(CMAKE_C_COMPILER "/usr/local/bin/gcc")

 
# 工程名称
project(main LANGUAGES CXX)

add_compile_options(-std=c++11)


# 编译源码生成目标
add_executable(main main.cpp)
 
target_include_directories(main PUBLIC inc)
target_link_libraries(
	main
	PUBLIC
	/usr/local/xx/libruntime.so)

 

指定生成先后顺序

add_dependencies(main target1 target2)

添加子目录

add_subdirectory(dir_name)

生成动态库

add_library(lib1 SHARED lib1.cpp)
set_property(TARGET lib1 PROPERTY POSITION_INDEPENDENT_CODE ON)

 

从源码安装cmake

./configure
make
make install

 

参考

https://cliutils.gitlab.io/modern-cmake/modern-cmake.pdf

https://aiden-dong.github.io/2019/07/20/CMake%E6%95%99%E7%A8%8B%E4%B9%8BCMake%E4%BB%8E%E5%85%A5%E9%97%A8%E5%88%B0%E5%BA%94%E7%94%A8/

https://cmake.org/cmake/help/latest/guide/tutorial/index.html

你可能感兴趣的:(cmake简单案例)