Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    'extern' construction and undefined reference

    General and Desktop
    5
    13
    9560
    Loading More Posts
    • 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.
    • A
      alan73 last edited by

      Hello to all! Respect to all :)

      Please, help me with a question.
      I'm trying to compile library from sources with 'extern' construction:

      @A.cpp
      int A = 5;
      @

      @B.cpp
      extern int A;

      void foo()
      { A=10; }@

      But got an error: undefined reference to 'A' from 'foo()'. Both files are added to project. What it can be?
      Thanks!

      1 Reply Last reply Reply Quote 0
      • N
        napajejenunedk0 last edited by

        Could you paste the contents of all files?

        1 Reply Last reply Reply Quote 0
        • Jeroentjehome
          Jeroentjehome last edited by

          Hi,
          Adding extern int A in your B.cpp doesn't do anything for 'finding' A by the linker. It only tells the compiler that somewhere a variable exists named A. It is common practice to add the external definition to the header file corresponding to the source file where it is declared.
          So:
          @
          //A.cpp:
          int A = 5;
          //A.hpp:
          extern int A; // Make this variable known to the rest who includes this hpp file
          // B.cpp:
          #include "A.hpp"
          void foo()
          {
          A = 10;
          }
          @
          But to use a variable like this is more C programming and not C++/Object orientated programming.
          C++ options would be to make a public variable in a class / Maybe even a singleton class. But still it is possible.

          Greetz, Jeroen

          1 Reply Last reply Reply Quote 0
          • S
            shreesh last edited by

            Hi all,

            I have been trying to use this "extern" variable for my project, but could not figure out the exact way of doing it. Here is what I have done,

            I have a "login" form where I want to assign QString value to a "global_variable". And then use this saved value in a second form "account".

            Code I wrote is,

            @//login.h
            extern QString global_variable //throws error that, storage space assigned

            //login.cpp, in a signal slot function
            QString global_variable = ui->lineEdit->text();

            //account.cpp
            #include "login.h"

            QString exe = staff_username; // written under function
            //: account::account(QWidget *parent) :
            //QMainWindow(parent),
            //ui(new Ui::staff)

            ui->label_name->setText(exe); @

            Can anybody help me with this please?

            1 Reply Last reply Reply Quote 0
            • S
              shreesh last edited by

              Hi all,

              I have been trying to use this "extern" variable for my project, but could not figure out the exact way of doing it. Here is what I have done,

              I have a "login" form where I want to assign QString value to a "global_variable". And then use this saved value in a second form "account".

              Code I wrote is,

              @//login.h
              extern QString global_variable //throws error that, storage space assigned

              //login.cpp, in a signal slot function
              QString global_variable = ui->lineEdit->text();

              //account.cpp
              #include "login.h"

              QString exe = staff_username; // written under function
              //: account::account(QWidget *parent) :
              //QMainWindow(parent),
              //ui(new Ui::staff)

              ui->label_name->setText(exe); @

              Can anybody help me with this please?

              1 Reply Last reply Reply Quote 0
              • A
                andreyc last edited by

                If this construction is inside a function then it is not global variable.
                @
                //login.cpp, in a signal slot function
                QString global_variable = ui->lineEdit->text();
                @

                "Global variable":http://www.learncpp.com/cpp-tutorial/42-global-variables/ are declared outside of any functions.

                1 Reply Last reply Reply Quote 0
                • A
                  andreyc last edited by

                  If this construction is inside a function then it is not global variable.
                  @
                  //login.cpp, in a signal slot function
                  QString global_variable = ui->lineEdit->text();
                  @

                  "Global variable":http://www.learncpp.com/cpp-tutorial/42-global-variables/ are declared outside of any functions.

                  1 Reply Last reply Reply Quote 0
                  • S
                    shreesh last edited by

                    Oh, yes. You were right.
                    But in my case I need to assign a value in a signal slot function.
                    How would you suggest me to go on this?

                    1 Reply Last reply Reply Quote 0
                    • S
                      shreesh last edited by

                      Oh, yes. You were right.
                      But in my case I need to assign a value in a signal slot function.
                      How would you suggest me to go on this?

                      1 Reply Last reply Reply Quote 0
                      • A
                        andreyc last edited by

                        You need to declare QString global_variable outside of slot function in login.cpp and assign it inside the function.
                        login.cpp
                        @
                        ...
                        QString global_variable;

                        void slot_function()
                        {
                        global_variable = "whatever";
                        }
                        @

                        PS: Using global variables is not good practice.
                        I would suggest to rethink a design and use class member variables or pass it through signal/slot.

                        1 Reply Last reply Reply Quote 0
                        • A
                          andreyc last edited by

                          You need to declare QString global_variable outside of slot function in login.cpp and assign it inside the function.
                          login.cpp
                          @
                          ...
                          QString global_variable;

                          void slot_function()
                          {
                          global_variable = "whatever";
                          }
                          @

                          PS: Using global variables is not good practice.
                          I would suggest to rethink a design and use class member variables or pass it through signal/slot.

                          1 Reply Last reply Reply Quote 0
                          • S
                            shreesh last edited by

                            Thanks a lot!!
                            I was stuck in this since hours.

                            It is working now..God bless!!

                            1 Reply Last reply Reply Quote 0
                            • S
                              shreesh last edited by

                              Thanks a lot!!
                              I was stuck in this since hours.

                              It is working now..God bless!!

                              1 Reply Last reply Reply Quote 0
                              • First post
                                Last post