question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using dependencies with statics leads to linking dilemma

See original GitHub issue

My cmake-js example project consists out of the NAPI using cpp project as an usual cmake library target, but for separation reasons it uses another cmake library target in a subfolder as dependency: https://github.com/Superlokkus/spielwiese/blob/napi/CMakeLists.txt#L60

Everything worked fine until I used a static object in the dependency, it leads to a linking dilemma:

Under Linux I got liking issues due to missing relocatable code flags used for the dependency so adding the SHARED property to the dependency target fixes the problem under linux.

Under Windows on the other hand t works without the SHARED force, i.e. defaulted static build, but when using SHARED for the dependency for let it work under linux, I won’t link because MSVC will name the shared library in another way when build as a shared i.e. dll.

The obvious workaround would be to use a switch in cmake on the dependency to force shared only on linux, but this is not working as intended I guess.

To reproduce: Checkout https://github.com/Superlokkus/spielwiese/tree/napi_linking_problems_statics , add SHARED to https://github.com/Superlokkus/spielwiese/blob/napi_linking_problems_statics/src/lib_name/CMakeLists.txt if using MSVC, then yarn install && yarn test.

Used: cmake-js 6.0.0 cc (GCC) 9.2.0 MSVC 2019

Logs short

Linux

info RUN cmake --build “/home/markus/development/spielwiese/build” --config Release Scanning dependencies of target lib_name [ 25%] Building CXX object src/lib_name/CMakeFiles/lib_name.dir/src/lib.cpp.o [ 50%] Linking CXX static library liblib_name.a [ 50%] Built target lib_name Scanning dependencies of target spielwiese [ 75%] Building CXX object CMakeFiles/spielwiese.dir/src/spielwiese.cpp.o [100%] Linking CXX shared library Release/spielwiese.node /usr/bin/ld: src/lib_name/liblib_name.a(lib.cpp.o): relocation R_X86_64_PC32 against symbol `static_map’ can not be used when making a shared object; recompile with -fPIC /usr/bin/ld: final link failed: bad value collect2: error: ld returned 1 exit status make[2]: *** [CMakeFiles/spielwiese.dir/build.make:85: Release/spielwiese.node] Error 1 make[1]: *** [CMakeFiles/Makefile2:96: CMakeFiles/spielwiese.dir/all] Error 2 make: *** [Makefile:130: all] Error 2 ERR! OMG Process terminated: 2

Windows

Checking Build System Building Custom Rule C:/Users/marku/development/spielwiese/src/lib_name/CMakeLists.txt lib.cpp LINK : warning LNK4199: /DELAYLOAD:NODE.EXE ignored; no imports found from NODE.EXE [C:\Users\marku\development\spielwiese\build\src\lib_name\lib_name.vcxproj] lib_name.vcxproj -> C:\Users\marku\development\spielwiese\build\Release\lib_name.dll Building Custom Rule C:/Users/marku/development/spielwiese/CMakeLists.txt spielwiese.cpp win_delay_load_hook.cc Generating Code… LINK : fatal error LNK1181: cannot open input file ‘src\lib_name\Release\lib_name.lib’ [C:\Users\marku\development\spielwiese\build\spielwiese.vcxproj] ERR! OMG Process terminated: 1 error Command failed with exit code 1.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
justin-hackincommented, Nov 26, 2020

@bromix a little late here but thanks for helping me resolve this issue as I stated in my SO question

1reaction
bromixcommented, Mar 16, 2020

If your dependency is a static lib you’ve to use this setting: https://cmake.org/cmake/help/v3.17/prop_tgt/POSITION_INDEPENDENT_CODE.html

I had the same issue like you and this solved my problem under Linux and MacOS. Also this works with Windows.

# this setting is needed for unix a like systems, so we can link the library
set_target_properties("<YOUR_PROJECT_NAME>"
    PROPERTIES
        POSITION_INDEPENDENT_CODE TRUE
)

This value is only true by default, if the project is a module or a shared lib (e.g. a DLL under windows). So for a static lib you’ve to force the value to true. Maybe this helps.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using dependencies with statics leads to linking dilemma
My cmake-js example project consists out of the NAPI using cpp project as an usual cmake library target, but for separation reasons it...
Read more >
Dependency Factors in Evidence Theory: An Analysis ... - NCBI
We present an overview of the main studies developed to deal with uncertainties due to dependencies. We note an emphasis on the use...
Read more >
Linking static libraries, that share another static library
There is no simple solution to dependency management without a dependency manager (CocoaPods, Carthage, SPM). That said, you do not need a ...
Read more >
Libtool Inter-library Dependencies - GNU.org
Libtool's basic premise is to make static and shared libraries behave the same way from a programmer's point of view. This allows users...
Read more >
Solving the World's Open-Source Software Security Problem
Dependency Issues: Solving the World's Open-Source Software ... Knowing what vulnerabilities are in your software is a full-time and nearly ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found