之前项目中使用的spdlog 库,但是一直使用的是herder only 模式,简单是简单,但是编译的速度实在是太慢了,所以最近就在探索使用动态库的方法加速编译的速度。
仓库的地址 :
https://gitee.com/jeasonb/spdlog_dyn
使用方法
1. 动态库的编译方法
root@jeason:~/spdlog/dynamic# cmake --version
cmake version 3.10.2
CMake suite maintained and supported by Kitware (kitware.com/cmake).
root@jeason:~/spdlog/dynamic# mkdir build
root@jeason:~/spdlog/dynamic# cd build/
##### 下面使用的是 动态库的方法
root@jeason:~/spdlog/dynamic/build# cmake -DDYN=1 ..
in dyn mode
-- Configuring done
-- Generating done
-- Build files have been written to: /root/spdlog/dynamic/build
root@jeason:~/spdlog/dynamic/build# make -j4
Scanning dependencies of target spdlog
[ 10%] Building CXX object CMakeFiles/spdlog.dir/spdlog/src/file_sinks.cpp.o
[ 20%] Building CXX object CMakeFiles/spdlog.dir/spdlog/src/async.cpp.o
[ 30%] Building CXX object CMakeFiles/spdlog.dir/spdlog/src/cfg.cpp.o
[ 40%] Building CXX object CMakeFiles/spdlog.dir/spdlog/src/color_sinks.cpp.o
[ 50%] Building CXX object CMakeFiles/spdlog.dir/spdlog/src/fmt.cpp.o
[ 60%] Building CXX object CMakeFiles/spdlog.dir/spdlog/src/spdlog.cpp.o
[ 70%] Building CXX object CMakeFiles/spdlog.dir/spdlog/src/stdout_sinks.cpp.o
[ 80%] Linking CXX shared library libspdlog.so
[ 80%] Built target spdlog
Scanning dependencies of target app
[ 90%] Building CXX object CMakeFiles/app.dir/main.cpp.o
[100%] Linking CXX executable app
[100%] Built target app
root@jeason:~/spdlog/dynamic/build# touch ../main.cpp ## 这里 touch 以下 重新编译
root@jeason:~/spdlog/dynamic/build# make
[ 80%] Built target spdlog
Scanning dependencies of target app
[ 90%] Building CXX object CMakeFiles/app.dir/main.cpp.o
[100%] Linking CXX executable app
[100%] Built target app
root@jeason:~/spdlog/dynamic/build#
读者可以自行体会编译时的速度,可以发现 除了第一次编译的比较慢之外,后续对main 修改完之后再编译都会变得很快。
2. 使用头文件进行编译
root@jeason:~/spdlog/dynamic/build# cmake -DDYN=0 ..
in header mode
-- Configuring done
-- Generating done
-- Build files have been written to: /root/spdlog/dynamic/build
root@jeason:~/spdlog/dynamic/build# make -j4
Scanning dependencies of target app
[ 50%] Building CXX object CMakeFiles/app.dir/main.cpp.o
[100%] Linking CXX executable app
[100%] Built target app
root@jeason:~/spdlog/dynamic/build# ./app
[2022-02-20 16:59:39.544] [info] 12323123
[2022-02-20 16:59:39.544] [info] 12323123
[2022-02-20 16:59:39.544] [info] 12323123
root@jeason:~/spdlog/dynamic/build# touch ../main.cpp
root@jeason:~/spdlog/dynamic/build# make
Scanning dependencies of target app
[ 50%] Building CXX object CMakeFiles/app.dir/main.cpp.o
[100%] Linking CXX executable app
[100%] Built target app
root@jeason:~/spdlog/dynamic/build#
在只使用头文件的模式下,编译器本质上是将所有的头文件进行展开 然后重新编译的,所以 即使是只编译一个很小的文件,编译的也是非常慢。
简单说就是 稍微改一下main.cpp 都需要编译很久!
具体的编译效果建议读者自行下载代码体会!