编译C ++源文件时出现链接器错误,未定义对类函数的引用

我创建了一个样本最小目录结构,通过它可以重新创建链接器错误问题。 下面是我所拥有的目录结构:

person = Person.nodes.first_or_none(name='James')
if person == None:
    personNew = Person(name='James').save()

每个文件的内容如下:

tmp.hxx

satyam@sat:~/cpp/build$ ls -R
.:
src  template

./src:
main.cxx  main.o

./template:
tmp.cxx  tmp.hxx  tmp.o

tmp.cxx

template <typename T>
class Array {
private:
    T *ptr;
    int size;
public:
    Array(T arr[],int s);
    void print();
};

main.cxx

#include <iostream>
#include "tmp.hxx"

using namespace std;


template <typename T>
Array<T>::Array(T arr[],int s) {
        ptr = new T[s];
        size = s;
        for(int i = 0; i < size; i++)
                ptr[i] = arr[i];
}

template <typename T>
void Array<T>::print() {
        for (int i = 0; i < size; i++)
                cout<<" "<<*(ptr + i);
        cout<<endl;
}

首先,我移至 template 目录,并使用以下命令制作tmp.cxx的目标文件:

#include <tmp.hxx>

int main() {
    int arr[5] = {1,2,3,4,5};
    Array<int> a(arr,5);
    a.print();
    return 0;
}

然后我移到 src 目录,并使用以下命令制作main.cxx的目标文件:

satyam@sat:~/cpp/build/template$ g++ -c tmp.cxx

最后,我尝试使用以下命令在 / cpp / build 目录中创建二进制文件,并得到以下链接器错误:

satyam@sat:~/cpp/build/src$ g++ -c -I ../template/ main.cxx

我尝试查看SO的其他线程,发现在main.cxx中包含tmp.cxx文件可以解决此问题,但是我真的不想使用该选项,因为它看起来不正确。不知道在哪里看。

任何与此有关的帮助都是很好的。

wuxiaolong0815 回答:编译C ++源文件时出现链接器错误,未定义对类函数的引用

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3164777.html

大家都在问