Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cmake: add library to all targets

Tags:

cmake

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)

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

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

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.

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

Thanks

like image 946
zhihuifan Avatar asked May 11 '18 12:05

zhihuifan


1 Answers

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)
like image 60
Tsyvarev Avatar answered Nov 15 '22 08:11

Tsyvarev