Using dependencies with statics leads to linking dilemma
See original GitHub issueMy 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:
- Created 4 years ago
- Comments:8 (2 by maintainers)
Top GitHub Comments
@bromix a little late here but thanks for helping me resolve this issue as I stated in my SO question
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 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.