Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CMake to produce -L<path> -l<lib> link flags for static libraries

I'm using CMake 2.8 in order to build an application based on MQX OS (using CodeWarrior).
The CMake project basically builds a set of static libraries (let's say LIB1 and LIB2).
I then reference these libraries in the final executable cmake rule:

target_add_executable(X ${some_sources})
target_link_libraries(X LIB1 LIB2)

My problem is that some symbols are defined in more that one library.
Thus, a link command like:

mwldarm <args> -o <output> <objects> /path/to1/libLIB1.a /path/to2/libLIB2.a

would lead to multiple definition of symbols error. Instead, I would like CMake to generate a link command like:

mwldarm <args> -o <output> <objects> -L/path/to1 -L/path/to2 -lLIB -lLIB2

Question: How to get the following variables from CMAKE?

  • Libraries directories flags (ex: -L/path/to1 -L/path/to2)
  • Libraries link flags (ex: -lLIB -lLIB2)

I've read stuff concerning RPATH but it seems to concern shared libraries only. Am I right?

Thanks for advance.
I do appreciate.

like image 588
maqui Avatar asked Feb 11 '13 23:02

maqui


2 Answers

It seems that policy CMP0003 may be what you need.

To use it add the following line near the beginning of your CMakeLists.txt:

CMAKE_POLICY( SET CMP0003 OLD )

Another possibility is to directly set the dependencies and search path, however it's not the cleanest way. Assuming you libraries are called liba.a and libb.a, then:

LINK_DIRECTORIES( ${paths_to_search_for} )
TARGET_ADD_EXECUTABLE(X ${some_sources} )
ADD_DEPENDENCIES(X LIB1 LIB2)
TARGET_LINK_LIBRARIES(X a b )

Note that in this case a and b are not cmake targets, therefore a little machinery is needed to correctly set the dependencies.

like image 191
Massimiliano Avatar answered Nov 03 '22 22:11

Massimiliano


Part of the design of CMake is that it links with full paths. Why is that a problem?

Toggling the behavior with the policy is not the correct approach.

http://www.cmake.org/gitweb?p=cmake.git;a=commitdiff;h=cd4fa896b

like image 1
steveire Avatar answered Nov 03 '22 22:11

steveire