Halcyon
Halcyon

Reputation: 1429

CMake Conditional Library Linking Depending on the Platform

I am linking Boost into my application using CMake:

I have this code that works on Linux in my CMakeLists.txt: TARGET_LINK_LIBRARIES(app -lboost_system -lboost_thread -lcrypt)

But on OpenBSD -lboost_thread is -lboost_thread-mt instead, and -lcrypt is not required. Is there a way of writing the CMakeLists.txt in such a way that it handles both of these cases depending on which library exists?

Upvotes: 1

Views: 768

Answers (1)

The need to handle such platform specific differences is why one should use find modules instead. Specifically FindBoost in this case. After find_package is executed, you should be able to pull in the thread library by its canonical target name Boost::thread.

Any required PUBLIC libraries or headers will be part of the the canonical target's link interface, and so will be added automatically, without further effort on your part. All you'll need to do is add the canonical target as a link dependency

target_link_libraries(app Boost::thread)

Upvotes: 3

Related Questions