3

By removing the moc generation step from QMake I no longer have to rely on QtCreator. At the moment I am using a custom script to generate/update Qt moc files before generating makefiles via GYP in eclipse. Does anyone know how to add moc as build rules to GYP so I can consolidate this step?

At the moment I have the script as a build rule inside of eclipse running every-time but I would like to avoid this approach by having GYP generate a makefile with the moc rules already inside of it like QMake does.

Links of interest:

Reference to developing Qt with GYP: https://groups.google.com/group/gyp-developer/browse_thread/thread/42cfb9902b86d715/b17701d9a6805671?show_docid=b17701d9a6805671

GYP Homepage: http://code.google.com/p/gyp/

QMake Homepage: http://qt-project.org/doc/qt-4.8/qmake-manual.html

My current moc script:

#!/bin/bash
MOC="/path/to/Qt/4.8.1/gcc/bin/moc"
SRC_DIR="/path/to/project"

$MOC $SRC_DIR/SkDebuggerUI.h -o $SRC_DIR/moc_SkDebuggerUI.cpp
$MOC $SRC_DIR/SkQtWidget.h -o $SRC_DIR/moc_SkQtWidget.cpp
Flarex
  • 511
  • 6
  • 15
  • 1
    Other than the moc step there is nothing special about Qt apps, you can build them with anything. note you can safely moc all the code, it will just ignore stuff that isn't Q_OBJECT – Martin Beckett Jun 06 '12 at 18:26
  • Hey Martin, can you look at this question again now that I have updated it. My original approach was very vague. – Flarex Jun 07 '12 at 13:37

2 Answers2

1

I had exactly the same question. Instead of including a separate step with moc script, I added the following lines into my .gyp file.

'conditions': [
    ['OS=="linux"': {
        'sources': [
            'your other source code files',
            '<!(moc numerickeypad.h -o moc_numerickeypad.cpp && echo moc_numerickeypad.cpp)',
        ],
    }],
],

I didn't try it on other operating systems, but I think you can do it on other systems similarly. Let me know, if you have figured better ways.

Johnson
  • 71
  • 5
0

I just went through this, and while it's not perfect, it's workable. I did the following:

  • Made a separate 'target' for generating the mocs
  • Had that target only have the .h files that we want to run moc on as 'sources'
  • Exposed the generated .cpp files as a direct_dependent_settings, so their sources get compiled in dependent targets
  • Build a 'rule' to run moc on each of the header files in the 'sources' list.
  • Had my executable depend on that target

It looks like this:

'targets':
[
{
    'target_name': 'editor_generate_mocs',
    'type': 'none',
    'sources': 
        [
            'src/editorwindow.h',
        ],

    'direct_dependent_settings':
        {
            'sources':
            [
                '<(SHARED_INTERMEDIATE_DIR)/editorwindow_moc.cpp',
            ]
        },
    'rules':
        [
            {
                'rule_name': 'generate_qt_mocs_rule',
                'extension': '.h',
                'msvs_cygwin_shell' : 0, # Don't run cygwin set_env to run a command.
                'inputs':
                    [
                        '>(qt_dir)/bin/moc.exe',
                    ],
                'outputs':
                    [
                        '<(SHARED_INTERMEDIATE_DIR)/<(RULE_INPUT_ROOT)_moc.cpp',
                    ],
                'action':
                    [
                        '>(qt_dir)/bin/moc.exe',
                        '--compiler-flavor', 'msvc',
                        '<(RULE_INPUT_PATH)',
                        '-o', '<(SHARED_INTERMEDIATE_DIR)/<(RULE_INPUT_ROOT)_moc.cpp'
                    ],
                'message': '<(SHARED_INTERMEDIATE_DIR)/<(RULE_INPUT_ROOT)_moc.cpp'
            }
        ]
    },
# ..., rest of my targets
}
]
Eddie Parker
  • 4,519
  • 2
  • 31
  • 40