0

I'm trying to create a singleton which wraps an shared library. But when I'm try to get the instance of class (from an static function) I'm getting the error "undefined referenced to NativeLibrary::Instance()".

NativeLibrary.h:

#ifndef NATIVELIBRARY_H_INCLUDED
#define NATIVELIBRARY_H_INCLUDED

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
using namespace std;

class NativeLibrary
{
#ifdef _WIN32
    HINSTANCE handle;
#else
    void* handle;
#endif // _WIN32
        NativeLibrary();
        ~NativeLibrary();
    private:
        static NativeLibrary* _instance;
    public:
        static NativeLibrary* Instance();
};
#endif // NATIVELIBRARY_H_INCLUDED

NativeLibrary.cpp:

#include "NativeLibrary.h"
#include "NativeArray.h"

#ifdef _WIN32
#include <windows.h>
#define symLoad GetProcAddress GetProcAddress
#else
#include <dlfcn.h>
#define symLoad dlsym
#endif

NativeLibrary::_instance = NULL;

NativeLibrary::NativeLibrary()
{
#ifdef _WIN32
    handle = LoadLibrary("NativeLibrary.dll");
#elif __APPLE__
    handle = dlopen("NativeLibrary.dylib", RTLD_LAZY);
#else
    handle = dlopen("NativeLibrary.so", RTLD_LAZY);
#endif // _WIN32
}

NativeLibrary::~NativeLibrary()
{
#ifdef _WIN32
    FreeLibrary(handle);
#else
    dlclose(handle);
#endif // _WIN32
}

NativeLibrary* NativeLibrary::Instance()
{
    if(_instance != NULL)
        _instance = new NativeLibrary();

    return _instance;
}

main.cpp (where I'm trying to get the instance):

#include "NativeLibrary.h"
void main()
{
    NativeLibrary* instanceLib = NativeLibrary::Instance();
}
Joseph Moreno
  • 61
  • 1
  • 5
  • Also [What should main() return in C and C++?](https://stackoverflow.com/questions/204476/what-should-main-return-in-c-and-c) – walnut Jan 05 '20 at 19:39
  • I'm using ldl option, so the linked is dynamic. – Joseph Moreno Jan 05 '20 at 21:18
  • Instance() creates a class with handle and inside the Instance loads the needed symbols. I omit that code because I'm getting an error trying invoke Instance() which is defined into cpp file. – Joseph Moreno Jan 05 '20 at 22:58
  • So `NativeLibrary.cpp` isn't actually compiled into the shared library? The naming was confusing me then, but I see that it would have been clear if I read your question correctly, sorry. But in that case the question remains: Did you link the object file created from `NativeLibrary.cpp` with the one created from `main.cpp`? – walnut Jan 05 '20 at 23:01
  • No. NativeLibrary.cpp and NativeLibrary.h are wrappers for NativeLibrary.so. Im import NativeLibrary.h in main.cpp, there I trying to get the Instance of singleton class NativeLibrary (defined on h, inplemented on cpp). – Joseph Moreno Jan 06 '20 at 03:06
  • You still need to compile and link both `NativeLibrary.cpp` and `main.cpp`. Are you doing that? – walnut Jan 06 '20 at 03:10
  • In fact, .so file isn't c++ compiled, but is c compatible. Yes I want a wrapper into a singleton. Im really know nothing about c++ :( – Joseph Moreno Jan 06 '20 at 14:36
  • You will need to show how you are compiling the files shown in your question if you want help. See also answers to the linked duplicate. The code itself is fine and doesn't generate the error if compiled properly. (Assuming `#include "NativeArray.h"` which you didn't show doesn't do anything weird.) – walnut Jan 06 '20 at 17:47

0 Answers0