0

I have a following cmake file

cmake_minimum_required(VERSION 2.6) # Locate GTest find_package(GTest REQUIRED) include_directories(${GTEST_INCLUDE_DIRS}) include_directories(../src/gcc_xml_parsing) file(GLOB info_model "../src/info_model/*.h" "../src/info_model/*.cpp" "../src/gcc_xml_parsing/*.h" "../src/gcc_xml_parsing/*.cpp" "../src/messages_filed_with_values/*.h" "../src/messages_filed_with_values/*.cpp" ) # Link runTests with what we want to test and the GTest and pthread library add_executable(runTests_xml unit/gcc_xml_parsing/ut_XMLFile.cpp ${info_model} ) add_executable(runTests_hexDumpUtil unit/info_model/ut_HexDumpUtil.cpp ${info_model} ) add_executable(runTests_cstruct unit/info_model/ut_CStruct.cpp ${info_model}) add_executable(runTests_primitive_type_field unit/info_model /ut_PrimitiveTypeField.cpp ${info_model}) add_executable(runTests_enumField unit/info_model/ut_EnumField.cpp ${info_model}) add_executable(runTests_ArrayOfFields unit/info_model/ut_ArrayType.cpp ${info_model}) target_link_libraries(runTests_xml ${GTEST_LIBRARIES} ${GTEST_MAIN_LIBRARIES} pthread boost_regex) target_link_libraries(runTests_hexDumpUtil ${GTEST_LIBRARIES} ${GTEST_MAIN_LIBRARIES} pthread boost_regex ) target_link_libraries(runTests_cstruct ${GTEST_LIBRARIES} ${GTEST_MAIN_LIBRARIES} pthread boost_regex ) target_link_libraries(runTests_primitive_type_field ${GTEST_LIBRARIES} ${GTEST_MAIN_LIBRARIES} pthread boost_regex ) target_link_libraries(runTests_enumField ${GTEST_LIBRARIES} ${GTEST_MAIN_LIBRARIES} pthread boost_regex ) target_link_libraries(runTests_ArrayOfFields ${GTEST_LIBRARIES} ${GTEST_MAIN_LIBRARIES} pthread boost_regex ) 

In general,after making and running a MakeFile the program compiles fine. The problem is, that the make file builds the source from ${info_model} for every add_executable ( the makefile is building all *.o files for every executable. How to fix it ? Thanks.

Edit: After making changes in my cmake (according to the first answer in this post ), my application compiles fine, but has linkage problems. The whole log is large, thus I am pasting the only first part:

Linking CXX executable runTests_ArrayOfFields libinfo_model_lib.a(XMLFile.cpp.o): In function `bool boost::regex_search<__gnu_cxx::__normal_iterator<char const*, std::string>, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const* .... 

1 Answer 1

1

You can move those files to a static library:

add_library(info_model_lib STATIC ${info_model}) add_executable(runTests_xml unit/gcc_xml_parsing/ut_XMLFile.cpp) [...] target_link_libraries(runTests_xml ${GTEST_LIBRARIES} ${GTEST_MAIN_LIBRARIES} pthread boost_regex info_model_lib) [...] 

This way the source files will only be compiled once (as part of building the info_model_lib target) and will then be linked to each executable. The resulting binary will look almost the same. Depending on the compiler used you may lose some optimizations due to this change, but usually it is not enough to give a measurable decrease in performance.

Sign up to request clarification or add additional context in comments.

2 Comments

Hi, I have updated my cmake, but still have problems during linkage. Please take a look at my edit above. Thank you
@friko This is a different and unrelated issue. You should create a new question describing the linker issue in detail. Also, the error message you provided is truncated, so it's impossible to see which symbol is actually missing.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.