Как решить неопределенную ошибку символа во встроенном .so файле? - PullRequest
1 голос
/ 05 июня 2019

Я хочу собрать файл .so в Ubuntu16.04.Версия gcc:

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

У меня есть student_info.cpp , student_info.h и Makefile в том же каталоге.
Содержимое 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 is:

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)

Как я могу решить эту неопределенную ошибку символа?Спасибо.

1 Ответ

1 голос
/ 05 июня 2019

ldd говорит, что конструктор по умолчанию student_info не определен. Необходимо указать определение конструктора по умолчанию в student_info.h или student_info.cpp. E.g.:

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

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