简体   繁体   中英

C++ Undefined reference errors to the constructors and destructors of the classes which are inside a shared library

Ok, I am very new in C++ development. This question may be silly but I can not find its response in any tutorial, book, question/response. It would be great if somebody can kindly explain it to me.

I have 1 header-source pair inside of a shared library libdummy.so:

This is dummy.h:

class dummy
{
    public:
    ~dummy();
    dummy();

    bool dosomething(int a);
};

and this is dummy.cpp:

#include "dummy.h"

dummy::dummy()
{
//some assignments here
    clear();
}

dummy::~dummy()
{
clear();
}

bool dummy::dosomething(int a)
{
    // do something here
    return true;
}

EDIT: I tell you above the sample codes of dummy.h and dummy.cpp but these files are not in my hand. They are packed inside the library libdummy.so. I have only the libdummy.so shared library file in the hand.

And I have a client to access my shared library.

client.h is here:

#include "dummy.h"

class client
{
    public:
    void myownjob();

    dummy thingy;

    //and some functions here
};

and finally this is the client.cpp:

#include "client.h"

void client::myownjob()
{
    thingy.dosomething(1);
}

Now my problem is; when I try to compile this code, I get undefined reference errors to the constructor and destructor:

error: undefined reference to 'dummy::~dummy()'
error: undefined reference to 'dummy::dosomething(int)'
error: undefined reference to 'dummy::dummy()'

EDIT: The dummy.h and dummy.cpp are inside libdummy.so. I have only 3 files in the hand: libdummy.so, client.h and client.cpp.

That's why;

  • I can not delete ~dummy(); and dummy(); in the dummy.h to let the compiler creating them automatically. Because dummy.h is inside the libdummy.so shared library. It is not directly editable.

  • I can not do some braceleted empty definitions like ~dummy(){} and dummy(){} in the dummy.h. Because dummy.h is inside the libdummy.so shared library. It is not directly editable.

  • I can not include dummy.cpp to SRC_FILES line of my makefile. Because dummy.cpp is inside libdummy.so shared library. It is not a seperate file.

I think this is a very simple/beginner problem, but I can not find its response anywhere. What I have to do to use a class which is inside a shared library, in C++, when I get undefined reference errors to the constructors and destructors?

Thanks in advance.

请同时编译两个文件。如果使用linux,则

g++ client.cpp dummy.cpp

I don't know about any NDK, but this error is a linking error - where you don't provide the library at link time (even though it's a dynamic library, you have to provide it when linking)

Normally I'd tell you to compile + link like this:

g++ client.cpp -ldummy

( -ldummy links with libdummy.so )

but given you're using some makefile you're not familiar with, I don't really know how you should do it. I can guess though:

Apparently you have a field named SRC_FILES . A quick (and incomplete) google suggest you have a field named LOCAL_LDLIBS . If so try setting this:

LOCAL_LDLIBS = -ldummy

Or, if the libdummy.so file isn't in your standard path:

LOCAL_LDLIBS = -L/dummy/file/path -ldummy

( /dummy/file/path is obviously the path of your dummy library)

Note that depending on your configuration, you might need to add the -L even if the file is in your working directory.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM