首页 > 解决方案 > ./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++clinkerruntime-errorshared-libraries

解决方案


我怎样才能消除错误?

将您的应用程序与 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抓住了问题。


推荐阅读