C++ CMake与QT多重定义

C++ CMake与QT多重定义,c++,qt,header,cmake,multiple-definition-error,C++,Qt,Header,Cmake,Multiple Definition Error,我正在使用Qt和Cmake进行一个项目,我是这两方面的新手,所以请容忍我 我有两个使用Qt的类,一个名为MapMaker,另一个名为ImageLabelMapMaker继承自QMainWindow和ImageLabel自QLabel Imagelabel拥有一个类作为属性,我将其命名为Map,该Map拥有一个方法AddReshibler。MapMaker拥有imageLabel,imageLabel拥有地图 Map的每个方法都位于Map.hpp文件中,而MapMaker和ImageLabel方法

我正在使用Qt和Cmake进行一个项目,我是这两方面的新手,所以请容忍我

我有两个使用Qt的类,一个名为
MapMaker
,另一个名为
ImageLabel
MapMaker
继承自
QMainWindow
ImageLabel
QLabel

Imagelabel拥有一个类作为属性,我将其命名为Map,该Map拥有一个方法AddReshibler。MapMaker拥有imageLabel,imageLabel拥有地图

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
定义
(头保护),它们是不同的

有趣的是,如果我
内联
方法
添加障碍
,那么错误就会消失

这是我的名片:

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部分有关(一些我没有做过或不知道的项目配置),但我不知道怎么做

据我所知,引发多定义错误的唯一错误是标题保护中的错误。这里还有什么其他原因

我知道这是一个相当棘手的问题,请告诉我我应该提供哪些信息来帮助你

仅保护在同一文件中多次包含同一头文件(包含所有头文件的源文件)。它不保护不同翻译单元中的多个定义


要么将函数定义内联(如果这样做有意义的话),要么将它们移动到自己的源文件中。

内联一切都会更容易,不是吗?@Malc请记住,
inline
只是对编译器的一个提示,编译器可以忽略它(在这种情况下,函数不是内联的,但在每个翻译单元中仍然是本地的,并且没有导出)。此外,创建函数只对可以内联的函数有意义,例如循环中的小函数、简单的getter或setter函数等。更大或更复杂的函数应该放在单独的源文件中。非常感谢所有清晰的解释。这真的很有帮助!