Note that there are some explanatory texts on larger screens.

plurals
  1. POQt, CMake, Visual Studio and Q_OBJECT in cpp files
    primarykey
    data
    text
    <p>I'm developing a large project using Qt 4.6, CMake 2.8 and Visual Studio 2008 for the Windows platform.</p> <p>As far the build system goes, it's all standard stuff: I'm using CMake's <code>QT4_WRAP_CPP</code> macro to generate moc files from header files, which are then linked into the final executable in the <code>add_executable</code> command. Everything is working as expected.</p> <p>The only restriction with this setup is that I can't define widgets or helper using <code>Q_OBJECT</code> in <em>.cpp</em> files. This would be very convenient for small, context-specific helpers classes that should appear right next to where they're used.</p> <p>I tried to pass the whole list of source files (both <em>.h</em> and <em>.cpp</em>) to <code>QT4_WRAP_CPP</code>, instead of just the header files, but that doesn't work (linking fails because some moc-related symbols are undefined).</p> <p>I think the problem is that, for a given pair of files <em>foo.h</em> and <em>foo.cpp</em>, the <code>QT4_WRAP_CPP</code> macro will generate the same moc file (<em>moc_foo.cxx</em>) in the same directory, and obviously that means the first file will be overwritten by the second one, and as a result symbols will be missing at link-time.</p> <p>Is there a way to fix or work around that problem? For instance, I tried to add a specific rule for <em>foo.cpp</em> of the form</p> <pre><code>QT4_GENERATE_MOC(directory/foo.cpp directory/foo.moc) </code></pre> <p>and then add</p> <pre><code>#include "foo.moc" </code></pre> <p>at the end of <em>foo.cpp</em>. I think this ought to work, but alas Visual Studio only allows one build rule per file, and <em>.cpp</em> files already have a build rule (compilation to object file), so this approach doesn't work, at least with Visual Studio.</p> <p>Another idea that I had was to create a new macro, say <code>QT4_WRAP_CPP_WITH_PREFIX</code>, based on <code>QT4_WRAP_CPP</code> (which is defined in <em>share/cmake-2.8/Modules/Qt4Macros.cmake</em>), that would take an additional prefix argument and would add this prefix to the generated moc files. That way, I would call <code>QT4_WRAP_CPP_WITH_PREFIX</code> twice, once for <em>.h</em> files and once for <em>.cpp</em> files, with different prefixes. What I just dislike about this approach is that I'd be messing with the internals of CMake's Qt support, instead of using the public API.</p> <p>Any better idea?</p> <p>Cheerz, Franz</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload