cmake:将库添加到所有目标 [英] cmake: add library to all targets

查看:208
本文介绍了cmake:将库添加到所有目标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我可行的CMakelists.txt。基准库位于/usr/local/lib/libbenchmark.a中,它是通过 google基准中的标准过程构建的

Here is my workable CMakelists.txt. The benchmark library is in /usr/local/lib/libbenchmark.a and it is built with the standard process in google benchmark

cmake_minimum_required(VERSION 3.5.1)
project(tiny_benchmark)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED on)
set(CMAKE_CXX_EXTENSIONS off)
set(CMAKE_EXPORT_COMPILE_COMMANDS on)
# set(CMAKE_STANDARD_LIBRARIES ${CMAKE_STANDARD_LIBRARIES}  pthread benchmark /usr/local/lib/benchmark.a)
# set(CMAKE_STANDARD_LIBRARIES ${CMAKE_STANDARD_LIBRARIES}  pthread)
add_executable(foo foo.cpp)
add_executable(bar bar.cpp)
target_link_libraries(foo pthread benchmark)
target_link_libraries(bar pthread benchmark)

由于我有很多小型基准工具需要构建,所以我不想为每个库调用 target_link_libraries(bar pthread基准)

Since I have many small benchmark tool to build so I don't want call target_link_libraries(bar pthread benchmark) for every library.

我想只需定义可执行文件即可。并且可以自动添加pthread和基准。

I want to just define the executable. and the pthread and benchmark can be added automatically.

我发现CMAKE_STANDARD_LIBRARIES是为此目的而设计的,但是我尝试了多种格式的
CMAKE_STANDARD_LIBRARIES和CMAKE_CXX_STANDARD_LIBRARIES,但没有人使用。

I found CMAKE_STANDARD_LIBRARIES is designed for this purpose, but I tried CMAKE_STANDARD_LIBRARIES and CMAKE_CXX_STANDARD_LIBRARIES in many format, but no one works.

在这种情况下我犯了什么错误?我的cmake版本是3.5.1。

what error did I make in this case? my cmake version is 3.5.1.

谢谢

推荐答案

您可以使用 link_libraries 命令。它的效果类似于 target_link_libraries ,但会应用于调用后创建的所有目标。

You may use link_libraries command. Its effect similar to target_link_libraries, but is applied to all targets created after the call.

link_libraries(pthread benchmark)

add_executable(foo foo.cpp)
add_executable(bar bar.cpp)

这篇关于cmake:将库添加到所有目标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆