AUTOMOC causes generated files to not be found
cmake_minimum_required(VERSION 3.14)
project(testAutomoc)
find_package(Qt5Core REQUIRED)
file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/foo.cpp)
file(GENERATE OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/myConfig.h
CONTENT
"
//----
"
)
add_executable(testTarget
foo.cpp
${CMAKE_CURRENT_BINARY_DIR}/myConfig.h
)
# Comment this to make the build work:
set_property(TARGET testTarget PROPERTY AUTOMOC ON)
Cmake output:
$ cmake .. -G Ninja
-- The C compiler identification is GNU 7.4.0
-- The CXX compiler identification is GNU 7.4.0
-- Check for working C compiler: /usr/lib/ccache/cc
-- Check for working C compiler: /usr/lib/ccache/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/lib/ccache/c++
-- Check for working CXX compiler: /usr/lib/ccache/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
CMake Error at CMakeLists.txt:17 (add_executable):
Cannot find source file:
/home/stephen/dev/src/playground/cmake/build/myConfig.h
Tried extensions .c .C .c++ .cc .cpp .cxx .cu .m .M .mm .h .hh .h++ .hm
.hpp .hxx .in .txx
-- Generating done
CMake Generate step failed. Build files cannot be regenerated correctly.
Note that the file was generated:
$ ls
CMakeCache.txt CMakeFiles cmake_install.cmake foo.cpp myConfig.h
So, re-running cmake results in success:
$ cmake .
-- Configuring done
-- Generating done
-- Build files have been written to: /home/stephen/dev/src/playground/cmake/build
Every version of CMake I tested (since 3.0.0
) is affected. 2.8.12
also fails, but does not generate the file.
This workaround seems to work:
set_property(SOURCE ${CMAKE_CURRENT_BINARY_DIR}/myConfig.h PROPERTY GENERATED TRUE)
set_property(SOURCE ${CMAKE_CURRENT_BINARY_DIR}/myConfig.h PROPERTY SKIP_AUTOMOC TRUE)
Edited by Stephen Kelly