MantisBT - CMake
View Issue Details
0014535CMake(No Category)public2013-10-31 14:472014-06-02 08:37
Janne Rönkkö 
Stephen Kelly 
normalmajorhave not tried
closedfixed 
LinuxArchlinux
CMake 2.8.12 
CMake 2.8.12.1CMake 2.8.12.1 
0014535: Definitions Not Defined In Qt Automoc
With CMake 2.8.12 (and with tested git version from commit 8a6e82724c42920855c2348e914636f52a0c55d5) automoc does not set definitions for moc properly.

With CMake 2.8.11.2 this problem does not exist.

I have created example project that demonstrates the issue.

With CMake 2.8.12 (or with the git version I tried) the plugin is not created properly because QT5 define is not set while moc runs.
$ tar xf example.tar.gz
$ mkdir example/build
$ cd example/build

$ cmake --version
cmake version 2.8.11.2

$ cmake .. -DUSE_AUTOMOC=false
-- The C compiler identification is GNU 4.8.2
-- The CXX compiler identification is GNU 4.8.2
...
-- Found Qt4: /usr/bin/qmake-qt4 (found version "4.8.5")
Not using automoc
MocSrc /home/janne/tmp/example/build/moc_PluginImpl.cxx
-- Configuring done
-- Generating done
-- Build files have been written to: /home/janne/tmp/example/build

$ make

$ ./loadplugin libtestplugin.so
Loading plugin from "libtestplugin.so"
fileName(): "/home/janne/tmp/example/build/libtestplugin.so"
isLoaded(): false
err: "Unknown error"
Success!

$ cmake .. -DUSE_AUTOMOC=true
-- The C compiler identification is GNU 4.8.2
-- The CXX compiler identification is GNU 4.8.2
...
-- Found Qt4: /usr/bin/qmake-qt4 (found version "4.8.5")
Using automoc
-- Configuring done
-- Generating done
-- Build files have been written to: /home/janne/tmp/example/build

$ make
                                                                                                                                                                                                                                                                               
$ ./loadplugin libtestplugin.so
Loading plugin from "libtestplugin.so"
fileName(): "/home/janne/tmp/example/build/libtestplugin.so"
isLoaded(): false
err: "Unknown error"
Success!
                                                                                                                                                                                                                                                                               
                                                                                                                                                                                                                                                                               
$ cmake --version
cmake version 2.8.12
                                                                                                                                                                                                                                                                               
                                                                                                                                                                                                                                                                               
$ cmake .. -DUSE_AUTOMOC=false
-- The C compiler identification is GNU 4.8.2
-- The CXX compiler identification is GNU 4.8.2
-- Found Qt4: /usr/bin/qmake-qt4 (found version "4.8.5")
Not using automoc
MocSrc /home/janne/tmp/example/build/moc_PluginImpl.cxx
-- Configuring done
-- Generating done
-- Build files have been written to: /home/janne/tmp/example/build
                                                                                                                                                                                                                                                                               
$ make
                                                                                                                                                                                                                                                                               
$ ./loadplugin libtestplugin.so
Loading plugin from "libtestplugin.so"
fileName(): "/home/janne/tmp/example/build/libtestplugin.so"
isLoaded(): false
err: "Unknown error"
Success!
                                                                                                                                                                                                                                                                               
                                                                                                                                                                                                                                                                               
$ cmake .. -DUSE_AUTOMOC=true
-- The C compiler identification is GNU 4.8.2
-- The CXX compiler identification is GNU 4.8.2
-- Found Qt4: /usr/bin/qmake-qt4 (found version "4.8.5")
Using automoc
-- Configuring done
-- Generating done
-- Build files have been written to: /home/janne/tmp/example/build
                                                                                                                                                                                                                                                                               
$ make

$ ./loadplugin libtestplugin.so
Loading plugin from "libtestplugin.so"
fileName(): "/home/janne/tmp/example/build/libtestplugin.so"
isLoaded(): false
err: "Unknown error"
!! No plugin found.
The issue exists with Qt4 and Qt5.
No tags attached.
gz example.tar.gz (2,525) 2013-10-31 14:47
https://public.kitware.com/Bug/file/4935/example.tar.gz
Issue History
2013-10-31 14:47Janne RönkköNew Issue
2013-10-31 14:47Janne RönkköFile Added: example.tar.gz
2013-11-02 08:59Stephen KellyAssigned To => Stephen Kelly
2013-11-02 08:59Stephen KellyStatusnew => assigned
2013-11-02 08:59Stephen KellyNote Added: 0034311
2013-11-02 09:00Stephen KellyStatusassigned => feedback
2013-11-02 13:32Janne RönkköNote Added: 0034335
2013-11-02 13:32Janne RönkköStatusfeedback => assigned
2013-11-03 15:48Stephen KellyNote Added: 0034338
2013-11-03 21:31Jason HaslamNote Added: 0034339
2013-11-04 05:46Stephen KellyNote Added: 0034340
2013-11-05 11:20Brad KingNote Added: 0034394
2013-11-05 11:40Brad KingNote Added: 0034395
2013-11-05 11:47Brad KingTarget Version => CMake 2.8.12.1
2013-11-05 12:07Brad KingNote Added: 0034397
2013-11-05 13:36Brad KingStatusassigned => resolved
2013-11-05 13:36Brad KingResolutionopen => fixed
2013-11-05 13:36Brad KingFixed in Version => CMake 2.8.12.1
2014-06-02 08:37Robert MaynardNote Added: 0036005
2014-06-02 08:37Robert MaynardStatusresolved => closed

Notes
(0034311)
Stephen Kelly   
2013-11-02 08:59   
Your description is confusing.

Please describe the problem in terms of the moc command line, if that's where the problem is.
(0034335)
Janne Rönkkö   
2013-11-02 13:32   
With CMake 2.8.11.2 the automoc command that is run is (in my case):
/opt/cmake-2.8.11.2/bin/cmake -E cmake_automoc /home/janne/tmp/cmake-qt-automoc-defines-bug/cmake-2.8.11.2/CMakeFiles/testplugin_automoc.dir/

And with CMake 2.8.12:
/usr/bin/cmake -E cmake_automoc /home/janne/tmp/cmake-qt-automoc-defines-bug/cmake-2.8.12/CMakeFiles/testplugin_automoc.dir/ ""

In the directory that is given to automoc command (....CMakeFiles/testplugin_automoc.dir) there is a file AutomocInfo.cmake where is the difference causing the issue I'm seeing:

In the CMake 2.8.12 build directory the file contains the following line:
set(AM_MOC_COMPILE_DEFINITIONS "")

And in the CMake 2.8.11.2 build directory the corresponding line is:
set(AM_MOC_COMPILE_DEFINITIONS ";QT4;QT4;QT_NO_DEBUG;QT_GUI_LIB;QT_CORE_LIB")

So the automoc does not define the defines when using CMake 2.8.12 as it does with the older CMake version.
(0034338)
Stephen Kelly   
2013-11-03 15:48   
Ok. Your testcase seems to be still bigger than it should be. See http://sscce.org/ [^] . Remove anything which is not part of showing the problem.

Here is my testcase:

  cmake_minimum_required(VERSION 2.8.11)

  project(foo)

  find_package(Qt4 REQUIRED)
  set(CMAKE_AUTOMOC ON)
  add_library(foo MODULE foo.cpp)
  target_link_libraries(foo PUBLIC Qt4::QtCore)

it passes with 2.8.12.

Please post a minimum change to it that shows the problem.
(0034339)
Jason Haslam   
2013-11-03 21:31   
I see this too. The issue for me is that definitions (at least those added with add_definitions) are no longer automatically set in the moc invocation. Here is your minimal sample modified to reproduce the issue:

CMakeLists.txt:
  cmake_minimum_required(VERSION 2.8.11)

  project(foo)

  find_package(Qt4 REQUIRED)
  set(CMAKE_AUTOMOC ON)

  # This definition expected to be set in moc invocation.
  add_definitions(-DFOO)
  include_directories(${CMAKE_CURRENT_BINARY_DIR})

  add_library(foo MODULE foo.cpp)
  target_link_libraries(foo PUBLIC Qt4::QtCore)

  # Uncomment to work around the issue.
  #set_target_properties(foo PROPERTIES AUTOMOC_MOC_OPTIONS -DFOO)

foo.cpp:
  #include <QObject>

  class Foo
  #ifdef FOO
    : public QObject
  #endif
  {
    Q_OBJECT
  };

  #include "foo.moc"

Without the workaround on the last line the moc invocation fails. In cmake 2.8.11.2 the defines were set automatically.
(0034340)
Stephen Kelly   
2013-11-04 05:46   
Thanks, that's a better problem description.

I've pushed the fix-autogen-definitions topic to fix this issue.
(0034394)
Brad King   
2013-11-05 11:20   
Re 0014535:0034340: That fix is based on refactored post-2.8.12 infrastructure. To fix this regression in a 2.8.12.x release we need a change based on v2.8.12~2.
(0034395)
Brad King   
2013-11-05 11:40   
For reference, "git bisect" reports the regression was introduced here:

 Refactor cmTarget::GetCompileDefinitions to use an out-vector, not a string.
 http://cmake.org/gitweb?p=cmake.git;a=commitdiff;h=18412153 [^]
(0034397)
Brad King   
2013-11-05 12:07   
Steve constructed a fix applicable to 2.8.12:

 Automoc: Add directory-level COMPILE_DEFINITIONS to command line
 http://cmake.org/gitweb?p=cmake.git;a=commitdiff;h=a1b9465b [^]

It will be included in 2.8.12.1.
(0036005)
Robert Maynard   
2014-06-02 08:37   
Closing resolved issues that have not been updated in more than 4 months.