how to link shared libs in windows and linux in cross compile cmake build

296 Views Asked by At

I have a c++ third party shared library "My3rdParty" in windows and linux that I need my own c++ shared library "MySharedLib" to link to. My build needs to build on windows for windows, on windows for linux and on linux for linux. I created a findMy3rdparty.cmake module so I can use findPackage on My3rdParty file that uses findLibrary and findPackage to get the headers and libs and dlls and that works fine, then I create an imported target. 3rdParty library is the .so in linux and the .lib in windows. There's another find library for the shared library if its a windows target.

if( NOT TARGET My3rdParty )

    add_library( My3rdParty STATIC IMPORTED PUBLIC ${My3rdParty_LIBRARY} )

endif()

set_target_properties( My3rdParty PROPERTIES
            INTERFACE_INCLUDE_DIRECTORIES       "${My3rdParty_INCLUDE_DIRS}"
            IMPORTED_LOCATION                   "${My3rdParty_LIBRARY}"
            INTERFACE_LINK_LIBRARIES            "${My3rdParty_LIBRARY}"
            IMPORTED_IMPLIB                     "${My3rdParty_LIBRARY}"
    )

The libs and dlls and so's are stored in variables and are moved to the correct spot in the build dir and install dir. This compiles and links fine for everything and runs fine on windows on my windows computer but running on a linux machine it doesn't. On Linux MySharedLib.so is looking for a My3rdParty.so in a windows file format on the windows build machine which i'm pretty sure is because of Imported_Location. I think how the My3rdParty Target is being made is wrong. How should it be setup?

: error MySharedLIb.so could not find c://filepathtolinuxsharedlibonbuildcomputer/My3rdParty.so

I've tried a few variations of setting these target properties and not setting them and settings them depending on windows/linux target, etc. I don't really want to mess with things like rpath and ld_library_path, etc because all the other so files that I build are working and linking and running fine in linux and I'm pretty confident the error is in the generation of the 3rd party target.

How should you link libs, dlls, sos, and headers in a cross compilation compatible way when the install is not to system folders in linux?

1

There are 1 best solutions below

0
On

Solution was to not set IMPORTED_IMPLIB and not set IMPORTED_LOCATION.

I also changed the add_library line to be

add_library( My3rdParty INTERFACE IMPORTED )