简体   繁体   中英

C++ program compiles and links, but then raises undefined symbol error

I'm getting quite a headache with this problem because I simply cannot figure out why it happens. I cannot reproduce it on my own machine, but it happens for colleagues all the time.

I have a C++ program that compiles and links without any errors (on their machines as well). But when trying to execute it, they get an "undefined symbol error" - even though linking didn't throw any errors.

Do you have any suggestions what could be the reason? I have checked the linking order of the dependencies, the CMake version, the compiler version, etc., but didn't get any further.

Just roughly, the CMakeLists.txt looks like that:

cmake_minimum_required(VERSION 3.18.2)

project(name)
set(TARGET targetname)

add_library(dependency OBJECT ${DEPENDENCY_SOURCES})
add_dependencies(${TARGET} dependency)
target_include_directories(${TARGET} PRIVATE ${DEPENDENCY_SOURCE_DIR}/src)

target_link_libraries(${TARGET} PRIVATE ${dependency})

The actual project is here (not wanting to do advertisement, I'm just lost at this point)

PS:

An example is given below. As mentioned, installing works without any problems, but the symbols are missing at runtime.

The error message looks like this then one tries to execute the program from Python (the C++ functions are bound via Pybind11): 错误信息

PS 2:

I've gotten an installation log from a colleague. The only difference to mine is that CMake can't find the OpenMP libraries.

  -- The C compiler identification is GNU 9.3.0
  -- The CXX compiler identification is GNU 9.3.0
  -- Detecting C compiler ABI info
  -- Detecting C compiler ABI info - done
  -- Check for working C compiler: /home/aco/miniconda3/envs/my_hetbuilder/bin/x86_64-conda-linux-gnu-cc - skipped
  -- Detecting C compile features
  -- Detecting C compile features - done
  -- Detecting CXX compiler ABI info
  -- Detecting CXX compiler ABI info - done
  -- Check for working CXX compiler: /home/aco/miniconda3/envs/my_hetbuilder/bin/x86_64-conda-linux-gnu-c++ - skipped
  -- Detecting CXX compile features
  -- Detecting CXX compile features - done
  -- Project source directory: /tmp/pip-req-build-hb5tu6_r
  -- CMAKE_BUILD_TYPE=RELEASE
  -- Found Python3: /home/aco/miniconda3/envs/my_hetbuilder/bin/python3.8 (found version "3.8.12") found components: Interpreter Development Development.Module Development.Embed
  -- Found Python3 header files /home/aco/miniconda3/envs/my_hetbuilder/include/python3.8
  -- Submodule update
  -- Performing Test HAS_FLTO
  -- Performing Test HAS_FLTO - Success
  -- Found pybind11: /home/aco/miniconda3/envs/my_hetbuilder/include (found version "2.7.1" )
  -- Building spglib from submodule.
  -- Could NOT find OpenMP_C (missing: OpenMP_C_FLAGS OpenMP_C_LIB_NAMES) (Required is at least version "4.5")
  -- Could NOT find OpenMP_CXX (missing: OpenMP_CXX_FLAGS OpenMP_CXX_LIB_NAMES) (Required is at least version "4.5")
  -- Could NOT find OpenMP (missing: OpenMP_C_FOUND OpenMP_CXX_FOUND) (Required is at least version "4.5")
  -- Could not find OpenMP
  -- Configuring done
  -- Generating done
  CMake Warning:
    Manually-specified variables were not used by the project:

      EXAMPLE_VERSION_INFO
      PYTHON_EXECUTABLE


  -- Build files have been written to: /tmp/pip-req-build-hb5tu6_r/build/temp.linux-x86_64-3.8
  [  3%] Building C object CMakeFiles/hetbuildersymspg.dir/external/spglib/src/arithmetic.c.o
  [  6%] Building C object CMakeFiles/hetbuildersymspg.dir/external/spglib/src/cell.c.o
  [ 10%] Building C object CMakeFiles/hetbuildersymspg.dir/external/spglib/src/debug.c.o
  [ 13%] Building C object CMakeFiles/hetbuildersymspg.dir/external/spglib/src/delaunay.c.o
  [ 16%] Building C object CMakeFiles/hetbuildersymspg.dir/external/spglib/src/determination.c.o
  [ 20%] Building C object CMakeFiles/hetbuildersymspg.dir/external/spglib/src/hall_symbol.c.o
  [ 23%] Building C object CMakeFiles/hetbuildersymspg.dir/external/spglib/src/kgrid.c.o
  [ 26%] Building C object CMakeFiles/hetbuildersymspg.dir/external/spglib/src/kpoint.c.o
  [ 30%] Building C object CMakeFiles/hetbuildersymspg.dir/external/spglib/src/mathfunc.c.o
  [ 33%] Building C object CMakeFiles/hetbuildersymspg.dir/external/spglib/src/niggli.c.o
  [ 36%] Building C object CMakeFiles/hetbuildersymspg.dir/external/spglib/src/overlap.c.o
  [ 40%] Building C object CMakeFiles/hetbuildersymspg.dir/external/spglib/src/pointgroup.c.o
  [ 43%] Building C object CMakeFiles/hetbuildersymspg.dir/external/spglib/src/primitive.c.o
  [ 46%] Building C object CMakeFiles/hetbuildersymspg.dir/external/spglib/src/refinement.c.o
  [ 50%] Building C object CMakeFiles/hetbuildersymspg.dir/external/spglib/src/site_symmetry.c.o
  [ 53%] Building C object CMakeFiles/hetbuildersymspg.dir/external/spglib/src/sitesym_database.c.o
  [ 56%] Building C object CMakeFiles/hetbuildersymspg.dir/external/spglib/src/spacegroup.c.o
  [ 60%] Building C object CMakeFiles/hetbuildersymspg.dir/external/spglib/src/spg_database.c.o
  [ 63%] Building C object CMakeFiles/hetbuildersymspg.dir/external/spglib/src/spglib.c.o
  [ 66%] Building C object CMakeFiles/hetbuildersymspg.dir/external/spglib/src/spin.c.o
  [ 70%] Building C object CMakeFiles/hetbuildersymspg.dir/external/spglib/src/symmetry.c.o
  [ 70%] Built target hetbuildersymspg
  [ 73%] Building CXX object CMakeFiles/hetbuilder_backend.dir/backend/pybindings.cpp.o
  [ 76%] Building CXX object CMakeFiles/hetbuilder_backend.dir/backend/atom_class.cpp.o
  [ 80%] Building CXX object CMakeFiles/hetbuilder_backend.dir/backend/atom_functions.cpp.o
  [ 83%] Building CXX object CMakeFiles/hetbuilder_backend.dir/backend/coincidence_algorithm.cpp.o
  [ 86%] Building CXX object CMakeFiles/hetbuilder_backend.dir/backend/helper_classes.cpp.o
  [ 90%] Building CXX object CMakeFiles/hetbuilder_backend.dir/backend/interface_class.cpp.o
  [ 93%] Building CXX object CMakeFiles/hetbuilder_backend.dir/backend/logging_functions.cpp.o
  [ 96%] Building CXX object CMakeFiles/hetbuilder_backend.dir/backend/math_functions.cpp.o
  [100%] Linking CXX shared module ../lib.linux-x86_64-3.8/hetbuilder_backend.cpython-38-x86_64-linux-gnu.so
  [100%] Built target hetbuilder_backend

Does ldd show that libsymspg is not linked or that the link cannot be resolved?

There are a lot of compiler and linker options for how symbols are resolved at build time and at run time, and CMake does not usually set up the build system to require that a symbol from an external header is actually resolvable at build time.

If you don't see libsymspg at all in the ldd output , you need to add linking flags (preferably with target_link_libraries if symspg provides a CMake static or shared library target).

If you see libsymspg in the ldd output, but it cannot be resolved , then you need to provide a hint to the loader. Usually, either add to the LD_LIBRARY_PATH environment variable, or configure the RPATH of the binary. CMake supports several paradigms, so there are various relevant properties ( https://cmake.org/cmake/help/latest/search.html?q=RPATH ), and your choice depends to some extent on your intended relocatability for your package and on the expected ABI compatibility of the dependency. But you could start with INSTALL_RPATH_USE_LINK_PATH

update

The original post is potentially misleading: you imply that you are linking against a shared object library, but this does not seem to be the case.

Comparing https://github.com/romankempt/hetbuilder/blob/6cf49f1e39c7f24cc75bfa696c5623b5a882c9aa/CMakeLists.txt to the output you shared, you are not linking against libsymspg.so (nor libsymspg.a ). You are just building some of its source files and linking the object files directly into your binary (with a local OBJECT target). Presumably, you missed one.

Note also that there are ways to try to catch this sort of thing at build time. See, for instance, the --no-undefined (gnu ld) option.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM