CMake和QT多重定义

时间:2015-07-27 16:43:28

标签: c++ qt header cmake multiple-definition-error

我正在使用Qt和Cmake进行项目,我是这两件事的新手,所以请耐心等待。

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

ImageLabelQLabel

Imagelabel拥有一个Class as属性,我将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错误是标头警卫中的一个错误。还有什么可能是这个原因?

我知道现在这个问题非常混乱,只需告诉我应该提供哪些信息来提供帮助。

1 个答案:

答案 0 :(得分:3)

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

制作函数定义inline(如果这样做有意义)或将它们移动到自己的源文件中。