[英]Error “undefined reference to” when I compile a C++ code, but the original makefile code compiles
I downloaded a generic AVL implementation from: http://sourceforge.net/projects/standardavl/files/standardavl/0.1/ 我从以下网址下载了一个通用的AVL实现: http : //sourceforge.net/projects/standardavl/files/standardavl/0.1/
The makefile in this project compiles correctly the code. 此项目中的makefile正确编译代码。 The compiler produces the follow output:
编译器生成以下输出:
g++ -pedantic -Wall -O3 -c standardavl.cpp -o standardavl.o
g++ -pedantic -Wall -O3 -c Point.cpp -o Point.o
g++ -o standardavl standardavl.o Point.o
g++ -o genpoints genpoints.o Point.o
The makefile compiles only "standardavl.cpp", because standartavl includes "AvlTree.h" and this file includes "AvlTree.cpp": makefile只编译“standardavl.cpp”,因为standartavl包含“AvlTree.h”,这个文件包含“AvlTree.cpp”:
standardavl.cpp standardavl.cpp
#include "AvlTree.h"
#include "Point.h"
(...)
AvlTree.h AvlTree.h
(...)
#include "AvlTree.cpp"
In my project I removed the last line (#include "AvlTree.cpp") from the file AvlTree.h and compiled individually that files. 在我的项目中,我从文件AvlTree.h中删除了最后一行(#include“AvlTree.cpp”)并单独编译了这些文件。 I do not use the file "standardavl.cpp" also.
我也不使用文件“standardavl.cpp”。 I changed the file Point.h to KeyPair.h and implemented all operators there.
我将文件Point.h更改为KeyPair.h并在那里实现了所有运算符。
My compiler produces the follow output: 我的编译器生成以下输出:
g++ -pedantic -Wall -O3 -c -o ../lib/CPUTimer.o ../lib/CPUTimer.cpp -I. -I../lib
g++ -pedantic -Wall -O3 -c -o ../lib/AvlTree.o ../lib/AvlTree.cpp -I. -I../lib
g++ -pedantic -Wall -O3 -c -o ../lib/keypair.o ../lib/keypair.cpp -I. -I../lib
g++ -pedantic -Wall -O3 -c -o graph.o graph.cpp -I. -I../lib
g++ -pedantic -Wall -O3 -c -o dijkstra.o dijkstra.cpp -I. -I../lib
g++ -pedantic -Wall -O3 -o ../../q1 ../lib/CPUTimer.o ../lib/AvlTree.o ../lib/keypair.o graph.o dijkstra.o questao1.cpp -I. -I../lib
dijkstra.o: In function 'Dijkstra::executeAvl(int)':
dijkstra.cpp:(.text+0x25d): undefined reference to 'AvlTree<KeyPair, std::less<KeyPair>, nil<KeyPair> >::AvlTree()'
(... a lot of errors like above ...)
collect2: ld returned 1 exit status
make[1]: ** [q1] Erro 1
What I am doing wrong here? 我在这做错了什么?
Assuming AvlTree.h declares a template class AvlTree<>
, and AvlTree.cpp defines the implementation: 假设AvlTree.h声明了一个模板类
AvlTree<>
,并且AvlTree.cpp定义了实现:
You can't define the implementation of template code in a separate translation unit (.cpp) -- it must exist where it is used. 您无法在单独的转换单元(.cpp)中定义模板代码的实现 - 它必须存在于使用它的位置。 That's why they included it in the header.
这就是为什么他们把它包含在标题中。 Putting the implementation in a .cpp file to be included was just a design choice to keep the .h file concise.
将实现放在要包含的.cpp文件中只是保持.h文件简洁的设计选择。
(Well, technically you can put the template definition in a separate translation unit, but you must explicitly instantiate it for the template arguments you'll be using. I'm guessing that's not what you want to do.) (从技术上讲,你可以将模板定义放在一个单独的翻译单元中,但是你必须为你将要使用的模板参数显式地实例化它。我猜这不是你想要做的。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.