c++ - cmake: In which order do I have to specify TARGET_LINK_LIBRARIES -
i struggling again , again linker problems since 1 has specify libraries within target_link_libraries in correct order. how can determine order? example:
i have following libraries
liba depends on boost libb depends on postgresql , liba (and therefore on boost) mytarget uses liba, libb , boost directly (and through libb depends on postgresql)
since required libaries linked if executable created have specifiy libraries when linking mytarget (the final executable):
target_link_libraries(${applicationname} libboost_program_options.a libboost_system.a libboost_filesystem.a libboost_date_time.a libboost_regex.a # should include boost libraries strangely libs (the ones above) # need specified "by hand"??? ${boost_libraries} # postgresql stuff libpq.a libsoci_core.a libsoci_postgresql.a libpq.so # libs libb.a liba.a ${cmake_thread_libs_init} # pthreads, needed boost ${cmake_dl_libs} # libdl.so etc. )
since linking boost static cmakelists.txt contains
set(boost_use_static_libs on)
however, still linking errors such "undefined references boost::re_detail::perl_matcher or boost::date_time::month_formatter"
this annoying, changing lib ordering , undefined references disappear new undefined references appear.
how can identify correct ordering?!
edit:
i solved problem above drawing respectively identifying dependencies between libs , ordering them appropriately (therefore libboost_log.a has been added):
target_link_libraries(${applicationname} libb.a liba.a # postgresql stuff libpq.a libsoci_core.a libsoci_postgresql.a libpq.so # boost libboost_program_options.a libboost_system.a libboost_log.a libboost_filesystem.a libboost_date_time.a libboost_regex.a # should include boost libraries strangely libs (the ones above) # need specified "by hand"??? ${boost_libraries} # lowlevel needed boost ${cmake_thread_libs_init} # pthreads, needed boost ${cmake_dl_libs} # libdl.so etc. )
so ordering top down. top executable, followed directly used libraries. further dependencies follow , @ end 1 has add low level dependencies (used boost).
in case, when want respect order link libraries, use add_dependencies cmake command. mean:
find_package(boost components date_time filesystem system ...) find_package(postgresql required) if(boost_found , postgresql_found) include_directories(${boost_include_dirs}) include_directories(${postgresql_include_dirs}) # applicationname -> your_application # lib_a_target -> liba # lib_b_target -> libb set(lib_a_all_deps ${boost_libraries}) set(lib_b_all_deps ${lib_a_all_deps} ${postgresql_libraries}) set(extra_app_deps ) # here libs aren't in boost or postgre set(your_app_all_deps ${lib_a_all_deps} ${lib_b_all_deps} ${extra_app_deps}) # here'll add_executable, add_library code target_link_libraries(${lib_a_target} ${lib_a_all_deps}) add_dependencies(${lib_a_target} ${lib_a_all_deps}) target_link_libraries(${lib_b_target} ${lib_b_all_deps}) add_dependencies(${lib_b_target} ${lib_b_all_deps}) target_link_libraries(${applicationname} ${your_app_all_deps}) add_dependencies(${applicationname} ${your_app_all_deps}) endif()
i've not tested code i'll more or less. know example part of possible cmakelists.txt need see code writing entirely.
i hope helps you!
Comments
Post a Comment