I have a project which uses Qt5 and I have a CMakeLists.txt
file that I use for creating the Visual Studio Solution.
This is an excerpt of my CMakeLists.txt
cmake_policy(SET CMP0020 NEW)
set(CMAKE_AUTOMOC ON)
find_package(Qt5 REQUIRED COMPONENTS core widgets)
set(COMMON_INCLUDE_DIR ${PROJECT_SOURCE_DIR}/src)
include_directories( ${Boost_INCLUDE_DIRS}
${COMMON_INCLUDE_DIR}
)
file(GLOB_RECURSE COMMON_SOURCE "*.hpp" "*.cpp")
add_library(${PROJECT_NAME} ${COMMON_SOURCE})
qt5_use_modules(${PROJECT_NAME} Widgets)
When I try to compile the code it returns the following error:
>AUTOMOC : error : C:/Users/.../Projects/MyProject/build/MyProjects_automoc.cpp The file includes the moc file "moc_MyFile.cpp", but could not find header "MyFile{.h,.hh,.h++,.hm,.hpp,.hxx,.in,.txx}" in C:/Users/.../Projects/MyProject/build/
The moc file have been auto-generated and the header is not in the build folder, but in a folder locate in the src directory.
How is possible to fix this error?
It is good to add:
when using the
AUTOMOC
feature. Furthermore, thisis a mistake. It should rather be:
Finally, you should only explicitly push sources to compilation, but not headers! As stated in the documentation:
If
Q_OBJECT
is in thefoo.h
(i.e.QObject
is declared in the header file), then in the correspondingfoo.cpp
don't forget to add#include "moc_foo.cpp"
, preferably at the end of the file;If
Q_OBJECT
is in thefoo.cpp
(i.e.QObject
is declared in the source file), then, again, in thefoo.cpp
itself don't forget to add#include "foo.moc"
, preferably at the end of the file.Therefore, follow these recommendations and change
to
You could also find my other answer helpful. Your question is very similar, so I'd recommend to search better before posting next time.
Good luck.