0

I am looking at my problem for hours and I am stuck...

I have a library lib_1 with function getNumber().
Then I have a library lib_2 with function addNumbers().
addNumbers() calls getNumber() from lib_1.

My CMakeLists.txt:

add_library( lib_1 STATIC IMPORTED )
set_target_properties(lib_1 PROPERTIES IMPORTED_LOCATION path_to_lib_1)

add_library( lib_2 STATIC lib2.cpp )
target_link_libraries( lib_2 lib_1 )
target_include_directories(lib_2 PUBLIC include )

lib_2 compiles just fine, also the unit tests which use getNumber() are working.

Then I want to link lib_2 to my application app.

add_library( lib_1 STATIC IMPORTED )
set_target_properties(lib_1 PROPERTIES IMPORTED_LOCATION path_to_lib_1)

add_library( lib_2 STATIC IMPORTED )
set_target_properties(lib_2 PROPERTIES IMPORTED_LOCATION path_to_lib_2)

add_executable(app my_source_files )
target_link_libraries(app lib_1 lib_2)

When I compile my app I get this well known error:
In function addNumbers() from lib_2, undefined reference to getNumber()

I really don't get it, why its an undefined reference??

And btw do I really need to link against lib_1 in my application when already linking to lib_2 which itself is statically linking to lib_1?

madmax
  • 1,803
  • 3
  • 25
  • 50
  • 2
    Order matters! If `lib_2` depends on `lib_1`, then `lib_2` must be *before* `lib_1` when linking. – Some programmer dude Dec 05 '18 at 13:38
  • 3
    "do I really need to link against lib_1 in my application when already linking to lib_2 which itself is statically linking to lib_1?" - Yes, you need. Unlike to *shared* libraries, *static* ones don't contain information about linked libraries. – Tsyvarev Dec 05 '18 at 13:39
  • Omfg, didn't think about ordering them ... Thanks, it's working now... – madmax Dec 05 '18 at 13:54

1 Answers1

0

So as one can see in comments, the solution is to have the correct order of statically linked libraries:

add_executable(app my_source_files )
target_link_libraries(app lib_2 lib_1)
madmax
  • 1,803
  • 3
  • 25
  • 50