Linking error with cmake

前端 未结 1 1522
粉色の甜心
粉色の甜心 2021-01-14 02:05

I\'m trying to understand why I get a linking error while compiling a project generated with CMake.

The CMakeFiles.txt builds a static library for each folder of the

相关标签:
1条回答
  • 2021-01-14 02:38

    This is a problem of a library dependency that was not modeled correctly in CMake.

    Your LIB_WORLD references methods from LIB_MAP. This dependency is not modeled in your CMake scripts. As both of those are static libraries, they will still build fine on their own. (Remember that static libraries are essentially a bunch of object files packed together, but they never pass through the linker.)

    However, as soon as you link them to an executable or a shared library, you will get the problem. Even though your executable links against both LIB_WORLD and LIB_MAP, it does so in the wrong order. So when the linker is trying to resolve the missing symbols for LIB_WORLD, it does not know yet about the symbols exported by LIB_MAP, hence the error message you experienced.

    The proper fix is to introduce the dependency on LIB_WORLD:

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

    Now whenever you link something against LIB_WORLD you will always also link against LIB_MAP and CMake will take care that the order is right. (In particular, if your executable does not make direct use of methods from LIB_MAP you might want to remove it from its target_link_libraries altogether.)

    As an additional benefit, it now allows you to build LIB_WORLD as a shared library, which would have failed with a linker error before.

    0 讨论(0)
提交回复
热议问题