c++ - ./libbar.so:未定义符号:__gxx_personality_v0,如何解决?
问题描述
在制作一个简单的测试用例时,我遇到了另一个问题。请帮我。
这是文件。
<<< bar.cpp >>>
#include <stdint.h>
#include <stdio.h>
extern "C" {
uint64_t var_from_lib;
}
class BC;
class BC {
public:
void bar(void);
BC();
~BC();
};
BC::BC()
{
}
BC::~BC()
{
}
void BC::bar(void)
{
printf("class function : var_from_lib = %lx\n", var_from_lib);
}
extern "C" {
void bar(void)
{
printf("global function : var_from_lib = %lx\n", var_from_lib);
BC tmp;
tmp.bar();
}
}
<<< main1.c >>>
#include <dlfcn.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
extern uint64_t var_from_lib; // = 0x12345678;
int main1(void)
{
void * dlh = dlopen("./libbar.so", RTLD_NOW);
if (!dlh) {
fprintf(stderr, "%s\n", dlerror());
exit(EXIT_FAILURE);
}
void (*bar)(void) = dlsym(dlh,"bar");
if (!bar) {
fprintf(stderr, "%s\n", dlerror());
exit(EXIT_FAILURE);
}
var_from_lib = 0x12341111;
bar();
return 0;
}
<<< main2.c >>>
#include <dlfcn.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
extern uint64_t var_from_lib; // = 0x12345678;
int main2(void)
{
void * dlh = dlopen("./libbar.so", RTLD_NOW);
if (!dlh) {
fprintf(stderr, "%s\n", dlerror());
exit(EXIT_FAILURE);
}
void (*bar)(void) = dlsym(dlh,"bar");
if (!bar) {
fprintf(stderr, "%s\n", dlerror());
exit(EXIT_FAILURE);
}
var_from_lib = 0x12342222;
bar();
return 0;
}
<<< main.c >>>
#include <dlfcn.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
extern uint64_t var_from_lib; // = 0x12345678;
uint64_t __attribute__((weak)) var_from_lib; // = 0x12345678;
extern int main1();
extern int main2();
int main(int argc, char *argv[])
{
if (atoi(argv[1]) == 1) {
main1();
}
else if (atoi(argv[1]) == 2) {
main2();
}
else {
printf("usage : main [1|2]\n");
}
return 0;
}
<<< Makefile >>>
.PHONY: all clean test
LDEXTRAFLAGS ?=
all: prog
%.o: %.c
gcc -c -Wall -fpic -o $@ -ldl $<
%.o: %.cpp
g++ -c -Wall -fpic -o $@ $<
libbar.so: bar.o
gcc -shared -o $@ $<
main: main.o main1.o main2.o
gcc -c -Wall -o $@ $< -rdynamic
prog: main.o main1.o main2.o | libbar.so
gcc $(LDEXTRAFLAGS) -o $@ $^ -ldl
clean:
rm -f *.o *.so prog
这是执行结果。
ckim@ckim-ubuntu:~/testdir$ make
gcc -c -Wall -fpic -o main.o -ldl main.c
gcc -c -Wall -fpic -o main1.o -ldl main1.c
gcc -c -Wall -fpic -o main2.o -ldl main2.c
g++ -c -Wall -fpic -o bar.o bar.cpp
gcc -shared -o libbar.so bar.o
gcc -o prog main.o main1.o main2.o -ldl
ckim@ckim-ubuntu:~/testdir$ prog 1
./libbar.so: undefined symbol: __gxx_personality_v0
我怎样才能消除错误?
解决方案
我怎样才能消除错误?
将您的应用程序与 C++ 库链接。g++
与GLOBAL
dlopen
图书馆链接libstdc++.so
。总的来说,gcc -shared -o libbar.so bar.o
应该是g++ -shared -o libbar.so bar.o
- 它是一个 C++ 库。gcc -Wl,--no-undefined -shared -o libbar.so bar.o
抓住了问题。
推荐阅读
- c# - 将 SQL 中的空 DateTime 从 01.01.1900 转换为 C# 中的 null,反之亦然
- java - 如何在此 Java Swing 代码中正确地将 GUI 与逻辑分开
- c# - xUnit Equal 两个相同的集合,相同的顺序,相同的类型,返回 false
- c# - 如何在 Kendo Mvc Grid 中发送参数列表
- ios - 为什么我的场景相机在使用 SCNTechnique 时只显示黑屏?
- excel - 如何使用宏将数字格式化为文件名中至少包含 5 位数字?
- reactjs - React Hooks useCallback 导致子级重新渲染
- java - Kotlin 类型不匹配:必需的长期找到的单位
- css - 平滑收缩动画
- java - 抽象类的实例化