2
votes

I have some target_link_libraries :

add_library(x x.cc)
target_link_libraries(x depX1 depX2 depX3)
add_executable(exe exe.cc)
target_link_libraries(exe x ${shared_lib1} ${shared_lib2})

Which results into the exe linking with x and the dependencies of x : depx1, depx2 etc The problem is that the shared_libs are intercalated between x and the dependencies of x and this is not acceptable in g++ 4.6 (it worked in older versions).

How to fix? I need to put the shared libs at the END of the compilation line, just like I specified in the CMakeLists.txt file. So I do not want them intercalated, I want them at the end of the compilation line. Also note that depx1, depx2 depx3 etc have their own dependencies as well so the only thing I want is that the shared libs to appear at the end of the compilation line.

How to do that with cmake? Thanks

--LATER EDIT2-- => SOLVED This can be solved by using an ugly hack:

add_custom_command(TARGET TargetName
                   PRE_LINK COMMAND ${PROJECT_SOURCE_DIR}/custom_script.sh 
                   ARGS ${PROJECT_BINARY_DIR}/src/TargetName/CMakeFiles/TargetName.dir/link.txt)

where src/TargetName/CMakeFiles/TargetName.dir/link.txt is the link command line CMake produces and custom_script.sh is a script which parses the file and arranges the dynamic link libraries at the end of the compilation line.

--LATER EDIT--
So I understand that with the current CMake and no option to put the shared libraries AT THE END of the compilation line and with g++4.6, everything breaks ! This is awesome !

1
Could you show the output of make VERBOSE=1 to see exactly what is the problem? - Michal Kottman
Hi, the output is something like the following: /usr/bin/c++ -g -O2 -Wall -pthread -static x -shared_lib1 -shared_lib2 depX2 depX3. I want shared_lib1 and shared_lib2 to be at the end of the compilation line - FlorinP
What if you specify you want a static library, like: add_library(x STATIC x.cc)? Also, try using two calls to target_link_libraries, one with x and one with shared libraries. - Michal Kottman
@MichalKottman, I hope you know that add_library has the default specifier as STATIC. - FlorinP
If the link-order matters, doesn't that mean that your shared_libs depend on depX1,2,3? And currently you do not have that dependency in your CMakeLists Why not list depX1,2,3 twice? Once between x and shared-libs and a second time at the end? - André

1 Answers

0
votes

Have you tried to repeat the x dependencies? For instance:

target_link_libraries(exe x depX1 depX2 depX3 ${shared_lib1} ${shared_lib2})