mingw32-make与“ MinGW Makefiles”生成器一起执行以下命令无法将可执行文件链接到对象库

问题描述 投票:0回答:1

我正在使用CMake来促进C ++项目的跨平台构建。使用相同的CMakeLists.txt文件,我成功完成了Linux(生成Unix Makefile以便使用g ++编译),OSX(生成Unix Makefile以便使用clang ++编译)和Windows(针对Visual Studio 16 2019生成)的构建。

最近,我一直在使用Windows上的MinGW Makefiles生成器来测试构建过程。 CMake生成Makefile没有任何问题。但是,当我在生成Makefile的目录中调用mingw32-make时,我开始遇到问题。我可以将几个源文件编译到一个对象库中,但是当我尝试将print_graphs可执行文件链接到该对象库时,我在对象库的第一个源文件中遇到了方法泛滥的multiple definition错误,例如如下:

Scanning dependencies of target print_graphs
[ 52%] Building CXX object src/CMakeFiles/print_graphs.dir/print_graphs.cpp.obj
[ 57%] Linking CXX executable print_graphs.exe
CMakeFiles\print_graphs.dir/objects.a(roaring.c.obj):roaring.c:(.text+0x6833): multiple definition of `roaring_bitmap_get_copy_on_write'
CMakeFiles\print_graphs.dir/objects.a(print_graphs.cpp.obj):print_graphs.cpp:(.text$roaring_bitmap_get_copy_on_write[roaring_bitmap_get_copy_on_write]+0x0): first defined here
CMakeFiles\print_graphs.dir/objects.a(roaring.c.obj):roaring.c:(.text+0x6850): multiple definition of `roaring_bitmap_set_copy_on_write'
CMakeFiles\print_graphs.dir/objects.a(print_graphs.cpp.obj):print_graphs.cpp:(.text$roaring_bitmap_set_copy_on_write[roaring_bitmap_set_copy_on_write]+0x0): first defined here
CMakeFiles\print_graphs.dir/objects.a(set_cover_solver.cpp.obj):set_cover_solver.cpp:(.text$ra_get_index[ra_get_index]+0x0): multiple definition of `ra_get_index'
CMakeFiles\print_graphs.dir/objects.a(roaring.c.obj):roaring.c:(.text+0x65a1): first defined here
CMakeFiles\print_graphs.dir/objects.a(set_cover_solver.cpp.obj):set_cover_solver.cpp:(.text$ra_get_container_at_index[ra_get_container_at_index]+0x0): multiple definition of `ra_get_container_at_index'
CMakeFiles\print_graphs.dir/objects.a(roaring.c.obj):roaring.c:(.text+0x660e): first defined here
CMakeFiles\print_graphs.dir/objects.a(set_cover_solver.cpp.obj):set_cover_solver.cpp:(.text$roaring_bitmap_add_range[roaring_bitmap_add_range]+0x0): multiple definition of `roaring_bitmap_add_range'
CMakeFiles\print_graphs.dir/objects.a(roaring.c.obj):roaring.c:(.text+0x688e): first defined here
CMakeFiles\print_graphs.dir/objects.a(set_cover_solver.cpp.obj):set_cover_solver.cpp:(.text$roaring_bitmap_contains[roaring_bitmap_contains]+0x0): multiple definition of `roaring_bitmap_contains'
CMakeFiles\print_graphs.dir/objects.a(roaring.c.obj):roaring.c:(.text+0x690a): first defined here
collect2.exe: error: ld returned 1 exit status
mingw32-make[2]: *** [src\CMakeFiles\print_graphs.dir\build.make:104: src/print_graphs.exe] Error 1
mingw32-make[1]: *** [CMakeFiles\Makefile2:933: src/CMakeFiles/print_graphs.dir/all] Error 2
mingw32-make: *** [Makefile:94: all] Error 2

在我的src /目录的CMakeLists.txt文件中,我具有以下基本代码:

# Add header files:
set(HEADERS
    ${HEADERS_DIR}/cxxopts.h
    ${HEADERS_DIR}/roaring.h
    ${HEADERS_DIR}/roaring.hh
    ${HEADERS_DIR}/pugiconfig.h
    ${HEADERS_DIR}/pugixml.h
    ...
)

# Add object source files:
set(SOURCES 
    roaring.c
    pugixml.cpp
    ...
)

# Combine the object source files into an object library:
add_library(objects OBJECT ${SOURCES} ${HEADERS})

# Point the build targets to their include directories:
target_include_directories(objects PRIVATE ${HEADERS_DIR})

# Add all executable scripts to be generated:
add_executable(program1 $<TARGET_OBJECTS:objects> program1.cpp )

# Point the build targets to their include directories:
target_include_directories(program1 PRIVATE ${HEADERS_DIR})

# Make sure the build targets are compiled with C++11:
target_compile_features(program1 PRIVATE cxx_std_11)

我是CMake的新手,所以我敢打赌,问题在我的CMakeLists.txt文件中是不好的形式。但是由于当我使用其他生成器时CMake能够构建一切而不会出现问题,因此我不确定是什么问题。有人知道特别是MinGW Makefiles生成器可能引起问题的原因吗?

c++ windows makefile cmake mingw
1个回答
0
投票

[好的,我发现了问题,并且正如我所料,这对CMake来说是不好的形式。在我的代码部分中,带有

# Combine the object source files into an object library:
add_library(objects OBJECT ${SOURCES} ${HEADERS})

没有必要引用${HEADERS},因为该连接在接下来的几行中已明确建立:

# Point the build targets to their include directories:
target_include_directories(objects PRIVATE ${HEADERS_DIR})

[在初始add_library调用中包含对头文件的引用似乎导致头文件(或至少对它们的引用)的重复。

[我仍然不确定为什么mingw32-make时,其他构建工具不会因此问题而被绊倒,但是无论如何都应保持适当的语法,并且现在按预期运行。

© www.soinside.com 2019 - 2024. All rights reserved.