c++11cmakeshared-librariesstatic-libraries

symbol lookup error in shared library for existing function from static library


I have a static library "import" that provides a non-member function import_init(char*) as well as a class ImportObject with the member function Import(char*) that references import_init(char*).

I can build the dynamic library "viewer" using the static library "import", but at runtime, I'm getting this error:

./TestApplication: symbol lookup error: ./libViewer.so: undefined symbol: _Z17import_initPc

I'm surprised, that I'm not getting an error at compile time. Also it seems, that the function is available. In nm output, it's shown once as "in the text (code) section" and once as "undefined". I don't understand what the letters after the function name mean exactly and why they differ.

$ nm -g libViewer.so | grep import_init
00000000001e1688 T _Z17import_initPKc
                 U _Z17import_initPc

I'm suspecting, that I made a mistake with the CMake configuration.

This is a simplified version of CMakeLists.txt for "import":

cmake_minimum_required (VERSION 3.14)
include(FetchContent)

project (Import)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)

...

add_library(Import STATIC)

target_include_directories(Import
    PUBLIC
        ${CMAKE_CURRENT_SOURCE_DIR}/include
)

target_link_libraries(Import
    ...
)

target_link_options(Import
    PRIVATE
        $<$<PLATFORM_ID:Linux>:-Wl,--whole-archive>
        ${ADD_LINKER_OPTION}
)

add_subdirectory(src)

This is a simplified version of CMakeLists.txt for "viewer":

cmake_minimum_required (VERSION 3.14)
include(FetchContent)

project (Viewer)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)

...

FetchContent_Declare(
    Import
    GIT_REPOSITORY git@URL:Import.git
    GIT_TAG HEAD
    GIT_SHALLOW TRUE
)
FetchContent_MakeAvailable(Import)

add_library(libViewer SHARED)

set_target_properties(libViewer PROPERTIES LIBRARY_OUTPUT_NAME Viewer)

target_include_directories(libViewer
    PUBLIC
        ${CMAKE_CURRENT_SOURCE_DIR}/include
    PRIVATE
        ${import_SOURCE_DIR}/include
)

target_link_libraries(libViewer
    Import
    ...
)

add_subdirectory(src)

Solution

  • $ nm -g libViewer.so | grep import_init
    00000000001e1688 T _Z17import_initPKc
                     U _Z17import_initPc
    

    As Tsyvarev already mentioned in the comments, the output of nm shows, that the function import_init was defined with the argument const char* (_Z17import_initPKc) but declared with the argument char* (_Z17import_initPc).

    The definition of the function import_init with char* as argument (_Z17import_initPc) couldn't be found.