Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

linking cmrc with a shared library #17

Open
neel opened this issue Feb 24, 2020 · 6 comments
Open

linking cmrc with a shared library #17

neel opened this issue Feb 24, 2020 · 6 comments

Comments

@neel
Copy link

neel commented Feb 24, 2020

Linker error when I try to link with cmrc generated library with a SHARED library.

/usr/bin/ld: thelibx-resources.a(lib.cpp.o): relocation R_X86_64_PC32 against symbol `_ZN4cmrc6detail9directoryD1Ev' can not be used when making a shared object; recompile with -fPIC

I want the shared library to be statically linked with the cmrc_add_resource_library generated target thelibx-resources.

add_library(thelibx SHARED ${THE_LIBX_SOURCES})
target_link_libraries(thelibx ${Boost_PROGRAM_OPTIONS_LIBRARY} thelibx-resources)

If I remove that SHARED then everything works fine.

@neel neel changed the title linking a shared library linking cmrc with a shared library Feb 24, 2020
@mbr0wn
Copy link

mbr0wn commented Mar 9, 2020

I see the same thing.

@mbr0wn
Copy link

mbr0wn commented Mar 9, 2020

It looks like forcing -fPIC will fix this. Here's my changes to CMakeRC.cmake:

     # Generate the actual static library. Each source file is just a single file
     # with a character array compiled in containing the contents of the
     # corresponding resource file.
     add_library(${name} STATIC ${libcpp})
     set_property(TARGET ${name} PROPERTY CMRC_LIBDIR "${libdir}")
     set_property(TARGET ${name} PROPERTY CMRC_NAMESPACE "${ARG_NAMESPACE}")
     target_link_libraries(${name} PUBLIC cmrc::base)
     set_property(TARGET ${name} PROPERTY CMRC_IS_RESOURCE_LIBRARY TRUE)
     set_property(TARGET ${name} PROPERTY POSITION_INDEPENDENT_CODE ON)

Note the last line.

@mbr0wn
Copy link

mbr0wn commented Mar 9, 2020

I'm just not quite sure if that's a general solution.

@themarpe
Copy link

If your root project is a shared library I think that you should apply the following in your CMakeLists.txt:

set(CMAKE_POSITION_INDEPENDENT_CODE ON)

before adding dependencies, to compile them with -fPIC.

Or maybe try adding a property only to cmrc resource library after cmrc_add_resource_library call.

cmrc_add_resource_library(foo-resources ALIAS foo::rc NAMESPACE foo  ...)
set_property(TARGET foo::rc PROPERTY POSITION_INDEPENDENT_CODE ON)

Let me know how these options work for you - to not need to modify the CMakeRC.cmake file

@mbr0wn
Copy link

mbr0wn commented Jul 16, 2020

Thanks @themarpe. You would have to do

set_property(TARGET foo-resources PROPERTY POSITION_INDEPENDENT_CODE ON)

in your example (aliases are not permitted in set_property()), but your comment was spot-on. Thanks!

@mbr0wn
Copy link

mbr0wn commented Jul 16, 2020

@neel @themarpe I would suggest closing this issue. This ^^^ is the right answer. It might be worth putting into the docs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants