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. Compile two cpp files with QT

Compile two cpp files with QT

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 164 Views
  • 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.
  • O Offline
    O Offline
    OlegD
    wrote on 7 Jun 2022, 22:43 last edited by
    #1

    Hello! Need your advise
    Need to compile:

    #ifndef TEST_H
    #define TEST_H

    int a = 0;
    void simplePrint ();

    #endif // TEST_H

    #include <iostream>
    #include <iomanip>
    #include <cstdlib>
    #include <ctime>
    #include <Test.h>

    using namespace std;
    using std::ios;

    void simplePrint ()
    {
    cout << "a " << a << endl;
    }

    #include <iostream>
    #include <Test.h>
    using namespace std;
    using std::ios;

    int main()
    {
    cout << "Hello World!" << endl;
    return 0;
    }

    PRO_FILE
    TEMPLATE = app
    CONFIG += console c++11
    CONFIG -= app_bundle
    CONFIG -= qt

    SOURCES +=
    Test.cpp
    main.cpp

    HEADERS +=
    Test.h

    :-1: error: debug/main.o:C:\Users\iceberg\Desktop\Test4\build-Test4-Desktop_Qt_6_2_3_MinGW_64_bit-Debug/../../TestBsae/Test4/Test.h:6: multiple definition of `a'; debug/Test.o:C:\Users\iceberg\Desktop\Test4\build-Test4-Desktop_Qt_6_2_3_MinGW_64_bit-Debug/../../TestBsae/Test4/Test.h:6: first defined here

    1 Reply Last reply
    0
    • C Offline
      C Offline
      ChrisW67
      wrote on 8 Jun 2022, 02:38 last edited by
      #2

      This is a basic C/C++ question an nothing to do with the Qt library.

      Your Test.h declares a and allocates storage for it.
      When you compile Test.cpp it includes Test.h. The resulting Test.o object file contains space for a.
      When you compile Main.cpp it includes Test.h. The resulting main.o object file contains space for a.
      When the linker combines Test.o and main.o to make the resulting executable it fails because there are two a variable space allocations in the same scope.

      There should only be space allocated in one place for a.
      Test.h

      #ifndef TEST_H
      #define TEST_H
      
      extern int a;  
      // ^^^ A declaration of the existence of "a" so that main.cpp can be compiled
      //     No space allocated
      void simplePrint ();
      
      #endif // TEST_H
      

      Test.cpp

      #include <iostream>
      #include <iomanip>
      #include <cstdlib>
      #include <ctime>
      #include <Test.h>
      
      using namespace std;
      using std::ios;
      
      int a = 0; 
      // ^^^ The storage for "a" should be in one CPP file only
      
      void simplePrint ()
      {
      cout << "a " << a << endl;
      }
      
      1 Reply Last reply
      1

      1/2

      7 Jun 2022, 22:43

      • Login

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