cmake编译静态库
准备三个文件CMakeLists.txt xlog.cpp xlog.h,内容分别如下所示:
//xlog.h
#ifndef XLOG_H
#define XLOG_H
class XLog
{
public:
XLog();
};
#endif
//xlog.cpp
#include "xlog.h"
#include <iostream>
using namespace std;
XLog::XLog()
{
cout<<"Create XLog"<<endl;
}
#CMakeLists.txt
cmake_minimum_required(VERSION 3.20) //指定cmake最低版本要求
project(xlog) //添加项目
add_library(xlog STATIC xlog.h xlog.cpp) //在项目中编译生成静态库
编译生成build文件夹结果如下:
wj@ubuntu:~/xiacaojun/cmake_learning/102cmake_lib/xlog$ ls
CMakeLists.txt xlog.cpp xlog.h
wj@ubuntu:~/xiacaojun/cmake_learning/102cmake_lib/xlog$ 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/xiacaojun/cmake_learning/102cmake_lib/xlog/build
下面才是真正编译成可执行文件。
wj@ubuntu:~/xiacaojun/cmake_learning/102cmake_lib/xlog$ cmake --build build
[ 50%] Building CXX object CMakeFiles/xlog.dir/xlog.cpp.o
[100%] Linking CXX static library libxlog.a
[100%] Built target xlog
wj@ubuntu:~/xiacaojun/cmake_learning/102cmake_lib/xlog/build$ ls
CMakeCache.txt CMakeFiles cmake_install.cmake libxlog.a Makefile
可以看到build目录下,已经有libxlog.a 生成静态库了。
cmake 链接静态库
先新建一个test_xlog.cpp,和一个 CMakeLists.txt
wj@wj:~/cmake/cmake_learn/102cmake_lib/test_xlog$ ls
CMakeLists.txt test_xlog.cpp
//test_xlog.cpp
#include <iostream>
#include "xlog.h"
using namespace std;
int main(int argc,char* argv[])
{
XLog log;
cout<<"test xlog"<<endl;
return 0;
}
#CMakeLists.txt
cmake_minimum_required(VERSION 3.20)
project(test_xlog)
add_executable(test_xlog test_xlog.cpp)
这一步没有什么问题,因为不涉及到具体的编译。
wj@wj:~/cmake/cmake_learn/102cmake_lib/test_xlog$ 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/test_xlog/build
报错:fatal error: xlog.h: 没有那个文件或目录
wj@wj:~/cmake/cmake_learn/102cmake_lib/test_xlog$ cmake --build build
[ 50%] Building CXX object CMakeFiles/test_xlog.dir/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.cpp.o] 错误 1
make[1]: *** [CMakeFiles/Makefile2:83:CMakeFiles/test_xlog.dir/all] 错误 2
make: *** [Makefile:91:all] 错误 2
修改CMakeLists.txt
#CMakeLists.txt
cmake_minimum_required(VERSION 3.20)
project(test_xlog)
include_directories("../xlog")
add_executable(test_xlog test_xlog.cpp)
编译还是报错:
wj@wj:~/cmake/cmake_learn/102cmake_lib/test_xlog$ cmake -S . -B build
-- Configuring done
-- Generating done
-- Build files have been written to: /home/wj/cmake/cmake_learn/102cmake_lib/test_xlog/build
wj@wj:~/cmake/cmake_learn/102cmake_lib/test_xlog$ cmake --build build
[ 50%] Building CXX object CMakeFiles/test_xlog.dir/test_xlog.cpp.o
[100%] Linking CXX executable test_xlog
/usr/bin/ld: CMakeFiles/test_xlog.dir/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:83:CMakeFiles/test_xlog.dir/all] 错误 2
make: *** [Makefile:91:all] 错误 2
修改CMakeLists.txt,在CMakeLists.txt中修改 指定库查找路径,并且修改指定加载的库
#CMakeLists.txt
cmake_minimum_required(VERSION 3.20)
project(test_xlog)
#指定头文件查找路径
include_directories("../xlog")
#指定库查找路径
link_directories("../xlog/build")
add_executable(test_xlog test_xlog.cpp)
#指定加载的库
target_link_libraries(test_xlog xlog)
可以看到编译成功了:
wj@wj:~/cmake/cmake_learn/102cmake_lib/test_xlog$ cmake -S . -B build
-- Configuring done
-- Generating done
-- Build files have been written to: /home/wj/cmake/cmake_learn/102cmake_lib/test_xlog/build
wj@wj:~/cmake/cmake_learn/102cmake_lib/test_xlog$ cmake --build build
Consolidate compiler generated dependencies of target test_xlog
[ 50%] Linking CXX executable test_xlog
[100%] Built target test_xlog
进入build运行可执行程序,可以看到,打印如期的结果出来:
wj@wj:~/cmake/cmake_learn/102cmake_lib/test_xlog$ ls
build CMakeLists.txt test_xlog.cpp
wj@wj:~/cmake/cmake_learn/102cmake_lib/test_xlog$ cd build/
wj@wj:~/cmake/cmake_learn/102cmake_lib/test_xlog/build$ ls
CMakeCache.txt CMakeFiles cmake_install.cmake Makefile test_xlog
wj@wj:~/cmake/cmake_learn/102cmake_lib/test_xlog/build$ ./test_xlog
Create XLog
test xlog