编译 {#编译}
-
库github仓库路径为:https://github.com/gabime/spdlog ,clone源码并解压到本地,如
D:/spdlog
,目录下应包含CMakeLists.txt
文件。 -
在目录下新建文件夹build,在命令行进入build目录
|-------------|------------------------------| |
1 2
|mkdir build cd build
| -
cmake -B . -S ../ -DSPDLOG_BUILD_EXAMPLES=ON -G "Visual Studio 16 2022" -A x64
,指定编译器为VS2022 x64,并且编译示例|-----------|----------------------------------------------------------------------------------------| |
1
|cmake -B . -S ../ -DSPDLOG_BUILD_EXAMPLES=ON -G "Visual Studio 16 2022" -A x64
| -
cmake --build . --config release and cmake --build . --config debug
,编译结果为静态库|-----------|-----------------------------------------------------------------------------| |
1
|cmake --build . --config release and cmake --build . --config debug
| -
将编辑成果和头文件手动拷贝一下,比如到D:/dev/spdlog下,目录结构为
|---------------|------------------------------------------------------------------| |
1 2 3
|./spdlog/include ./spdlog/lib/debug ./spdlog/lib/release
| -
大功告成
使用 {#使用}
CMakeLists.txt {#CMakeLists-txt}
注意其中链接库的写法,关键字optimized指除debug 以外的所有config方式。 在debug下链接spdlogd.lib,在其他模式下链接spdlog.lib。好用。
|---------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| cmake_minimum_required(VERSION 3.20) project(spdlod_demo) set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) set(LibPath_SpdLog e:/dev/spdlog) #set(CMAKE_PREFIX_PATH ${LibPath_SpdLog}) add_executable(${PROJECT_NAME} "./src/main.cpp") # 链接 spdlog 库 target_link_libraries(${PROJECT_NAME} # debug:链接 spdlogd.lib PUBLIC debug ${LibPath_SpdLog}/lib/debug/spdlogd.lib # 其他 config:链接 spdlog.lib PUBLIC optimized ${LibPath_SpdLog}/lib/release/spdlog.lib ) target_include_directories(${PROJECT_NAME} PUBLIC ${LibPath_SpdLog}/include )
|
示例代码 {#示例代码}
|-------------------|----------------------------------------------------------------------------------------------------------------------------------------------------|
| 1 2 3 4 5
| #include "spdlog/spdlog.h" int main() { // Use the default logger (stdout, multi-threaded, colored) spdlog::info("Hello, {}!", "World"); }
|
参考链接:https://zhuanlan.zhihu.com/p/690761574