Linux下CMake链接googletest【CMake编写】

源码下载

git clone https://github.com/google/benchmark.git
git clone https://github.com/google/googletest.git benchmark/googletest

这两个源代码分别编译 并执行

sudo make install

头文件被安装在

/usr/local/include

库文件被安装在

/usr/local/lib

CMake文件链接文件简介

通过find_package查找

添加代码如下:

find_package(Ceres REQUIRED)
include_directories(${Ceres_INCLUDE_DIRS})
target_link_libraries(Test ${Ceres_LIBRARIES})

以Ceres库为例,find_package通过findceres.cmake文件查找ceres的头文件位置和.a文件(库文件)位置,在find.cmake文件中包含了Ceres_INCLUDE_DIRS和Ceres_LIBRARIES的位置。

通过link_directories直接设置库文件和头文件的地址

以本例为例

include_directories("/home/usrname/benchmark/googletest/googletest/include/gtest")
link_directories("/usr/local/lib")
target_link_libraries(run libbenchmark.a)
include_directories指定了googletest的头文件的搜索路径
link_directories指定了googletest的.a文件的搜索路径
target_link_libraries将.a文件和可执行文件名链接

查找.a文件的位置:

/usr/lib或/usr/local/lib中 使用whereis命令查找指定的.a文件

也可以尝试用whereis搜索.cmake文件的路径,我想不起来在哪个路径了。

总结

使用find_package命令查找不到文件时是因为没有对应的findXXX.cmake文件。这时候需要使用link_directories命令直接设置库文件和头文件的地址。

添加pthread链接

我们需要的是pthread的库,不需要lpthread的库(lpthread会有一些问题,导致编译不过)
在cmake中添加pthread而不是lpthread:

set_target_properties(${TARGET} PROPERTIES
COMPILE_FLAGS "-pthread"
LINK_FLAGS "-pthread")

成果展示:

2022-01-25T14:36:29+08:00
Running ./run
Run on (16 X 4800 MHz CPU s)
CPU Caches:
  L1 Data 32 KiB (x8)
  L1 Instruction 32 KiB (x8)
  L2 Unified 256 KiB (x8)
  L3 Unified 16384 KiB (x1)
Load Average: 0.61, 0.59, 0.54
***WARNING*** CPU scaling is enabled, the benchmark real time measurements may be noisy and will incur extra overhead.
***WARNING*** Library was built as DEBUG. Timings may be affected.
---------------------------------------------------------------
Benchmark                     Time             CPU   Iterations
---------------------------------------------------------------
bench_array_operator        161 ns          160 ns      4085540
bench_array_at              160 ns          159 ns      4395393
bench_array_get             160 ns          160 ns      4384017

googletest原理详解链接。
使用的也是上面的例子。
CMake链接第三方库三种方法
CMake文件链接pthread更好的方法

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

铃灵狗

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

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

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

打赏作者

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

抵扣说明:

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

余额充值