Using MOC wihtout qmake

  • Hello,

    I have written a little c++ file that manages compiling small qt files (cpp) for me. It calls g++ via command line with all needed includes and libraries (qtmain/Qt5Core/Qt5Gui/Qt5Widgets etc...).
    It worked all great, but now I started using Q_OBJECT in my classes and need to run MOC first.

    What I tried so far is (the Q_OBJECT class is in main.h):
    @moc main.h -o moc_main.cpp@

    Now I have the needed code for g++:
    @g++ main.cpp moc_main.cpp -o -I.... -L.... -o main.exe@
    That failed with a lot of errors...
    Then I read something in google like I have to create object files first:
    @moc main.h -o moc_main.cpp
    g++ moc_main.cpp -o moc_main.o
    g++ main.cpp -o main.o
    g++ main.o moc_main.o -o moc_main.exe@
    Then I got the following error:
    "undefined reference to 'WinMain@16'"

    Can somebody answer the following two questions for me?

    1. Why do I have to create object files via MOC? Why isn't it possible to simply add the MOC-created moc_main.cpp to my main.cpp and then create executable file?

    2. What does the last error mean and how can I solve this?

    Thanks a lot for your help...

  • Hi again,

    don't know why, but now everything works fine when I do it like that:
    @moc main.h moc_main.cpp
    g++ moc_main.cpp main.cpp -I... -L... -o main.exe@
    I'm very lucky about that, so I don't have to create confusing object files or stuff...

    Other question:
    Whe I run g++ with my own executable file using command line (system("g++...");), how can I catch g++'s result (success or failure)? I need that to throw an error messagebox in case of failure...

    Any clues?

    Solved! For everyone who is interested in it:
    @g++ -main.cpp -o main.exe 2> main_error.txt@
    the main_error.txt will be created in case of success and also in case of failure, so you additionally have to check the contents:
    @ char error_buffer_1[10];
    FILE *file_error = fopen("main_error.txt", "r");
    fgets(error_buffer_1, 10, file_error);
    string error_buffer_2 = error_buffer_1;
    int error_buffer_length = error_buffer_2.length();

        if (file_error == NULL || fgets(error_buffer_1, 10, file_error) == NULL || error_buffer_length < 1)

    cout << "compiler error occured";


Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.