关于“未定义符号"的共享库错误;当使用dlopen时 [英] shared library error about "undefined symbol" when using `dlopen`

查看:75
本文介绍了关于“未定义符号"的共享库错误;当使用dlopen时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,这是我的最小可复制程序:

First thing first, here's my minimum reproducible program:

CMakeLists.txt:

CMakeLists.txt:

cmake_minimum_required(VERSION 3.17)
project(untitled4)

set(CMAKE_CXX_STANDARD 17)

add_library(lib1 SHARED lib1.cpp)

add_executable(untitled4 main.cpp)
target_link_libraries(untitled4 PRIVATE dl)

main.cpp:

#include "iostream"
#include <dlfcn.h>
int Test() {
  return 123456;
}
int main() {
  auto* handler = dlopen("/home/liu/source/untitled4/cmake-build-debug/liblib1.so", RTLD_LAZY|RTLD_GLOBAL);
  if (!handler) {
    std::cerr << dlerror();
    exit(1);
  }
}

和lib1.cpp:

#include "iostream"
extern int Test();
class Foo {
 public:
  Foo() {
    std::cout << Test() << std::endl;
  }
};
Foo foo;

现在让我解释一下:

如您所见,我在main.cpp中定义了一个名为 Test 的函数,并且我希望共享库 liblib1.so 在加载时调用它.

As you can see I defined a function called Test in main.cpp, and I want to the shared library liblib1.so call it when it is loaded.

但是当我运行 main()函数时,出现错误日志:

But when I run the main() function, there's an error log said:

/home/liu/source/untitled4/cmake-build-debug/liblib1.so: undefined symbol: _Z4Testv

我通过使用 nm untitled4 | grep Test 检查该符号,并且该符号似乎存在:

I check the symbol by using nm untitled4 |grep Test and the symbol seems exist:

0000000000000b87 t _GLOBAL__sub_I__Z4Testv
0000000000000aea T _Z4Testv

那我做错了什么?该如何解决?

So what did I do wrong? How to fix this?

要注意的重要一点是,在实际情况下, lib1 的内部版本和 main.cpp 的内部版本是完全分开的,两个版本都没有.彼此不认识.但是,如果可以解决问题(如果没有其他方法),我可以将它们组合为一个版本(非常困难).

An important thing to be notice is that in the real case, the build of lib1 and the build of main.cpp are totally separated, the two build don't know each other. But I can make them into one build (very difficult) if this can fix the problem(if there's no other way).

P.S.我尝试使用extern"C"将两个文件中的 Test()换行,但不起作用,似乎不是C/C ++函数的命名问题.

P.S. I tried using extern "C" to wrap around the Test() in both files, but not working, seems not the C/C++ function naming problem.

推荐答案

添加了链接器选项未定义符号"而失败.

With the addition of the linker option -rdynamic your code does not fail with "undefined symbol".

您可以使用 set_target_properties(untitled4 PROPERTIES ENABLE_EXPORTS 1) target_link_options(untitled4在私有"-rdynamic"之前)进行设置.

You can set that with set_target_properties(untitled4 PROPERTIES ENABLE_EXPORTS 1) or target_link_options(untitled4 BEFORE PRIVATE "-rdynamic").

示例:

cmake_minimum_required(VERSION 3.17)
project(untitled4)

set(CMAKE_CXX_STANDARD 17)

add_library(lib1 SHARED lib1.cpp)

add_executable(untitled4 main.cpp)
set_target_properties(untitled4 PROPERTIES ENABLE_EXPORTS 1)

target_link_libraries(untitled4 PRIVATE dl)

这篇关于关于“未定义符号"的共享库错误;当使用dlopen时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆