安装编译工具和依赖库
sudo apt install g++ gcc make ninja-build unzip libssl-dev -y
wget
下载和编译cmake
源码
wget https://github.com/Kitware/CMake/releases/download/v3.24.2/cmake-3.24.2.tar.gz
tar -xvf cmake-3.24.2.tar.gz
cd cmake-3.24.2
./configure # 生成makefile
make -j32
sudo make install
默认安装位置:/usr/local/bin/cmake
前置准备
- first_cmake.cpp
- CMakeLists.txt
#include <iostream>
using namespace std;
int main()
{
cout << "hello cmake" << endl;
return 0;
}
cmake_minimum_required(VERSION 3.20)
project(first_cmake VERSION 1.0)
add_executable(first_cmake 101first_cmake.cpp)
Linux平台编译
cd 01first_cmake
cmake -S . -B build
cmake --build ./build
# 执行生成的文件
./build/first_cmake
- -S source_dir
- -B build_dir
- --build build_dir
使用Ninja
cmake -S . -B ninja -G "Ninja"
cmake --build ninja
查看帮助
多线程编译
cmake --build ./build -j32
动态库和静态库(xlog)
静态库
- 类似于多个
.o
的合并,直接将代码编译到项目中,有些开源库不可以使用静态链接(需要静态授权)
- 如果两个静态库引用了同一个子库,项目中使用这两个库时会产生冲突,如果使用动态库就会发生这种情况
- 链接的时间比较长
window平台
xlog.lib -- release
xlib_d.lib -- debug
Linux平台
动态库
windows
xlog.lib + xlog.dll --> lib 是索引文件,需要手动导出
linux
头文件
- 函数名称和参数类型(索引查找函数地址),c++和c的查找方式不一样。
-
windows
中库要导出,不然不会生成.lib文件,调用的时候要导入。
源码准备
# test_xlog / CMakeLists.txt
cmake_minimum_required(VERSION 3.20)
project(xlog_test)
include_directories("../xlog")
# link_directories("../xlog/build")
# link_libraries(xlog)
add_executable(xlog_test main.cpp)
target_link_directories(xlog_test PUBLIC "../xlog/build")
target_link_libraries(xlog_test PUBLIC xlog)
- include 和 link 要在 add_executable之前
- target_xxx 与 xxx 的区别, target_xx 只是作用于某个目标,而 xx 是作用于所有目标,一个CMakes可以有多个目标
# xlog/CMakeLists.txt
cmake_minimum_required(VERSION 3.20)
project(xlog)
add_library(xlog STATIC xlog.cpp xlog.h)
编译动态
cmake_minimum_required(VERSION 3.20)
project(xlog)
add_library(xlog SHARED xlog.cpp xlog.h)
同时编译多个目标
cmake_minimum_required(VERSION 3.20)
project(xlogp)
include_directories(xlog)
add_library(xlog SHARED xlog/xlog.cpp xlog/xlog.h)
add_executable(xlog_test test_xlog/main.cpp xlog)
target_link_libraries(xlog_test xlog)
- 必须使用 target_link_libraries
- 因为在同一个项目中,所以不需要指定link_directories
ldd xlog_test
linux-vdso.so.1 (0x00007ffee81f9000)
libxlog.so => /home/luan/cmake/study/code/103cmake_lib/./build/libxlog.so (0x00007fb2a61f9000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fb2a5ffe000)
libstdc++.so.6 => /lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007fb2a5e1c000)
/lib64/ld-linux-x86-64.so.2 (0x00007fb2a6205000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007fb2a5ccd000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007fb2a5cb2000)
使用子目录
# 104cmake_lib
cmake_minimum_required(VERSION 3.20)
project(xlog_test)
add_subdirectory(xlog)
add_executable(xlog_test test_xlog/main.cpp xlog)
target_link_libraries(xlog_test xlog)
# xlog
cmake_minimum_required(VERSION 3.20)
project(xlog)
add_library(xlog SHARED xlog.cpp xlog.h)
target_include_directories(xlog INTERFACE ${CMAKE_CURRENT_SOURCE_DIR})
动态链接库与静态链接库开关
cmake_minimum_required(VERSION 3.20)
project(xlog)
set(BUILD_SHARED_LIBS ON)
add_library(xlog xlog.cpp xlog.h)
target_include_directories(xlog INTERFACE ${CMAKE_CURRENT_SOURCE_DIR})