![](/img/trans.png)
[英]Error “unresolved external symbol” when library is dynamically loaded at runtime
[英]Undefined symbol error in a shared library loaded at runtime
(我知道這個問題的各種迭代在這里經常被問到,但我沒有找到任何幫助)
我正在嘗試編寫一個簡單的動態庫並在運行時加載它。
庫文件:
mul.h
int mul(int a, int b);
mul.cpp
#include "mul.h"
int mul(int a, int b) {
return a * b;
}
然后我有這個Makefile
:
mul.cpp: mul.h
mul_lib: mul.cpp
g++ -Wall -fPIC -shared $< -o bin/libmul.so
bin/test.o: mul_lib
g++ test.cpp -ldl -o bin/test
所以我創建了一個.so
文件,並使用nm
檢查是否有mul
的符號 ...或者我認為是mul
的符號:
>nm -D bin/libmul.so
w __cxa_finalize
w __gmon_start__
w _ITM_deregisterTMCloneTable
w _ITM_registerTMCloneTable
00000000000010f9 T _Z3mulii
在test.cpp
中,我試圖用dlopen
加載這個庫:
typedef int (*mul_func)(int a, int b);
void* mul_lib = dlopen("bin/libmul.so", RTLD_NOW);
if (!mul_lib) {
/* fail to load the library */
fprintf(stderr, "Error: %s\n", dlerror()); // we don't exit here, library loaded
return 2;
}
mul_func* func = (mul_func*)dlsym(mul_lib, "mul");
if (!func) {
fprintf(stderr, "Error: %s\n", dlerror()); // we exit here
return 2;
}
(*func)(2, 4);
dlclose(mul_lib);
但我得到bin/libmul.so: undefined symbol: mul
我不明白為什么。 該符號出現在 object 文件中,似乎,並且它已正確加載(我假設),那么為什么該符號不存在?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.