QMake uses main.cpp from wrong Subdirectory (MSVC)



  • I am setting up a template Qt project in our company, which will be copied and adapted for future projects.
    The template project contains two sub directories: Source and UnitTest, whereas Source contains all the programm code files and UnitTest contains the Google Unit Tests.
    I've added one sample Unit Test, which should be executed already. Also, I have added a sample class to the Source directory which gets used in the main.cpp to output "Hello World!".
    The problem I now have, is that when I compile the template project with the Microsoft Visual C++ Compiler 10.0 (x86), it takes the main.cpp of the Sources directory for the UnitTest.exe. Therefore, when I run the UnitTest.exe it also displays "Hello World!" instead of running the unit test.
    When I compile with the MinGW5.1.0_64bit it works correctly!
    I've also tried to set the full path to the main.cpp in the .pro file without success...

    Does anyone know how I can write the .pro file, so that it works with the MSVC as well?

    Unit Test .pro file:

    TARGET = UnitTests
    
    TEMPLATE = app
    CONFIG += console
    CONFIG -= app_bundle
    
    DESTDIR = $$BIN_DIR
    
    win32 {
      TARGET_FILE=$$DESTDIR/$${TARGET}.exe
      QMAKE_CLEAN += $$TARGET_FILE
    }
    macx {
    }
    linux {
      TARGET_FILE=$$DESTDIR/$${TARGET}
      QMAKE_CLEAN += $$TARGET_FILE
    }
    
    include($$TOP_DIR/common.pri)
    
    INCLUDEPATH += \
      $$TOP_DIR \
      $$TOP_DIR/include \
      $$TOP_DIR/lib \
      $$TOP_DIR/UnitTest/include \
      $$TOP_DIR/Sources
    
    LIBS += \
      -L$$LIB_DIR -lgtest \
      -L$$LIB_DIR -lgmock \
    
    DEPENDPATH += $$LIB_DIR
    
    msvcCompilerUsed {
    message(msvc)
      PRE_TARGETDEPS += \
        $$LIB_DIR/gtest.lib \
        $$LIB_DIR/gmock.lib
    } else {
      PRE_TARGETDEPS += \
        $$LIB_DIR/libgtest.a \
        $$LIB_DIR/libgmock.a
    }
    
    #SOURCES += \
    #  $$TOP_DIR/UnitTest/testunits/main.cpp \                                This is what I've tried as well
    #  $$TOP_DIR/UnitTest/testunits/exampletest.cpp \
    #  $$TOP_DIR/Sources/helloworld.cpp
    
    SOURCES += \
      main.cpp \                                          This is the part where it takes the Sources/main.cpp
      exampletest.cpp \                                   instead of the UnitTest/testunits/main.cpp
      $$TOP_DIR/Sources/helloworld.cpp
    
    HEADERS +=
      $$TOP_DIR/Sources/helloworld.h
    
    

    Directory structure:
    Directory structure


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    I'd use $$PWD/main.cpp to ensure that you are using the right file.



  • This post is deleted!


  • @SGaist
    Hi and thanks for your answer.
    Unfortunately, the $$PWD/ doesnt change the outcome when building my project..
    My guess is that qt creator thinks the main.cpp from the Sources directory is the only main.cpp in the project.
    Therefore it also takes this main.cpp as the current file being parsed when using $$PWD..


  • Lifetime Qt Champion

    In what other .pro/.pri file do you have main.cpp ?



  • @SGaist
    Only in the UnitTest.pro and Sources.pro.
    Update: I've renamed the main.cpp of the Sources Subdirectory to "main_src.cpp" and now it seems to be working fine.
    I'm trying to achieve that without changing the name now..


  • Lifetime Qt Champion

    Just reminded me of something which is not a solution for your problem but might help. Usually when I write a unit test the main file follows the txt_xxx.cpp pattern where xxx is the name of the class tested in lower case.


  • Moderators

    It looks like it is taking the first main.cpp it finds because of your INCLUDEPATH. Try making sure that for your unittest.pro your INCLUDEPATH includes the directory with your unittest's main.cpp before an include that would have the other main.cpp in it.


Log in to reply
 

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