Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QMake uses main.cpp from wrong Subdirectory (MSVC)
Qt 6.11 is out! See what's new in the release blog

QMake uses main.cpp from wrong Subdirectory (MSVC)

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 3 Posters 3.0k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MarcHaeusler
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      M 1 Reply Last reply
      0
      • M Offline
        M Offline
        MarcHaeusler
        wrote on last edited by
        #3
        This post is deleted!
        1 Reply Last reply
        0
        • SGaistS SGaist

          Hi and welcome to devnet,

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

          M Offline
          M Offline
          MarcHaeusler
          wrote on last edited by
          #4

          @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..

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

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

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            M 1 Reply Last reply
            0
            • SGaistS SGaist

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

              M Offline
              M Offline
              MarcHaeusler
              wrote on last edited by
              #6

              @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..

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                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.

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  ambershark
                  wrote on last edited by
                  #8

                  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.

                  My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved