C++ 链接器错误 - 未定义引用"到未内联定义的函数
c++ linker error - 'undefined reference' to not inline defined functions
几天来,我试图使用Code::Blocks IDE(在Linux、Ubuntu 64位上)编译一个用C++编写的项目。代码有效,但存在一些链接器错误。我注意到,对于类中未内联定义的函数和其他文件中的函数(类是I*.h文件,这些函数的定义在*.cpp中),我会收到错误"未定义引用"。我试图编写自己的Makefile,但没有帮助。
Makefile:
all: project
project: main.o DList.o Person.o
g++ main.o DList.o Person.o -o project
main.o: main.cpp
g++ -c main.cpp
DList.o: include/DList.cpp
g++ -c include/DList.cpp
Person.o: include/Person.cpp
g++ -c include/Person.cpp
clean:
rm -rf *.o
虽然我在网上读到了一些关于这些错误的消息,但我不知道该怎么办。
//编辑我将Object.cpp和Object.h更改为Person.cpp和Person.h,将*.cpp文件移动到主目录,并更改*.cpp中的#include路径。
错误:
obj/Debug/main.o||In function `main':|
...main.cpp|19|undefined reference to `DListIterator<Person>::go(int)'|
...main.cpp|20|undefined reference to `std::basic_ostream<char, std::char_traits<char> >& operator<< <Person>(std::basic_ostream<char, std::char_traits<char> >&, DList<Person>&)'|
...main.cpp|21|undefined reference to `DList<Person>::~DList()'|
...main.cpp|21|undefined reference to `DList<Person>::~DList()'|
obj/Debug/main.o||In function `DList<Person>::insert(Person&)':|
...include/DList.h|45|undefined reference to `DList<Person>::insert(Person&, DListIterator<Person>&)'|
||=== Build finished: 5 errors, 0 warnings ===|
无论是在命令行中开始构建make,还是在代码::块中使用build函数,都没有区别。
当我将所有代码从*.cpp文件复制到*.h文件时,编译器没有返回任何错误,所以我认为这只是链接器的问题。
看起来您正试图单独编译一个模板。这在一般情况下是不可能的,因为模板只有在使用时才会被实例化,而且它永远不会在DList.cpp
文件中使用。尝试两种方法之一:
- 将
DList
中函数的定义移动到头文件中(这是正常的操作方式) - 将
DList
的一些显式实例化放入DList.cpp
文件中。(示例:template class DList<Person>;
)
问题的完整示例:目前您有:
//DList.h
template<typename T>
class DList {
void insert(T& newPerson);
//etc
};
//DList.cpp
#include "DList.h"
//The when this is being compiled,
//the compiler does not know about Person,
//so it cannot instantiate this function.
template<typename T>
void DList<T>::insert(T& newPerson) {
//implementation
}
//etc
//main.cpp
#include "DList.h"
#include "Person.h"
int main() {
//When this is being compiled, it does not know the
//definition of the out-of-line functions in `DList`,
//so it cannot instantiate them.
DList<Person> people;
people.insert(Person("Joe"));
}
一个可能的解决方案是删除DList.cpp
并将定义放在"DList.hpp"中:
//DList.hpp
template<typename T>
class DList {
void insert(T& newPerson) {
//implementation
}
~DList();
//etc
};
//the implementations can alternatively be
//placed outside the class, but in the header:
template<typename T>
DList<T>::~DList() {
//implementation
}
另一个修复方法是显式实例化DList
(在定义可用的编译单元中):
//DList.cpp
#include "DList.h"
#include "Person.h"
template<typename T>
void DList<T>::insert(T& newPerson) {
//implementation
}
//Explicit instantiation:
template class DList<Person>;
相关文章:
- 在命名空间中定义函数还是限定函数
- 为什么在定义函数之前先声明它
- 使用用户定义函数的字符串反转
- 用户定义函数中的指针和输入
- 这个c++代码是如何在没有定义函数的情况下运行的
- 具有外部"c"和程序集的未定义函数
- 已定义函数时出现 G++ "未定义的引用"错误
- 将自定义函数传递到基抽象类中以延迟执行
- C++使用 rand 定义函数语法
- Arduino:在 loop() 和自定义函数中运行相同的代码时出现问题
- 将具有固定签名的自定义函数名称注入 CRTP
- 使用定义函数模板别名
- 是否可以使用单个定义定义函数的常量和常规版本?(使用模板,自动,decltype等)
- 在内联程序集中定义函数和从 C++ 调用时出现问题
- 在 Metal 着色器代码中,如何定义函数的 in/out 参数变量?
- 声明和定义函数静态会产生"undefined reference to function_name()"
- 朋友定义函数的名称空间是什么
- 介子 对用户定义函数的未定义引用
- 如何使用 "using" 关键字定义函数原型/签名
- 在 C 结构中定义C++函数