我的CMakeLists.txt看起来像:
cmake_minimum_required(VERSION 2.8)
if(CMAKE_BUILD_TYPE STREQUAL Release)
SET(CMAKE_BUILD_TYPE Release)
SET (PROJECT_NAME location_recognition)
message("Release mode")
else()
SET(CMAKE_BUILD_TYPE Debug)
SET (PROJECT_NAME location_recognition)
SET(CMAKE_CXX_FILES "-g -Wall")
message("Debug mode")
endif()
#find QT libraries
find_package(Qt5Widgets REQUIRED)
find_package(Qt5Core REQUIRED)
find_package(Qt5Gui REQUIRED)
FIND_PACKAGE(OpenCV REQUIRED)
FIND_PACKAGE(Boost REQUIRED)
# Instruct CMake to run moc automatically when needed.
set(CMAKE_AUTOMOC ON)
# As moc files are generated in the binary dir, tell CMake
# to always look for includes there:
set(CMAKE_INCLUDE_CURRENT_DIR ON)
INCLUDE_DIRECTORIES( ${CMAKE_CURRENT_BINARY_DIR} )
# We need add -DQT_WIDGETS_LIB when using …Run Code Online (Sandbox Code Playgroud) 稍微搞砸了(以及对生成的Makefile的一些编辑),看起来正在发生的是moc没有正确处理MainWindow.h(包括在内main.cpp,MainWindow.cpp除非它与包含它的源文件位于同一文件夹中).
Moc运行MainWindow.cpp,不处理包含,因此没有看到Q_OBJECT宏,因此继续产生一个空的输出文件.我不确定moc通常是进程包含还是只是扫描目录,但无论哪种方式,需要mocing但在其他目录中的头文件都没有被处理!
问题似乎与moc产生的输出有关.在第一种情况下(编译的那个),hello-world_automoc.cpp并moc_MainWindow.cpp生成.hello-world_automoc.cpp好像
/* This file is autogenerated, do not edit*/
#include "moc_MainWindow.cpp"
Run Code Online (Sandbox Code Playgroud)
在第二种情况下,hello-world_automoc.cpp产生了一个看起来像的
/* This file is autogenerated, do not edit*/
enum some_compilers { need_more_than_nothing };
Run Code Online (Sandbox Code Playgroud)
并且根本没有moc_MainWindow.cpp.如果我从cmake手动调用moc而不是在破碎的情况下使用automoc,我确实得到moc_MainWindow.cpp但它是空的.
首先,不,我没有忘记set(CMAKE_AUTOMOC ON).还要注意的是MainWindow的析构函数声明和实现.
当我的目录结构如下所示:
CMakeLists.txt |__ main.cpp |__ MainWindow.cpp |__ MainWindow.h |__ MainWindow.ui
编译工作得很好.
但是,当它看起来像:
helloworld/ |__ CMakeLists.txt |__ src/ | |__ CMakeLists.txt | …