Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
cmake中的多源构建_Cmake - Fatal编程技术网

cmake中的多源构建

cmake中的多源构建,cmake,Cmake,我有一个下面的cmake文件 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/

我有一个下面的cmake文件

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 )
通常,在生成并运行MakeFile之后,程序可以很好地编译。问题是,make文件为每个add_可执行文件从${info_model}构建源代码(make文件为每个可执行文件构建所有*.o文件。如何修复它?谢谢

编辑: 在对我的cmake进行更改后(根据本文的第一个答案),我的应用程序编译得很好,但存在链接问题。整个日志很大,因此我只粘贴第一部分:

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* ....
链接CXX可执行运行测试\u数组字段

libinfo_model_lib.a(XMLFile.cpp.o):在函数`bool boost::regex_search中,可以将这些文件移动到静态库中:

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)
[...]

这样,源文件将只编译一次(作为构建
info\u model\u lib
目标的一部分)然后将链接到每个可执行文件。生成的二进制文件看起来几乎相同。根据所使用的编译器,您可能会因为此更改而丢失一些优化,但通常不足以显著降低性能。

您好,我已更新了我的cmake,但在链接过程中仍然存在问题。请查看我的e上面是dit,谢谢you@friko这是一个不同且不相关的问题。您应该创建一个新问题,详细描述链接器问题。此外,您提供的错误消息被截断,因此无法查看实际缺少的符号。