ead
ead

Reputation: 11

Dynamic Linker does not find Libraries in C using CLion

I'm working on macOS Sonoma 14.2 and I'm using Apple clang version 15.0.0 (clang-1500.1.0.2.5), cmake version 3.28.0 and GNU Make 3.81. I use CLion 2023.3 (Build #CL-233.11799.238, built on December 1, 2023) as IDE with, best to my knowledge, the standard way build an application via cmake.

I get the following error message from my dynamic linker on starting my build application. The building of the application works fine and I found no linking error in the log of the build process.

dyld[8397]: Symbol not found: _divsufsort64
  Referenced from: <12C04522-55DC-3F19-8288-E875505D9BCE> /Users/<user>/Documents/projects/problems_linker/cmake-build-debug/problems_linker
  Expected in:     <no uuid> unknown

Here is a minimal code example that produces the above error: (both files are in the same directory)

CMakeLists.txt

cmake_minimum_required(VERSION 3.27)
project(problems_linker C)

set(CMAKE_C_STANDARD 11)

set(SOURCES
main.c
)

set(INCLUDES
${CMAKE_CURRENT_BINARY_DIR}
/usr/local/include
/usr/local/lib
)

add_executable(problems_linker main.c)
target_include_directories(${PROJECT_NAME} PRIVATE ${INCLUDES})
find_package(ZLIB)
target_link_libraries(${PROJECT_NAME} PRIVATE ZLIB::ZLIB)
target_link_libraries(${PROJECT_NAME} PRIVATE /usr/local/lib/libdivsufsort64.dylib)

main.c

#include <stdio.h>
#include <divsufsort64.h>
#include <zlib.h>
#include <stdlib.h>

int main() {
printf("Hello, World!\n");

    gzFile gz;
    
    const uint8_t bs[] = {2, 3, 1, 0};
    long long n = 4;
    int64_t* sa = malloc(n * sizeof(*sa));
    if(divsufsort64(bs, sa, n) < 0)
    {
        
    }
    printf("Works\n");
    
    return 0;

}

The error should not occur using the following commands, but it occurs using the Run-Button of the CLion IDE. Using the command line with the following commands, the project builds and runs without any error.

mkdir build 
cd build 
cmake .. 
make  
./problems_linker

Note that this is a minimal example to reproduce the error, not very beautiful code that is used to solve any real problem. I included ZLIB in the example as well, because ZLIB is the only library, that still works for some reason.

I installed both, divsufsort and ZLIB, via brew:

divsufsort:

brew tap brewsci/bio
brew install Formula/libdivsufsort

zlib:

brew install zlib

I searched and tested to set the DYLD_FALLBACK_LIBRARY_PATH variable, but that does not change anything. I tried many variations of the CMakeLists including find_library calls etc., but nothing changed the error message.

I tested old projects via command line and CLion, that worked at their finishing date on both, CLion and Command Line, and got the same problem as described above.

Any help and ideas are appreciated. Thanks.

Edit: (based on the comment of @uta)

I tested every suggestion of that question. Adding that all together results in the following adapted CMakeLists.txt, but the error is still the same.

cmake_minimum_required(VERSION 3.27)
project(problems_linker C)

set(CMAKE_C_STANDARD 11)

set(SOURCES
        main.c
)

set(INCLUDES
        ${CMAKE_CURRENT_BINARY_DIR}
        /usr/local/include
        /usr/local/lib
)
SET(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
SET(CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_STATIC_LIBRARY_SUFFIX})
add_executable(problems_linker main.c)
target_include_directories(${PROJECT_NAME} PRIVATE ${INCLUDES})
find_package(ZLIB)
find_library(DIVSUFSORT libdivsufsort64 PATHS /usr/local/lib/)
target_link_libraries(${PROJECT_NAME} PRIVATE ZLIB::ZLIB)
target_link_libraries(${PROJECT_NAME} PRIVATE ${DIVSUFSORT})

I'm also not able to build it from command line with the CMakeLists file from above, it says, the variable DIVSUFSORT is not available.

I want to note, that the linker finds all paths correctly, only the dynamic linker fails.

Upvotes: 1

Views: 134

Answers (1)

ead
ead

Reputation: 11

By adding DYLD_LIBRARY_PATH=/usr/local/lib/ (the path to the .dylib file) to the Environmental Variables in the Run/Debug Configurations for each of my Configurations, it is properly working again.

I tried to set it at other locations, none of them worked. The DYLD_FALLBACK_LIBRARY_PATH variable does not work as well.

Note that this this is only a workaround.

Upvotes: 0

Related Questions