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. 'extern' construction and undefined reference
Forum Updated to NodeBB v4.3 + New Features

'extern' construction and undefined reference

Scheduled Pinned Locked Moved General and Desktop
13 Posts 5 Posters 11.4k Views 1 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.
  • JeroentjehomeJ Offline
    JeroentjehomeJ Offline
    Jeroentjehome
    wrote on last edited by
    #3

    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
    0
    • S Offline
      S Offline
      shreesh
      wrote on last edited by
      #4

      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
      0
      • S Offline
        S Offline
        shreesh
        wrote on last edited by
        #5

        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
        0
        • A Offline
          A Offline
          andreyc
          wrote on last edited by
          #6

          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
          0
          • A Offline
            A Offline
            andreyc
            wrote on last edited by
            #7

            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
            0
            • S Offline
              S Offline
              shreesh
              wrote on last edited by
              #8

              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
              0
              • S Offline
                S Offline
                shreesh
                wrote on last edited by
                #9

                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
                0
                • A Offline
                  A Offline
                  andreyc
                  wrote on last edited by
                  #10

                  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
                  0
                  • A Offline
                    A Offline
                    andreyc
                    wrote on last edited by
                    #11

                    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
                    0
                    • S Offline
                      S Offline
                      shreesh
                      wrote on last edited by
                      #12

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

                      It is working now..God bless!!

                      1 Reply Last reply
                      0
                      • S Offline
                        S Offline
                        shreesh
                        wrote on last edited by
                        #13

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

                        It is working now..God bless!!

                        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