Communication error with cmake

I am trying to understand why I get a binding error when compiling a project generated with CMake.

CMakeFiles.txt creates a static library for each project folder, and then merges everything together as follows:

# root CMakeLists.txt

add_subdirectory(subfolder1)
add_subdirectory(subfolder2)
add_subdirectory(...)

add_executable(target ${SOURCES})

set(LIBRARIES
  LIB_FOO
  LIB_BAR
  ...
)

target_link_libraries(target
  ${LIBRARIES}
)

then in each subfolder I have a simple CMakeLists.txt, for example

file(GLOB FOO_SOURCE *.cpp)
add_library(LIB_FOO ${FOO_SOURCE})

Now it works and compiles everything perfectly, but I get an undefined link when linking, so I tried to find out if everything is available at the end, and it looks like this. The actual error is as follows:

libLIB_WORLD.a(World.cpp.o): In function `World::generate(WorldGenOptions)':
World.cpp:(.text+0x803): undefined reference to `MapGenerator::MapGenerator(BlockMap*)'
World.cpp:(.text+0x837): undefined reference to `MapGenerator::generate(bool, WorldGenOptions)'

MapGenerator.cpp is now part LIB_MAP, so I checked if the file exists and contains characters:

:~$ nm libLIB_MAP.a | grep generate
....
00000000000044dc T _ZN12MapGenerator8generateEb15WorldGenOptions

:~$ nm CMakeFiles/LIB_MAP.dir/MapGenerator.cpp.o | grep generate
....
00000000000044dc T _ZN12MapGenerator8generateEb15WorldGenOptions

So, the symbol is present, at this moment I checked whether it is connected correctly with ld:

:~$ make VERBOSE=1
/usr/bin/g++  ... libLIB_MAP.a libLIB_WORLD.a ...

, , .

- , ? CMake, .

+4
1

, CMake.

LIB_WORLD LIB_MAP. CMake. , - . (, - , , , , .)

, , . , LIB_WORLD LIB_MAP, . , LIB_WORLD, , LIB_MAP, , , .

- LIB_WORLD:

add_library(LIB_WORLD [...])
target_link_libraries(LIB_WORLD LIB_MAP)

, - LIB_WORLD, LIB_MAP, CMake , . ( , LIB_MAP, target_link_libraries.)

LIB_WORLD , .

+5

Source: https://habr.com/ru/post/1599429/


All Articles