gcc - cmake library linking order -


i have following libraries lib_a, lib_b, lib_c, lib_d. doing in cmake files (order important):

  1. add_library(lib_a)
  2. add_library(lib_b)
  3. add_library(lib_c)
  4. add_library(lib_d)
  5. target_link_libraries(lib_b lib_c)
  6. target_link_libraries(lib_a lib_b)
  7. add_executable(exec)
  8. target_link_libraries(exec lib_a)
  9. target_link_libraries(exec lib_d)

this results in following linker command.

linker -llib_a -llib_d -llib_b -llib_c

q1. why lib_b , lib_c after lib_d?

q2. when change cmake little bit , this:

8_ target_link_libraries(lib_a lib_d) 9_ target_link_libraries(exec lib_a)

then linking order linker -llib_a -llib_b -llib_c -llib_d here, lib_b , lib_c before lib_d. means target_link_libraries works differently executable targets , library targets. right?

the problem here lib_b , lib_c depend on lib_d don't want make target_link_libraries(lib_b lib_d) , target_link_libraries(lib_c lib_d), because have more of such cases , have manually each library. of course doing in q2 solves problem q3 - order guaranteed somehow cmake or fortuity?

thank you

just link lib_b , lib_c libd, it's worth effort (i can tell experience), otherwise have big troubles, example if try install program. lib_c , lib_d should have symbols solved when finished creating library files, before linking them other library.

by way, can compact target_link_libraries in 1 line per target, like:

target_link_libraries(exec lib_a lib_d) 

and, if exec doesn't depend directly on lib_d, can avoid linking if have correctly linked lib_a lib_d.

anyway, regarding q1: if order guaranteed cmake, not guaranteed way your linker treat it, going suffer pain if rely on that


Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -