我正在使用Qt和Cmake进行项目,并且我是这两个方面的新手,所以请多多包涵。

我有两个使用Qt的类,一个名为MapMaker,另一个名为ImageLabel。从MapMaker继承的QMainWindow和从ImageLabel继承的QLabel
Imagelabel拥有一个Class作为属性,我将Map命名为拥有方法addObstacle的Map。 MapMaker拥有imageLabel,而ImageLabel拥有 map 。

Map的每个方法都在Map.hpp文件中,而MapMaker和ImageLabel方法都在.cpp文件中。

编译时出现以下错误:

CMakeFiles/mapmakerv3.dir/includes/Qtfiles/MapMakerv3.cpp.o: In function `Map::drawObstacle(int, int, bool)':
/home/malcolm/AASS/sketch_maker/includes/MapInterpretation/Map.hpp:431: multiple definition of `Map::addObstacle(int, int, bool)'
CMakeFiles/mapmakerv3.dir/main.cpp.o:/home/malcolm/AASS/sketch_maker/includes/MapInterpretation/Map.hpp:431: first defined here
CMakeFiles/mapmakerv3.dir/includes/Qtfiles/imageLabel.cpp.o: In function `Map::drawObstacle(int, int, bool)':
/home/malcolm/AASS/sketch_maker/includes/MapInterpretation/Map.hpp:431: multiple definition of `Map::addObstacle(int, int, bool)'
CMakeFiles/mapmakerv3.dir/main.cpp.o:/home/malcolm/AASS/sketch_maker/includes/MapInterpretation/Map.hpp:431: first defined here
CMakeFiles/mapmakerv3.dir/mapmakerv3_automoc.cpp.o: In function `Map::drawObstacle(int, int, bool)':
/home/malcolm/AASS/sketch_maker/includes/MapInterpretation/Map.hpp:431: multiple definition of `Map::addObstacle(int, int, bool)'
CMakeFiles/mapmakerv3.dir/main.cpp.o:/home/malcolm/AASS/sketch_maker/includes/MapInterpretation/Map.hpp:431: first defined here
collect2: error: ld returned 1 exit status
CMakeFiles/mapmakerv3.dir/build.make:187: recipe for target 'mapmakerv3' failed
make[2]: *** [mapmakerv3] Error 1
CMakeFiles/Makefile2:60: recipe for target 'CMakeFiles/mapmakerv3.dir/all' failed
make[1]: *** [CMakeFiles/mapmakerv3.dir/all] Error 2
Makefile:76: recipe for target 'all' failed
make: *** [all] Error 2

我在每个头文件的开头仔细检查了所有的ifndef define(头保护),它们并不相同。

有趣的是,如果我使用inline方法addObstacle,则错误消失。

这是我的Cmake:
cmake_minimum_required(VERSION 2.6)
project(mapmakerv3)

set (MapMaker_VERSION_MAJOR 1)
set (MapMaker_VERSION_MINOR 0)

find_package(Qt4 REQUIRED)
find_package( OpenCV REQUIRED)

find_package( Boost REQUIRED )

if ( NOT Boost_FOUND )
  message(STATUS "This project requires the Boost library, and will not be compiled.")
  return()
endif()

include_directories("${PROJECT_BINARY_DIR}" ${QT_INCLUDES} ${CMAKE_CURRENT_BINARY_DIR}  includes includes/Qtfiles includes/MapInterpretation/ includes/TopologicalMap/ includes/MapComparator/ Test/Ddata/)

set(CMAKE_AUTOMOC TRUE)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
add_executable(mapmakerv3 main.cpp includes/Qtfiles/MapMaker.cpp includes/Qtfiles/imageLabel.cpp)
target_link_libraries(mapmakerv3 ${QT_QTCORE_LIBRARY} ${QT_QTGUI_LIBRARY} ${OpenCV_LIBS})

install(TARGETS mapmakerv3 RUNTIME DESTINATION bin)
add_subdirectory(includes)

我尝试编译没有Qt依赖文件(没有ImageLabel或MapMaker)并且只有我制作的一些自定义类的测试文件,并且编译正常。我的猜测是该错误与Qt部分有关(我尚未完成或我不知道的某些项目配置),但我不知道如何做。

我知道的唯一引发multiple definition错误的错误是 header 防护中的错误。还有什么可能是这里的原因?

我知道目前这是一个非常麻烦的问题,只需告诉我应该提供哪些信息即可提供帮助。

最佳答案

Include guards仅保护在同一translation unit(具有所有头的源文件)中多次包含同一头文件。它不能防止不同翻译单元中的多个定义。

可以将函数定义设为inline(如果可以的话),也可以将它们移到自己的源文件中。

07-27 14:04