gpt4 book ai didi

linux - 如何解决构建的.so文件出现undefined symbol错误?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:32:05 36 4
gpt4 key购买 nike

我想在 Ubuntu16.04 中构建一个 .so 文件。 gcc的版本是:

gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.11)

我在同一个目录中有 student_info.cppstudent_info.hMakefile
student_info.h的内容是:

#include <iostream>

using namespace std;

class student_info
{
public:
student_info();

private:
char* name;
int score;

public:
void setName(char* name)
{
this->name = name;
}

void setScore(int score)
{
this->score = score;
}

char* getName()
{
return this->name;
}

int getScore()
{
return this->score;
}

};

student_info.cpp 是:

#include <iostream>
#include "student_info.h"

using namespace std;

extern "C"
{
student_info* student_info_new() {return new student_info();}
}

Makefile 是:

student_info.so: student_info.cpp student_info.h
g++ -std=c++11 -shared -fPIC -o student_info.so student_info.cpp

执行make 命令后。我得到了 student_info.so。但是在使用 ldd -r student_info.so 之后,我得到以下错误:

linux-vdso.so.1 =>  (0x00007fff269fa000)
libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f2111228000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f2111012000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f2110c48000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f211093f000)
/lib64/ld-linux-x86-64.so.2 (0x00007f21117ac000)
undefined symbol: _ZN12student_infoC1Ev (./student_info.so)

如何解决这个 undefined symbol 错误?谢谢。

最佳答案

ldd 表示 student_info 的默认构造函数未定义。您需要在 student_info.hstudent_info.cpp 中提供默认构造函数的定义。例如:

class student_info
{
public:
student_info() : name(), score() {} // Declaration and definition.

private:
char* name;
int score;
// ...
};

关于linux - 如何解决构建的.so文件出现undefined symbol错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56458642/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com