【问题标题】:./libbar.so: undefined symbol: __gxx_personality_v0, how to solve it?./libbar.so: undefined symbol: __gxx_personality_v0,怎么解决?
【发布时间】:2021-09-16 06:42:06
【问题描述】:

在制作一个简单的测试用例时,我遇到了另一个问题。请帮帮我。
这是文件。

<<< 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++ c linker runtime-error shared-libraries


    【解决方案1】:

    如何消除错误?

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

    【讨论】:

    • 啊,这很简单!谢谢! (我将 libbar.so 的配方更改为 g++ -shared -o $@ $&lt;。)
    猜你喜欢
    • 1970-01-01
    • 2023-02-08
    • 2016-07-10
    • 2022-06-15
    • 2019-01-15
    • 2022-12-03
    • 2023-02-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多