CybeX
CybeX

Reputation: 2396

qt - undefined reference to `_imp___Z*misc*googleDNS*'

Good day all

Please note : C++ newbie here

Background Info:

I have been trying to create a set of libraries, where a few libraries use each other.

In this particular case, I have to DLL libraries added as external libraries.

Problem:

Error reads:

netman.cpp:65: error: undefined reference to `_imp___ZN4misc9googleDNSE'

Code in main library:

//netmap.cpp - Partial

//...
QHostAddress serverIP = QHostAddress(misc().googleDNS);
//...

//Misc.h - Partial

#ifndef MISC_H
#define MISC_H

#include "misc_global.h"

#include <QString>
#include <QList>


class MISCSHARED_EXPORT misc
{
public:
    misc();
    ~misc();

    //Network related
    static QString googleDNS;

    //Command Codes
    static QString CMD_AUTH;
    static QString CMD_REQ;

    struct User_Settings{
    //...
};

//misc.cpp - Partial

#include "misc.h"

misc::misc(){

    //Network related
    QString googleDNS = QString("8.8.8.8");

    //Command Codes
    QString CMD_AUTH = QStringLiteral("AUTH");
    QString CMD_REQ = QStringLiteral("REQ");

    //Request Codes
    QString REQ_USER_INFO = QStringLiteral("USER_INFO");
    QString REQ_VPN_DATA = QStringLiteral("VPN_DATA");
    QString REQ_VPN_UP = QStringLiteral("VPN_UP");

    //...
}

Note : If I you might question the reason for misc().googleDNS, I am using this "method" for declaring and instantiating static strings.

What is/could be causing this "_imp__" error?

Help would be appreciated!

If more info is required, please leave a comment, I will update and add

_________________________________________________________________________

ANSWER/Solution

First off, thank you to @SomeProgrammingDude for providing the solution.

//misc.h

#ifndef MISC_H
#define MISC_H

#include "misc_global.h"

#include <QString>
#include <QList>

namespace Misc {

    //Network related
    QString googleDNS = QString("8.8.8.8");

    //Command Codes
    QString CMD_AUTH = QString("AUTH");
    QString CMD_REQ = QString("REQ");

    //Request Codes
    QString REQ_USER_INFO = QString("USER_INFO");
    QString REQ_VPN_DATA = QString("VPN_DATA");
    QString REQ_VPN_UP = QString("VPN_UP");

    class MISCSHARED_EXPORT misc
    {
    public:
        misc();
        ~misc();

        //...

    };

    //...
}
#endif // MISC_H

//misc.cpp

#include "misc.h"

using namespace Misc;

misc::misc(){}

misc::~misc(){}

//...

Rebuilding this by running qmake, then rebuilding, and proceeding to "main" library - netman.

//netman.h

//...
#include "misc.h"
#include "gen.h"

using namespace Misc;

class NETMANSHARED_EXPORT netman
{

public:
    netman();
    netman(QString hostname);
//...
};

#endif // NETMAN_H

//netman.cpp

#include "netman.h"

using namespace Misc;

netman::netman(){
}

then one can use one of 2 methods, very similar:

//...
QHostAddress serverIP = QHostAddress(googleDNS);
//...

or

//...
QHostAddress serverIP = QHostAddress(Misc::googleDNS);
//...

but the latter is preferred.

Hope this helps others!

Upvotes: 0

Views: 572

Answers (1)

Some programmer dude
Some programmer dude

Reputation: 409196

Two problems:

  1. Unless you create an instance of the misc class, the constructor will not be called.

  2. In the constructor you define the variables as local variables. The static member variables are not defined at all.

To solve the problem I first suggest you use a namespace instead of a class. A class with only public and static members is no better than just a simple namespace (I sidestep the whole issue about global variables).

Then you need to define the variables, which you need to do outside of any function. Directly in the file do:

QString misc::googleDNS = QString("8.8.8.8");

To put it together, you need two files.

First the header file:

#ifndef MISC_H
#define MISC_H

#include "misc_global.h"

#include <QString>
#include <QList>

namespace misc
{
    //Network related
    MISCSHARED_EXPORT QString googleDNS;

    // etc...
}

#endif

Then have the source file:

#include "mish.h"

namespace misc
{
    QString googleDNS = QString("8.8.8.8");

    // etc...
}

Upvotes: 2

Related Questions