Reputation: 783
I have a library with the same name as a system library. Most of the executables in my project link to my own library but one executable needs to link to the system library. CMake is generating a g++ command line linking to ../foo/libfoo.a and I need it to link to -lfoo instead.
I have a structure something like this:
/CMakeLists.txt
add_directory(foo)
add_directory(program)
/foo/CMakeLists.txt
add_library(foo foo.cpp)
/program/CMakeLists.txt
add_executable(program program.cpp)
target_link_libraries(program foo)
One solution would be to change the name of my library so it doesn't conflict but I'd rather not do that because reasons. I'm looking for some CMake magic to let me tell it to use the system library.
Upvotes: 3
Views: 2115
Reputation: 78270
I'd rather not do that because reasons.
I can see your hands are tied here :) so I would recommend "finding" the system library using find_library
and linking to that. If successful, find_library
will yield the absolute path to the library, and if you use that variable in your subsequent target_link_libraries
call, it should leave no room for ambiguity.
So in "/program/CMakeLists.txt", something like:
find_library(SystemFoo NAMES foo)
add_executable(program program.cpp)
target_link_libraries(program ${SystemFoo})
You may even want to include a few of the NO_xxx_PATH
args (e.g. NO_CMAKE_ENVIRONMENT_PATH
and/or NO_CMAKE_PATH
) to the find_library
call to reduce the number of locations CMake will search for the library.
Upvotes: 4