CMake和QT多重定义
CMake and QT multiple definition
我正在为一个项目使用 Qt 和 Cmake,我是这两个方面的新手,所以请多多包涵。
我有两个 class 使用 Qt,一个名为 MapMaker
,另一个名为 ImageLabel
。 MapMaker
继承自 QMainWindow
,ImageLabel
继承自 QLabel
Imagelabel 有一个 Class 作为我命名为 Map 的属性,它有一个方法 addObstacle。 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
我在每个 header 文件的开头仔细检查了我所有的 ifndef
define
(header 守卫),它们不一样。
有趣的是,如果我 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-dependant 文件(没有 ImageLabel 也没有 MapMaker)的测试文件,只有我制作的一些自定义 class 并且编译正常。我的猜测是该错误与 Qt 部分有关(一些项目配置我没有完成或我不知道)但我不知道如何。
我知道的唯一抛出 multiple definition
错误的错误是 header 守卫中的一个错误。还有什么可能是这里的原因?
我知道现在这是一个非常混乱的问题,请告诉我我应该提供哪些信息来提供帮助。
Include guards only protects for multiple inclusion of the same header file in the same translation unit(全部headers的源文件)。它不能防止不同翻译单元中的多个定义。
要么定义函数 inline
(如果这样做有意义),要么将它们移到自己的源文件中。
我正在为一个项目使用 Qt 和 Cmake,我是这两个方面的新手,所以请多多包涵。
我有两个 class 使用 Qt,一个名为 MapMaker
,另一个名为 ImageLabel
。 MapMaker
继承自 QMainWindow
,ImageLabel
继承自 QLabel
Imagelabel 有一个 Class 作为我命名为 Map 的属性,它有一个方法 addObstacle。 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
我在每个 header 文件的开头仔细检查了我所有的 ifndef
define
(header 守卫),它们不一样。
有趣的是,如果我 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-dependant 文件(没有 ImageLabel 也没有 MapMaker)的测试文件,只有我制作的一些自定义 class 并且编译正常。我的猜测是该错误与 Qt 部分有关(一些项目配置我没有完成或我不知道)但我不知道如何。
我知道的唯一抛出 multiple definition
错误的错误是 header 守卫中的一个错误。还有什么可能是这里的原因?
我知道现在这是一个非常混乱的问题,请告诉我我应该提供哪些信息来提供帮助。
Include guards only protects for multiple inclusion of the same header file in the same translation unit(全部headers的源文件)。它不能防止不同翻译单元中的多个定义。
要么定义函数 inline
(如果这样做有意义),要么将它们移到自己的源文件中。