在102cmake_lib目录下新建一个CMakeLists.txt
wj@wj:~/cmake/cmake_learn/102cmake_lib$ ls
CMakeLists.txt test_xlog xlog
首先新建一个CMakeLists.txt
#CMakeLists.txt test_xlog xlog
cmake_minimum_required(VERSION 3.20)
project(xlog)
add_library(xlog SHARED xlog/xlog.cpp)
add_executable(test_xlog test_xlog/test_xlog.cpp xlog)
wj@wj:~/cmake/cmake_learn/102cmake_lib$ cmake -S . -B build
-- The C compiler identification is GNU 9.4.0
-- The CXX compiler identification is GNU 9.4.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/wj/cmake/cmake_learn/102cmake_lib/build
wj@wj:~/cmake/cmake_learn/102cmake_lib$ ls
build CMakeLists.txt test_xlog xlog
编译的时候报错。
wj@wj:~/cmake/cmake_learn/102cmake_lib$ cmake --build build
[ 25%] Building CXX object CMakeFiles/xlog.dir/xlog/xlog.cpp.o
[ 50%] Linking CXX shared library libxlog.so
[ 50%] Built target xlog
[ 75%] Building CXX object CMakeFiles/test_xlog.dir/test_xlog/test_xlog.cpp.o
/home/wj/cmake/cmake_learn/102cmake_lib/test_xlog/test_xlog.cpp:2:10: fatal error: xlog.h: 没有那个文件或目录
2 | #include "xlog.h"
| ^~~~~~~~
compilation terminated.
make[2]: *** [CMakeFiles/test_xlog.dir/build.make:76:CMakeFiles/test_xlog.dir/test_xlog/test_xlog.cpp.o] 错误 1
make[1]: *** [CMakeFiles/Makefile2:111:CMakeFiles/test_xlog.dir/all] 错误 2
make: *** [Makefile:91:all] 错误 2
修改CMakeLists.txt
#CMakeLists.txt test_xlog xlog
cmake_minimum_required(VERSION 3.20)
project(xlog)
include_directories("xlog") #增加头文件路径
add_library(xlog SHARED xlog/xlog.cpp)
add_executable(test_xlog test_xlog/test_xlog.cpp xlog)
继续编译运行:
wj@wj:~/cmake/cmake_learn/102cmake_lib$ cmake -S . -B build
-- Configuring done
-- Generating done
-- Build files have been written to: /home/wj/cmake/cmake_learn/102cmake_lib/build
wj@wj:~/cmake/cmake_learn/102cmake_lib$ cmake --build build
Consolidate compiler generated dependencies of target xlog
[ 50%] Built target xlog
Consolidate compiler generated dependencies of target test_xlog
[ 75%] Linking CXX executable test_xlog
/usr/bin/ld: CMakeFiles/test_xlog.dir/test_xlog/test_xlog.cpp.o: in function `main':
test_xlog.cpp:(.text+0x2a): undefined reference to `XLog::XLog()'
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/test_xlog.dir/build.make:97:test_xlog] 错误 1
make[1]: *** [CMakeFiles/Makefile2:111:CMakeFiles/test_xlog.dir/all] 错误 2
make: *** [Makefile:91:all] 错误 2
继续修改CMakeLists.txt 成为下面这样。
#CMakeLists.txt test_xlog xlog
cmake_minimum_required(VERSION 3.20)
project(xlog)
#include_directories("xlog") #增加头文件路径
add_library(xlog SHARED xlog/xlog.cpp)
#add_executable(test_xlog test_xlog/test_xlog.cpp xlog)
编译运行:可以看到libxlog.so动态库已经生成了。
wj@wj:~/cmake/cmake_learn/102cmake_lib$ cmake -S . -B build
-- Configuring done
-- Generating done
-- Build files have been written to: /home/wj/cmake/cmake_learn/102cmake_lib/build
wj@wj:~/cmake/cmake_learn/102cmake_lib$ cmake --build build
Consolidate compiler generated dependencies of target xlog
[ 50%] Building CXX object CMakeFiles/xlog.dir/xlog/xlog.cpp.o
[100%] Linking CXX shared library libxlog.so
[100%] Built target xlog
wj@wj:~/cmake/cmake_learn/102cmake_lib$ cd build/
wj@wj:~/cmake/cmake_learn/102cmake_lib/build$ ls
CMakeCache.txt CMakeFiles cmake_install.cmake libxlog.a libxlog.so Makefile
继续修改CMakeLists.txt 成为下面这样。
#CMakeLists.txt test_xlog xlog
cmake_minimum_required(VERSION 3.20)
project(xlog)
#include_directories("xlog") #增加头文件路径
add_library(xlog SHARED xlog/xlog.cpp)
#add_executable(test_xlog test_xlog/test_xlog.cpp xlog)
add_executable(test_xlog test_xlog/test_xlog.cpp)
编译如下:编译报错,xlog.h头文件报错
wj@wj:~/cmake/cmake_learn/102cmake_lib$ cmake -S . -B build
-- Configuring done
-- Generating done
-- Build files have been written to: /home/wj/cmake/cmake_learn/102cmake_lib/build
wj@wj:~/cmake/cmake_learn/102cmake_lib$ cmake --build build
Consolidate compiler generated dependencies of target xlog
[ 50%] Built target xlog
Consolidate compiler generated dependencies of target test_xlog
[ 75%] Building CXX object CMakeFiles/test_xlog.dir/test_xlog/test_xlog.cpp.o
/home/wj/cmake/cmake_learn/102cmake_lib/test_xlog/test_xlog.cpp:2:10: fatal error: xlog.h: 没有那个文件或目录
2 | #include "xlog.h"
| ^~~~~~~~
compilation terminated.
make[2]: *** [CMakeFiles/test_xlog.dir/build.make:76:CMakeFiles/test_xlog.dir/test_xlog/test_xlog.cpp.o] 错误 1
make[1]: *** [CMakeFiles/Makefile2:111:CMakeFiles/test_xlog.dir/all] 错误 2
make: *** [Makefile:91:all] 错误 2
修改CMakeLists.txt ,增加头文件路径,修复编译报错:
#CMakeLists.txt test_xlog xlog
cmake_minimum_required(VERSION 3.20)
project(xlog)
include_directories("xlog") #增加头文件路径
add_library(xlog SHARED xlog/xlog.cpp)
#add_executable(test_xlog test_xlog/test_xlog.cpp xlog)
add_executable(test_xlog test_xlog/test_xlog.cpp)
再次编译如下,可以看到编译没有问题,但是链接出现问题了:
wj@wj:~/cmake/cmake_learn/102cmake_lib$ cmake -S . -B build
-- Configuring done
-- Generating done
-- Build files have been written to: /home/wj/cmake/cmake_learn/102cmake_lib/build
wj@wj:~/cmake/cmake_learn/102cmake_lib$ cmake --build build
Consolidate compiler generated dependencies of target xlog
[ 25%] Building CXX object CMakeFiles/xlog.dir/xlog/xlog.cpp.o
[ 50%] Linking CXX shared library libxlog.so
[ 50%] Built target xlog
Consolidate compiler generated dependencies of target test_xlog
[ 75%] Building CXX object CMakeFiles/test_xlog.dir/test_xlog/test_xlog.cpp.o
[100%] Linking CXX executable test_xlog
/usr/bin/ld: CMakeFiles/test_xlog.dir/test_xlog/test_xlog.cpp.o: in function `main':
test_xlog.cpp:(.text+0x2a): undefined reference to `XLog::XLog()'
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/test_xlog.dir/build.make:97:test_xlog] 错误 1
make[1]: *** [CMakeFiles/Makefile2:111:CMakeFiles/test_xlog.dir/all] 错误 2
make: *** [Makefile:91:all] 错误 2
修改链接错误,增加链接库路径:
#CMakeLists.txt test_xlog xlog
cmake_minimum_required(VERSION 3.20)
project(xlog)
include_directories("xlog") #增加头文件路径
add_library(xlog SHARED xlog/xlog.cpp)
#add_executable(test_xlog test_xlog/test_xlog.cpp xlog)
add_executable(test_xlog test_xlog/test_xlog.cpp)
target_link_libraries(test_xlog xlog)
编译运行如下:
wj@wj:~/cmake/cmake_learn/102cmake_lib$ cmake -S . -B build
-- Configuring done
-- Generating done
-- Build files have been written to: /home/wj/cmake/cmake_learn/102cmake_lib/build
wj@wj:~/cmake/cmake_learn/102cmake_lib$ cmake --build build
Consolidate compiler generated dependencies of target xlog
[ 50%] Built target xlog
Consolidate compiler generated dependencies of target test_xlog
[ 75%] Linking CXX executable test_xlog
[100%] Built target test_xlog
wj@wj:~/cmake/cmake_learn/102cmake_lib$ ls
build CMakeLists.txt test_xlog xlog
wj@wj:~/cmake/cmake_learn/102cmake_lib$ cd build/
wj@wj:~/cmake/cmake_learn/102cmake_lib/build$ ls
CMakeCache.txt CMakeFiles cmake_install.cmake libxlog.a libxlog.so Makefile test_xlog
wj@wj:~/cmake/cmake_learn/102cmake_lib/build$ ./test_xlog
Create XLog
test xlog
查看test_xlog依赖的动态库:
wj@wj:~/cmake/cmake_learn/102cmake_lib/build$ ldd test_xlog
linux-vdso.so.1 (0x00007ffecb376000)
libxlog.so => /home/wj/cmake/cmake_learn/102cmake_lib/build/libxlog.so (0x00007ff84ee17000)
libstdc++.so.6 => /lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007ff84ec21000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007ff84ea2f000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007ff84e8e0000)
/lib64/ld-linux-x86-64.so.2 (0x00007ff84ee23000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007ff84e8c5000)
将test_xlog移动到 ~目录下,然后执行,如下所示:
wj@wj:~/cmake/cmake_learn/102cmake_lib/build$ cp test_xlog ~/
wj@wj:~/cmake/cmake_learn/102cmake_lib/build$ cd ~
wj@wj:~$ ls
公共的 模板 视频 图片 文档 下载 音乐 桌面 bin cmake snap SSD_1T test_xlog WORK
wj@wj:~$ ./test_xlog
Create XLog
test xlog
wj@wj:~$ ldd test_xlog
linux-vdso.so.1 (0x00007fff3ad13000)
libxlog.so => /home/wj/cmake/cmake_learn/102cmake_lib/build/libxlog.so (0x00007ff00d62a000)
libstdc++.so.6 => /lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007ff00d434000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007ff00d242000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007ff00d0f3000)
/lib64/ld-linux-x86-64.so.2 (0x00007ff00d636000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007ff00d0d8000)