1

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.

Rancs
  • 165
  • 1
  • 10
  • 2
    How are you compiling and linking? Please show the actual commands you're using. – Mat Dec 02 '13 at 05:54
  • It sounds like you are either not adding the appropriate search paths for either the header file (compiling) or the library file (linking). – kingtorus Dec 02 '13 at 05:57
  • I compile that with the "ndk-build" command of Android ndk, not with make. But I thought this was a c++ subject, that's why I tagged only in c++ – Rancs Dec 02 '13 at 06:00
  • @kingtorus I added the path for header file. And the cpp file is in the same directory with header file. And linked the library. I can use some classes. – Rancs Dec 02 '13 at 06:01
  • 1
    Wondering if this [Android NDK Stackoverflow Question](http://stackoverflow.com/questions/2943828/how-to-compile-a-static-library-using-the-android-ndk/2957386#2957386) might help. – kingtorus Dec 02 '13 at 06:18
  • @kingtorus libdummy.so is not a static system library. It is a third party prebuilt shared library. And I don't have problems in adding header file paths. The 3rd party library I use is a very big library with tons of classes inside of it. I use can use most of the classes. But the only problem is: when I need to use a class with undefined constructors and destructors, I get undefined reference error. – Rancs Dec 02 '13 at 06:47
  • @kingtorus For example in the above sample code; dummy.h has dummy() and ~dummy(). What I have to do to avoid undefined reference error? I have to define them with empty bracelets dummy(){} and ~dummy(){}, right? But I can not do that because they are packed inside the library. They are not directly editable. Or another possible solution adding the dummy.cpp to the compiler line like Hitesh told below in his response. But either I can not do that because it is are packed. Hope I could explain the problem. I don't have a problem to access to the library or to the classes. – Rancs Dec 02 '13 at 06:47
  • I think this page can help you, http://stackoverflow.com/questions/496664/c-dynamic-shared-library-on-linux – hahaya Dec 02 '13 at 07:05
  • I think this page can help you, please have a look at this http://stackoverflow.com/questions/496664/c-dynamic-shared-library-on-linux – hahaya Dec 02 '13 at 07:07
  • @hahaya this page talk about creating a shared library himself. So he can add bracelets to the constructor in his dummy.h like that: dummy(){}. So he will not have a undefined reference problem. In my case, I use an already compiled 3rd party shared library. I can not change dummy.h and dummy.cpp. I don't have these 2 files. I have only libdummy.so and I can not edit it. – Rancs Dec 02 '13 at 07:19

2 Answers2

0

please compile both the file.. If you are using linux , than

g++ client.cpp dummy.cpp
Hitesh Vaghani
  • 1,342
  • 12
  • 31
  • dummy.cpp is inside libdummy.so. I have only 3 files: libdummy.so, client.h and client.cpp. Ok, I edited the question to clarify this point again. – Rancs Dec 02 '13 at 06:05
0

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.

rabensky
  • 2,864
  • 13
  • 18
  • 1
    in my question I didn't ask how I can do that in Android makefile. But I asked how it can be done in general c++. That's why I accept your response 'g++ client.cpp -ldummy' as the answer of my question. Thank you very much. (If I can not figure out how to do that in Android makefile, maybe I can open later a 2nd question under android-ndk tag) – Rancs Dec 02 '13 at 07:51