-
Notifications
You must be signed in to change notification settings - Fork 260
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
CMake: Errors when linking to shared library #248
Comments
I'll look into this over the weekend, its probably only a small fix required in the CMakeLists.txt file. |
+1 on this issue. I fixed it by adding this to the top level CMakeLists.txt in my own loguru fork, but I would prefer to be able to use upstream loguru without my own patches:
|
Sorry I never followed up on this, when I checked back in June I was unable to reproduce the error with my specific build environment so responding to the issue slipped my mind. Can I ask what OS and compiler you're using? @jalendw |
Okay nevermind, I setup an ubuntu 16 docker environment and was able to reproduce the The problem is that the Lines 88 to 92 in 4adaa18
Enabling stacktraces requires the loguru library to be linked to I'll need to think about this a bit more to decide on the best fix. |
Thanks @skaravos ! FWIW, I saw this on both Fedora40 and Ubuntu24.04, with GCC. |
Looking into this a bit more tonight, I noticed that I glossed over the fact that there were actually two distinct problems that you both were having.
and
I already addressed the However, I never explained the That errors occurs when you try to compile a So there were two solutions to that problem:
Both of those options will resolve your position-independent-code issue, and it looks like you both stumbled upon one of the two solutions and solved your own problems which is great. Loguru's CMakeLists.txt file doesn't explicitly define whether it should be compiled as a static/shared library with/without position independent code, because doing so could cause performance penalties or force usage constaints on the end user where it isn't needed. Instead it respects the cmake built-in variables |
Changes: - Fixes issue [emilk#248](emilk#248) - CMake now performs a quick `try_run()` to inspect whether the currently detected C++ compiler is using glibc, and if so sets the LOGURU_STACKTRACES variable to TRUE by default to match the default value of the LOGURU_STACKTRACES macro inside `loguru.cpp`. Notes: - This is required because the cmake file is currently setup to only link loguru to the `dl` library if the cmake variable LOGURU_STACKTRACES is TRUE. Unfortunately, the `loguru.cpp` file implicitly enables the stacktraces feature under certain conditions (glibc on non-windows) and the CMake file wasn't previously written to account for that. This meant that the `dl` library wasn't being linked even though stacktraces were enabled (in the .cpp file), causing a linking error. - `undefined reference to `dladdr'`
Thanks @skaravos , this seems to work without patching the loguru source: +if(UNIX AND NOT APPLE)
+ set(CMAKE_POSITION_INDEPENDENT_CODE ON)
+endif()
add_subdirectory(vendor/loguru)
+if(UNIX AND NOT APPLE)
+ unset(CMAKE_POSITION_INDEPENDENT_CODE)
+endif() |
I'm trying to use Loguru as a dependency for my shared library, but it's crashing with link-time errors. I have found a workaround that solves the issue, but this should probably be fixed, or at least documented.
Simple example:
main.cpp
:library.cpp
:CMakeLists.txt
:After configuring with CMake (version 3.16.3), running
make -j $(nproc)
yields:Workaround
This error can be fixed by adding
set(BUILD_SHARED_LIBS ON)
before theFetchContent_MakeAvailable(...)
call. After adding this line, I get a new error:I found that I can fix this new error by adding
set(LOGURU_STACKTRACES ON)
before theFetchContent_MakeAvailable(...)
call.After adding these lines, the new
CMakeLists.txt
looks like:And compilation now works. Note that this issue only happens when linking a shared library against loguru, static libraries and executables work fine.
How can this issue be fixed without requiring users to implement these workarounds, or at least document that these statements are necessary for shared libraries?
The text was updated successfully, but these errors were encountered: