Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Append to target link libraries

Tags:

cmake

Currently, in my CMake file, I do the following based on whether OpenMP is found or not:

if(OpenMP_CXX_FOUND)
    target_link_libraries(myapp PRIVATE lib_1 lib_2 OpenMP::OpenMP_CXX PUBLIC io_lib)
else(OpenMP_CXX_FOUND)
    target_link_libraries(myapp PRIVATE lib_1 lib_2 PUBLIC io_lib)

Perhaps, I am being picky here but this looks a bit ugly. I am wondering if there is a canonical way to do this which does not result in an ugly config. Perhaps, something like appending to the list of libs might be cleaner, but I could not figure out how to access the PRIVATE interface.

like image 816
Luca Avatar asked Oct 27 '25 14:10

Luca


1 Answers

A couple variations:

Instead, you can just conditionally append the OpenMP::OpenMP_CXX target:

target_link_libraries(myapp PRIVATE lib_1 lib_2 PUBLIC io_lib)
if(OpenMP_CXX_FOUND)
    target_link_libraries(myapp PRIVATE OpenMP::OpenMP_CXX)
endif()

as consecutive calls to target_link_libraries for the same target will append items in the order called.

Or, you can cut this logic down to a one-liner, using a conditional generator expression:

target_link_libraries(myapp 
    PRIVATE lib_1 lib_2 $<IF:${OpenMP_CXX_FOUND},OpenMP::OpenMP_CXX,> 
    PUBLIC io_lib
)
like image 82
Kevin Avatar answered Oct 30 '25 11:10

Kevin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!