Skip to content

Commit daff5fe

Browse files
jwillikersLebedevRI
authored andcommitted
Alias CMake Targets. Fixes #921 (#926)
* add Jordan Williams to both CONTRIBUTORS and AUTHORS * alias benchmark libraries Provide aliased CMake targets for the benchmark and benchmark_main targets. The alias targets are namespaced under benchmark::, which is the namespace when they are exported. I chose not to use either the PROJECT_NAME or the namespace variable but to hard-code the namespace. This is because the benchmark and benchmark_main targets are hard-coded by name themselves. Hard-coding the namespace is also much cleaner and easier to read. * link to aliased benchmark targets It is safer to link against namespaced targets because of how CMake interprets the double colon. Typo's will be caught by CMake at configuration-time instead of during compile / link time. * document the provided alias targets * add "Usage with CMake" section in documentation This section covers linking against the alias/import CMake targets and including them using either find_package or add_subdirectory. * format the "Usage with CMake" README section Added a newline after the "Usage with CMake" section header. Dropped the header level of the section by one to make it a direct subsection of the "Usage" section. Wrapped lines to be no longer than 80 characters in length.
1 parent 5ce2429 commit daff5fe

File tree

5 files changed

+31
-7
lines changed

5 files changed

+31
-7
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ Ismael Jimenez Martinez <[email protected]>
3333
Jern-Kuan Leong <[email protected]>
3434
JianXiong Zhou <[email protected]>
3535
Joao Paulo Magalhaes <[email protected]>
36+
Jordan Williams <[email protected]>
3637
Jussi Knuuttila <[email protected]>
3738
Kaito Udagawa <[email protected]>
3839
Kishan Kumar <[email protected]>

CONTRIBUTORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ Jern-Kuan Leong <[email protected]>
5050
JianXiong Zhou <[email protected]>
5151
Joao Paulo Magalhaes <[email protected]>
5252
John Millikin <[email protected]>
53+
Jordan Williams <[email protected]>
5354
Jussi Knuuttila <[email protected]>
5455
5556
Kaito Udagawa <[email protected]>

README.md

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,8 @@ BENCHMARK_MAIN();
178178
```
179179
180180
To run the benchmark, compile and link against the `benchmark` library
181-
(libbenchmark.a/.so). If you followed the build steps above, this
182-
library will be under the build directory you created.
181+
(libbenchmark.a/.so). If you followed the build steps above, this library will
182+
be under the build directory you created.
183183
184184
```bash
185185
# Example on linux after running the build steps above. Assumes the
@@ -194,6 +194,26 @@ Alternatively, link against the `benchmark_main` library and remove
194194
The compiled executable will run all benchmarks by default. Pass the `--help`
195195
flag for option information or see the guide below.
196196

197+
### Usage with CMake
198+
199+
If using CMake, it is recommended to link against the project-provided
200+
`benchmark::benchmark` and `benchmark::benchmark_main` targets using
201+
`target_link_libraries`.
202+
It is possible to use ```find_package``` to import an installed version of the
203+
library.
204+
```cmake
205+
find_package(benchmark REQUIRED)
206+
```
207+
Alternatively, ```add_subdirectory``` will incorporate the library directly in
208+
to one's CMake project.
209+
```cmake
210+
add_subdirectory(benchmark)
211+
```
212+
Either way, link to the library as follows.
213+
```cmake
214+
target_link_libraries(MyTarget benchmark::benchmark)
215+
```
216+
197217
## Platform Specific Build Instructions
198218

199219
### Building with GCC

src/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ foreach(item ${BENCHMARK_MAIN})
1818
endforeach()
1919

2020
add_library(benchmark ${SOURCE_FILES})
21+
add_library(benchmark::benchmark ALIAS benchmark)
2122
set_target_properties(benchmark PROPERTIES
2223
OUTPUT_NAME "benchmark"
2324
VERSION ${GENERIC_LIB_VERSION}
@@ -55,6 +56,7 @@ endif()
5556

5657
# Benchmark main library
5758
add_library(benchmark_main "benchmark_main.cc")
59+
add_library(benchmark::benchmark_main ALIAS benchmark_main)
5860
set_target_properties(benchmark_main PROPERTIES
5961
OUTPUT_NAME "benchmark_main"
6062
VERSION ${GENERIC_LIB_VERSION}
@@ -64,7 +66,7 @@ set_target_properties(benchmark_main PROPERTIES
6466
target_include_directories(benchmark PUBLIC
6567
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/../include>
6668
)
67-
target_link_libraries(benchmark_main benchmark)
69+
target_link_libraries(benchmark_main benchmark::benchmark)
6870

6971

7072
set(generated_dir "${CMAKE_CURRENT_BINARY_DIR}/generated")

test/CMakeLists.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,17 @@ add_library(output_test_helper STATIC output_test_helper.cc output_test.h)
3838

3939
macro(compile_benchmark_test name)
4040
add_executable(${name} "${name}.cc")
41-
target_link_libraries(${name} benchmark ${CMAKE_THREAD_LIBS_INIT})
41+
target_link_libraries(${name} benchmark::benchmark ${CMAKE_THREAD_LIBS_INIT})
4242
endmacro(compile_benchmark_test)
4343

4444
macro(compile_benchmark_test_with_main name)
4545
add_executable(${name} "${name}.cc")
46-
target_link_libraries(${name} benchmark_main)
46+
target_link_libraries(${name} benchmark::benchmark_main)
4747
endmacro(compile_benchmark_test_with_main)
4848

4949
macro(compile_output_test name)
5050
add_executable(${name} "${name}.cc" output_test.h)
51-
target_link_libraries(${name} output_test_helper benchmark
51+
target_link_libraries(${name} output_test_helper benchmark::benchmark
5252
${BENCHMARK_CXX_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT})
5353
endmacro(compile_output_test)
5454

@@ -178,7 +178,7 @@ add_test(NAME complexity_benchmark COMMAND complexity_test --benchmark_min_time=
178178
if (BENCHMARK_ENABLE_GTEST_TESTS)
179179
macro(compile_gtest name)
180180
add_executable(${name} "${name}.cc")
181-
target_link_libraries(${name} benchmark
181+
target_link_libraries(${name} benchmark::benchmark
182182
gmock_main ${CMAKE_THREAD_LIBS_INIT})
183183
endmacro(compile_gtest)
184184

0 commit comments

Comments
 (0)