I am using Qt 5.12.12 and CMake 3.16. I have a project structured like so:
src/
├─ include/
│ ├─ subfolder/
│ │ ├─ a.h
├─ CMakeLists.txt
├─ a.cpp
My CMakeLists.txt
file contains:
cmake_minimum_required(VERSION 3.16)
project(test LANGUAGES CXX)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(Qt5 REQUIRED COMPONENTS Core)
add_library(test SHARED ${CMAKE_CURRENT_SOURCE_DIR}/include/subfolder/a.h ${CMAKE_CURRENT_SOURCE_DIR}/a.cpp)
target_link_libraries(test PRIVATE Qt5::Core)
target_include_directories(test PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)
Inside a.cpp
, I have at the bottom the line #include "moc_a.cpp"
.
CMake's documentation seems to imply that this is fine, since I have explicitly added the header file as a source file.
However, I am given the error:
AutoMoc error
-------------
"SRC:/a.cpp"
includes the moc file "moc_a.cpp",
but a header "a.{h,hh,h++,hm,hpp,hxx,in,txx}"
could not be found in the following directories
"SRC:"
"SRC:/include"
...
If I remove the #include "moc_a.cpp"
statement, everything runs fine, MOC creates the appropriate files in the autogen folder, and my project works. If I explicitly include the folder include/subfolder
, it also works.
I know this include is not necessary when using CMake, but I am transitioning a project from QMake and would prefer to not modify things I don't need to. Is this convention only possible with explicitly included directories? I thought CMake would generate the moc_a.cpp
file inside of the include
folder in the autogen output directory and add that include
folder to the include path, but the error message tells me that it's not scanning the list of header sources at all.