0

I'm new to Ubuntu, I have a project just shipped to Ubuntu (which works fine on MSVC), and I write a cmakelist like this:

add_library(libA STATIC
            ....)


add_library(libB STATIC
            ....)
target_link_libraries(libB libA)


add_excutable(c
            ....)
target_link_libraries(c libA)
target_link_libraries(c libB)

when linking, it tells me that some code in "libB" calls functions in "libA" but are undefined references.

I also tried to use only "target_link_libraries(c libB)", but it doesn't work. I also tried to use

add_custom_target(combined ALL
  COMMAND ${CMAKE_AR} rc libcombined.a $<TARGET_FILE:libA> $<TARGET_FILE:libB>)

introduced in CMake: include library dependencies in a static library, but it tells me "libcombined.a" is "error adding symbols archive has no index"

I also find a similar question: CMake libraries that depend on each other

But it uses multiple cmakelist files, can I do it in one cmakelist file?

I'm really new to cmake, I need your help, thanks!

Alexis
  • 145
  • 8
  • *"when linking, it tells me that some code in "libB" calls functions in "libA" but are undefined references."* is that true? How does `libB` know where to find `libA` headers? – JohnFilleau Jan 16 '23 at 17:42
  • 3
    Probably, the undefined function is not defined even in LibA. In any case you need to add to the question post the **exact error message** and your C/C++ **code** which defines given function. – Tsyvarev Jan 16 '23 at 17:43
  • @Tsyvarev Thank you very much, I will try to show that the functions are indeed defined in libA, and update my question – Alexis Jan 16 '23 at 17:51
  • @Tsyvarev Thank you again, the implementations were indeed not included in libA!! – Alexis Jan 16 '23 at 20:46

1 Answers1

1

It tooks me two days to find out that the implementations were not included in the libA

It was never about dependencies.

In case there are also new users of cmake, I post my case here. If there are both c libs and cpp libs (and cuda), it should be:

project(projname LANGUAGES C CXX CUDA)

instead of

project(projname LANGUAGES CXX CUDA)

otherwise, the '.c' files are not included in the building. That was the reason that the functions were undefined, thanks to @Tsyvarev

Alexis
  • 145
  • 8