2
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON)

set(source_files
    a.cpp
    b.cpp
    ...
   )

set(header_files
    a.hpp
    b.hpp
    ...
   )

set(Qt_libs
    Qt5::Core
    Qt5::Gui
    Qt5::Widget
    ...
   )

add_library(demo SHARED ${header_files} ${source_files})
target_link_libraries(demo ${Qt_libs} ...)
set_properties(TARGET demo PROPERTY FOLDER "somewhere")
install(...)

I have a sample CMakeLists.txt shows above.

The most weird thing is, it won't generate those moc files until I manually modified (like adding a empty line to the file) those header files (where Q_OBJECT presents).

The situation not happen every time. But, once it happens, clean build nor deleting whole project file won't help.

I'm using qt 5.11, CMake 3.7, Visual Studio 2015.

Ducksoul
  • 21
  • 4
  • I have problems with `auto_moc` as well (as a lot of others might tell you). I always run a `cmake configure` after adding a new file which needs `moc` – Amfasis Nov 23 '18 at 13:09
  • I had similar problem and could not manage to solve it. So I decided to use the qt5_generate_moc() macro – ni1ight Nov 23 '18 at 13:18
  • https://stackoverflow.com/questions/37151163/cmake-automoc-with-files-on-different-folders/37174028#37174028 – Mac Nov 24 '18 at 00:52
  • @Mac I have both header and source files added to the target. It seems not produced by handling CMake file wrong because the problem happens only sometimes. – Ducksoul Nov 24 '18 at 11:35
  • did u try adding the headers to the solution as described there? – Mac Nov 25 '18 at 20:20
  • @Mac Yes, I did – Ducksoul Nov 26 '18 at 13:57

1 Answers1

0

You are setting the global setting with set() which could be overwritten. Please use set_target_properties, for example

project(exampleProj)
add_executable(exampleProj main.cpp)
set_target_properties(exampleProj
   PROPERTIES
     CMAKE_INCLUDE_CURRENT_DIR ON
     CMAKE_AUTOMOC ON)
  • Thank you for the answer, I will try it and posting result later. – Ducksoul Nov 26 '18 at 14:02
  • @Ducksoul if you do not have success with this answer, consider updating your question with the contents of your CMakelists.txt file so that the answer may be filled with more context – brettmichaelgreen Nov 26 '18 at 14:09
  • this method seems not solving the problem after a few tries. And my Cmake file is pretty much the same as https://stackoverflow.com/questions/37151163/cmake-automoc-with-files-on-different-folders?noredirect=1&lq=1 – Ducksoul Nov 28 '18 at 14:46
  • @Ducksoul I need your snippet so I can see if set_target_properties was used on a valid target . Excluding filenames of your sources would still produce a minimum, verifiable, example if the contents are sensitive – brettmichaelgreen Nov 28 '18 at 17:25
  • @Ducksoul do you have #include "a.moc" appended at the bottom of your a.cpp file (assuming a is the name of the Q_OBJECT) ? If not can you try doing that? I reread your question and I am looking at "it won't generate those moc files until I manually modified" – brettmichaelgreen Nov 30 '18 at 02:44
  • I tried `a.moc` in source file before with no luck. The only solution is modified those file where `Q_OBJECT` exists. I currently use a pre-build refresh script for those files as a temporary solution. – Ducksoul Dec 02 '18 at 12:52