'extern' construction and undefined reference



  • 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!



  • Could you paste the contents of all files?



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



  • 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?



  • 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?



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



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



  • 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?



  • 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?



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



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



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

    It is working now..God bless!!



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

    It is working now..God bless!!


Log in to reply
 

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