Search code examples
cmakelibpngldd

Application linking 2 versions of same library due to dependencies


I'm having problems with a library (libpng) which my application is dynamically linking on Linux.

The libpng version I'm using is libpng15. Since my application is also linking DI-GUY, which is linking libpng3, these 2 version clash. With "clash" I mean that the linker (ldd) picks the libpng3 instead of libpng15.

If I look at the ldd output, both libpng15 and libpng3 are listed. The order is the following:

    libpng.so.3 => ../lib/libpng.so.3 (0x00007f4bccdff000)
    libpng15.so.15 => ../lib/libpng15.so.15 (0x00007f4bc5d1b000)

In CMake, I make sure I link against 15 specifically:

add_library(PNG::Shared SHARED IMPORTED)
    set_target_properties(PNG::Shared PROPERTIES
    IMPORTED_LOCATION ${_IMPORT_PREFIX}/lib/libpng15.so.15.4.0
    INTERFACE_INCLUDE_DIRECTORIES ${_IMPORT_PREFIX}/include
    INTERFACE_LINK_LIBRARIES z
)

From my understanding, the application should take libpng15, but since libpng.so.3 is further up in the ldd than the 15 version, the linker picks the libpng.so.3 instead.

How can I please force my application to use the libpng15.so.15, and the DI-GUY lib will use the libpng.so.3. Since I cannot recompile DI-GUY, I'm stuck with libpng.so.3 being also linked, but it should not get confused with the 15 version...

What I don't understand is why the libpng.so.3 is even considered since I explicitly link against libpng15.so.15.4.0


Solution

  • The right solution was to make the library, which is linking libpng3, link it in private mode:

    target_link_libraries(${TARGET} PUBLIC 
        VigFramework
        PedestriansBDI_Base
        DIGUY::GraphicsApi
        DIGUY::Main
        DIGUYDevil::Main
        TBB::Main2
        Qt4::QtCore
        Qt4::QtSql
        Qt4::QtGui
        Qt4::QtXml
        Qt4::QtNetwork
        Tiff::Main
    )
    
    target_link_libraries(${TARGET} PRIVATE 
        PNG3::Shared #required for DIGUY
    )
    

    This will guarantee that the main application links with libpng15 and does not get polluted by libpng3 from the other libraries.