Unexpected Compilation / Linking Problem

  • I have the following .h file included in one .cpp file. Now that I need the same stuff in another file, I have included this .h file in another .cpp file (which does not include the .h file via some existing include path. This gives an error while trying to compile my complete code claiming that NameExp would be defined multiple times. All this code is in the same namespace. I had the impression that the preprocessors would prevent the error, but apparently my C++ is too rusty. Any ideas on what might be wrong?

    #ifndef EXPRESSIONS_H
    #define EXPRESSIONS_H
    #include <QRegularExpression>
    namespace DFM {
    QRegularExpression NameExp("^_?[a-zA-Z]([a-z]|[A-Z]|[0-9]|_|:|\\.)*$");
    #endif // EXPRESSIONS_H

  • I dived deeper into it and of course it requires some extra coding and the use of 'extern'. I got it solved now :)

  • Lifetime Qt Champion


    Why did you need to use extern ?

    What exact error did you got ?

  • Qt Champions 2017

    @SGaist said in Unexpected Compilation / Linking Problem:

    Why did you need to use extern ?

    He has put the variable definition in the header, so he gets multiple instances for the same symbol name. My guess is he got the "multiple definition" family of errors.

  • Lifetime Qt Champion

    @kshegunov Good point, I didn't thought about that one but makes sense.

Log in to reply

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