CMake: How to remove duplicates from target link list?
Asked Answered
S

2

6

My argument list for the linker is getting too long and I am receiving the following error at this point:

Error running link command: Argument list too long

I am using different 3rd party libraries in my application (including Qt, OpenCV, PointCloudLibrary, VTK, ...), some of which I compiled manually, others I installed through homebrew on Mac OS X 10.7.

For the actual build setup of my application I am using CMake (2.8-9). Recently, I started linking against PCL (PointCloudLibrary 1.6), which is giving me a headache and the reported error right now.

My CMake file looks something like this:

cmake_minimum_required(VERSION 2.6)
project(cmake_test)

add_definitions( -DBOOST_ALL_NO_LIB )

add_definitions( -DBOOST_ALL_DYN_LINK )

find_package(Boost COMPONENTS iostreams system filesystem regex thread date_time timer atomic locale graph exception REQUIRED)

if(Boost_FOUND)
    add_definitions("-DHAS_BOOST")
endif()

find_package(PCL REQUIRED)

include_directories(
    ${Boost_INCLUDE_DIRS}
    ${PCL_INCLUDE_DIRS}
)

link_directories(
    ${Boost_LIBRARY_DIRS}
    ${PCL_LIBRARY_DIRS}
)

add_definitions(
    ${PCL_DEFINITIONS}
)

add_executable(cmake_test cmake_test.cpp)

target_link_libraries(cmake_test
        ${Boost_LIBRARIES}
        ${PCL_COMMON_LIBRARIES}
        ${PCL_IO_LIBRARIES}
        ${PCL_FILTERS_LIBRARIES}
        ${PCL_FEATURES_LIBRARIES}
        ${PCL_GEOMETRY_LIBRARIES}
        ${PCL_SEGMENTATION_LIBRARIES}
        ${PCL_SURFACE_LIBRARIES}
        ${PCL_VISUALIZATION_LIBRARIES}
)

The main issue appears to be that the target link library list now contains multiple references to all boost libs. In fact, for every ${PCL_***_LIBRARIES} the complete ${Boost_LIBRARIES} list is added over and over again.

My question is, how could I remove such duplicates that come in as dependencies from third party libraries or if anyone has an idea how to handle this appropriately.

Please keep in mind that I am actually using additional libraries not listed in this CMake (e.g. Qt, VTK), which add further items to the target link list, eventually making it too big for the linker to handle.

Thank you!

Suffolk answered 6/8, 2013 at 14:12 Comment(2)
Is there any solution yet, which takes the comment of @languitar into account?Filly
I suppose you could call list(REMOVE_ITEM <list> <value>) for debug / release as <value>, if those unwanted entries in the list cause issues.Suffolk
M
8

Try the REMOVE_DUPLICATES list operation:

list(REMOVE_DUPLICATES <list>)

This will remove all duplicates from the list you specify.

See the CMake documentation.

Mummy answered 6/8, 2013 at 16:6 Comment(2)
How exactly did I miss that? Thanks!Suffolk
Please note that this can destroy proper linking in case your list contains link statemens for different compilation types. In that case the link list could look like debug;liba-d;release;liba;debug;libb-d... Here, remove_duplicates will remove all but the first occurrences of debug, release etc which results in garbage linker commands.Pyromagnetic
S
0

One possible solution could be to simply create a temporary list containing all the required ${PCL_***_LIBRARIES} and remove all ${BOOST_LIBRARIES} items from that list:

[...]

foreach(i ${Boost_LIBRARIES})

list(REMOVE_ITEM PCL_ALL_REQUIRED_LIBRARIES ${i})

endforeach(i)

[...]

I would prefer to actually find and delete duplicates, since I will only delete duplicates with ${Boost_LIBRARIES} this way, but at least the list size is reduced for now.

Suffolk answered 6/8, 2013 at 14:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.