cmake 编译、链接静态库

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

repinkply

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值