0

I supposed I do not have to link to a shared library when I use dlopen. However, in cmake target_link_libraries(main_dlopen dl) results in linker errors

main_dlopen.cpp.o: In function `main':
main_dlopen.cpp:25: undefined reference to `ntclass::init(char const*)'
etc...

If I use target_link_libraries(main_dlopen dl ntclass), where libntclass.so is my library, then everything is fine.

Is it really fine or am I missing something? As a background, I want to test non thread-safe libraries, as explained here, and expect that linking should be avoided with non thread-safe libs. Partially answered myself below

A full example is below (used this as reference).

(shared library)

ntclass.h

#ifndef NTCLASS_H
#define NTCLASS_H

#include <cstddef>

class ntclass
{
  private:
    static char *sptr;
    char *ptr;

  public:
    ntclass() : ptr(NULL) {}
    ~ntclass();

    void init(const char* str);
    void print();
};

typedef ntclass* create_t();

#endif // NTCLASS_H

ntclass.cpp

#include "ntclass.h"
#include <stdio.h>
#include <string.h>
#include <iostream>

char *gptr = NULL;

char *ntclass::sptr = NULL;

ntclass::~ntclass()
{
  if (gptr)
  {
    delete[] gptr;
    gptr = NULL;
  }
  if (sptr)
  {
    delete[] sptr;
    sptr = NULL;
  }
  if (ptr)
  {
    delete[] ptr;
    ptr = NULL;
  }
}

void ntclass::init(const char* str)
{
  int size = strlen(str)*sizeof(char);
  gptr = new char[size];
  memcpy(gptr, str, size);
  sptr = new char[size];
  memcpy(sptr, str, size);
  ptr = new char[size];
  memcpy(ptr, str, size);
}

void ntclass::print()
{
  std::cout << "Global: " << gptr << std::endl;
  std::cout << "Static: " << sptr << std::endl;
  std::cout << "Normal: " << ptr << std::endl;
}

extern "C" ntclass *create()
{
  return new ntclass();
}

(Main executable)

main_dlopen.cpp

#include <iostream>
#include "ntclass.h"

#include <dlfcn.h>
#include <stdlib.h>

using namespace std;

int main()
{
  void *handle = dlopen("./libntclass.so", RTLD_NOW);

  if (handle == NULL)
  {
     cerr << dlerror() << endl;
     exit(-1);
  }

  create_t *createA = (create_t*) dlsym(handle, "create");
  create_t *createB = (create_t*) dlsym(handle, "create");

  ntclass *A = createA();
  ntclass *B = createB();

  A->init("A");
  B->init("B");

  A->print();
  B->print();

  delete A;
  delete B;

  return 0;
}

(cmake)

cmake_minimum_required(VERSION 2.8)

set( CMAKE_VERBOSE_MAKEFILE on )
set(CMAKE_BUILD_TYPE RelWithDebInfo)

add_library(ntclass SHARED ntclass.cpp)

add_executable(main_dlopen main_dlopen.cpp)
target_link_libraries(main_dlopen dl) # <-- Here is a problem

Partial answer: I added keyword virtual for ntclass methods (init, print, ~ntclass) and it works fine now. Still, can anyone explain why is it needed?

Ivan
  • 599
  • 4
  • 18

1 Answers1

1

Your "partial answer" is a correct fix. For explanations, see that nice answer about virtual keyword.

In short:

Until init method in ntclass is declared as virtual, expression

A->init("A")

uses definition of the method in that class (independent on which actual type object A has). And because this definition is absent in main_dlopen.cpp, linker generates the error.

With virtual keyword, resolution of init method is deffered to runtime, when actual type of A object will be known.

Tsyvarev
  • 60,011
  • 17
  • 110
  • 153