0

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)
9
  • 1
    What content of ADD_LINKER_OPTION variable? You pass those linked options to the shared library (because creation of a static library doesn't involve linking step). So it is possible that you add options which suppress checking for undefined symbols. Commented Sep 10, 2024 at 14:34
  • Thanks, Tsyvarev. I'm quite sure it's just left from an example and no additional options are passed, but I'll double check that. Commented Sep 10, 2024 at 14:59
  • You are better to prepare minimal reproducible example, which simply doesn't include irrelevant details. Commented Sep 10, 2024 at 15:04
  • That's reasonable (I was hoping that I've made an obvious mistake that someone would recognize). I try to prepare a minimal reproducible example. Commented Sep 10, 2024 at 15:14
  • 1
    The function names you see in nm output for C++ code are mangled names. The difference between _Z17import_initPKc and _Z17import_initPc are parameter types. It looks like you define import_init with const char* parameter (while declare it with char* parameter). Commented Sep 10, 2024 at 16:29

1 Answer 1

0
$ 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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.