【问题标题】:When using dlopen, shall I link against a library I open?使用 dlopen 时,我应该链接到我打开的库吗?
【发布时间】:2017-06-01 08:49:46
【问题描述】:

我想我在使用 dlopen 时不必链接到共享库。但是,在 cmake target_link_libraries(main_dlopen dl) 中会导致链接器错误

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

如果我使用 target_link_libraries(main_dlopen dl ntclass),其中 libntclass.so 是我的库,那么一切都很好。

真的很好还是我错过了什么?作为背景,我想测试非线程安全库,正如here 所解释的那样,并期望应该避免使用非线程安全库进行链接。 在下面部分回答了我自己

一个完整的例子如下(使用this作为参考)。

(共享库)

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_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

部分回答: 我为 ntclass 方法(init、print、~ntclass)添加了关键字virtual,现在它工作正常。不过,谁能解释为什么需要它?

【问题讨论】:

    标签: c++ linux cmake shared-libraries dlopen


    【解决方案1】:

    您的“部分答案”是正确的解决方法。解释见that nice answervirtual关键字。

    简而言之:

    直到ntclass中的init方法被声明为,表达式

    A->init("A")
    

    使用该类中方法的定义(与A 具有的实际类型 对象无关)。因为main_dlopen.cpp 中没有这个定义,所以链接器会产生错误。

    使用virtual关键字,init方法的解析延迟到运行时,当A对象的实际类型将是已知的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-10-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-17
      • 2011-09-04
      相关资源
      最近更新 更多