c++ makefile 析构函数未定义引用
这对我来说是一个难题:
c++ undefined reference to destructor
这就是问题,代码与链接相同,完整makefile 和错误在这里。链接的答案确实有帮助,但只是为了强调我在意想不到的地方有一些标题。
makefile
CXX = g++
BIN = .
LIBS = -L.
INCLUDE = -I . -I
CXXFLAGS = -pipe # -O6
LFLAGS = -lm
GeomTest_OBJS = geomTest.o SASAGeometry.o
geomTest_source = SASAGeometry.cpp SASAGeometry.h sasa_transformMatrix.cpp sasa_transformMatrix.h geomSetup.cpp
SASAGeometry.o : SASAGeometry.cpp SASAGeometry.h sasa_transformMatrix.cpp sasa_transformMatrix.h
geomTest.o : geomSetup.cpp
geomTest : $(GeomTest_OBJS) makefile
$(CXX) -o geomTest.o -o SASAGeometry.o $(LIBS) $(INCLUDE) $(CXXFLAGS) $(geomTest_source) $(LFLAGS)
$(CXX) $(LIBS) $(INCLUDE) $(CXXFLAGS) -o $(BIN)/geomTest geomTest.o SASAGeometry.o $(LFLAGS)
clean : \rm *.o *~ p1
我已经声明并实例化了析构函数,并且没有(允许编译器执行其操作)
错误,
geomSetup.cpp:(.text+0x5ab): undefined reference to `SASAGeometry::~SASAGeometry()'
geomSetup.cpp:(.text+0x5cd): undefined reference to `SASAGeometry::~SASAGeometry()'
没有其他错误。 (抱歉,如果最好是点击看似已排序的问题而不是链接到它们,但我在这方面的努力没有奏效)
提前致谢!
编辑:
确保编译正确的源文件,而不是您忘记复制的旧文件。
嗨@trojanfoe和@Kerrick SB,这两个答案都让我查看了我的makefile并意识到它很丑陋。这是修改后的版本。不过,我仍然收到相同的“对析构函数的未定义引用”错误:
makefile:
CXX = g++
BIN = .
LIBS = -L.
INCLUDE = -I.
CXXFLAGS = -pipe # -O6
LDFLAGS = -lm
GeomTest_OBJS = sasa_transformMatrix.o SASAGeometry.o geomSetup.o
SASAGeometry.o : SASAGeometry.cpp SASAGeometry.h
sasa_transformMatrix.o : sasa_transformMatrix.cpp sasa_transformMatrix.h
geomSetup.o : geomSetup.cpp
geomTest : $(GeomTest_OBJS)
$(CXX) $(CXXFLAGS) $(LDFLAGS) -o $@ $+
clean : \rm *.o *~ p1
错误:
geomSetup.o: In function `main':
geomSetup.cpp:(.text+0x5ab): undefined reference to `SASAGeometry::~SASAGeometry()'
geomSetup.cpp:(.text+0x5cd): undefined reference to `SASAGeometry::~SASAGeometry()'
collect2: ld returned 1 exit status
make: *** [geomTest] Error 1
为什么链接器会抱怨析构函数而不是构造函数或类中的任何其他方法/函数?
再次感谢!
This is a puzzle to me:
c++ undefined reference to destructor
That's the issue, the code is the same as the link, full makefile and errors here. The linked answers did help but only to highlight that I had some headers in a place I did not expect.
makefile
CXX = g++
BIN = .
LIBS = -L.
INCLUDE = -I . -I
CXXFLAGS = -pipe # -O6
LFLAGS = -lm
GeomTest_OBJS = geomTest.o SASAGeometry.o
geomTest_source = SASAGeometry.cpp SASAGeometry.h sasa_transformMatrix.cpp sasa_transformMatrix.h geomSetup.cpp
SASAGeometry.o : SASAGeometry.cpp SASAGeometry.h sasa_transformMatrix.cpp sasa_transformMatrix.h
geomTest.o : geomSetup.cpp
geomTest : $(GeomTest_OBJS) makefile
$(CXX) -o geomTest.o -o SASAGeometry.o $(LIBS) $(INCLUDE) $(CXXFLAGS) $(geomTest_source) $(LFLAGS)
$(CXX) $(LIBS) $(INCLUDE) $(CXXFLAGS) -o $(BIN)/geomTest geomTest.o SASAGeometry.o $(LFLAGS)
clean : \rm *.o *~ p1
I have both declared and instantiated the destructor AND not (allowing the compiler to do its thing)
error
geomSetup.cpp:(.text+0x5ab): undefined reference to `SASAGeometry::~SASAGeometry()'
geomSetup.cpp:(.text+0x5cd): undefined reference to `SASAGeometry::~SASAGeometry()'
no other errors. (sorry if its preferable to bump seemingly sorted issues rather than link to them, but my effort at that didn't work)
Thanks in advance!
EDIT:
MAKE SURE TO COMPILE THE CORRECT SOURCE FILES, NOT THE OLD ONES YOU FORGOT TO COPY.
Hi @trojanfoe and @Kerrick SB, both answers made me look at my makefile and realise it was ugly. Here is the revised version. I am still getting the same 'undefined reference to destructor' error though:
makefile:
CXX = g++
BIN = .
LIBS = -L.
INCLUDE = -I.
CXXFLAGS = -pipe # -O6
LDFLAGS = -lm
GeomTest_OBJS = sasa_transformMatrix.o SASAGeometry.o geomSetup.o
SASAGeometry.o : SASAGeometry.cpp SASAGeometry.h
sasa_transformMatrix.o : sasa_transformMatrix.cpp sasa_transformMatrix.h
geomSetup.o : geomSetup.cpp
geomTest : $(GeomTest_OBJS)
$(CXX) $(CXXFLAGS) $(LDFLAGS) -o $@ $+
clean : \rm *.o *~ p1
error:
geomSetup.o: In function `main':
geomSetup.cpp:(.text+0x5ab): undefined reference to `SASAGeometry::~SASAGeometry()'
geomSetup.cpp:(.text+0x5cd): undefined reference to `SASAGeometry::~SASAGeometry()'
collect2: ld returned 1 exit status
make: *** [geomTest] Error 1
Why would the linker complain about the destructor and not the constructor or any other methods/functions in the class?
Thanks again!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的可执行输出文件和您的目标文件
geomTest.o
具有相同的名称!当链接器覆盖目标文件时,这肯定会给您带来麻烦。将其更改为
$(CXX) -o geomTest ...
,甚至更好地更改为$(CXX) $@ ...
以避免将来出现此类问题。事实上,您完全滥用了链接器命令:您只想有一个单个
-o
选项,并且直接列出对象,没有标志:在Makefile中,帮自己一个忙,使用魔法宏:
这里
-o $@
匹配目标名称,即-o myprog
,而$+
匹配所有依赖名称,即main.o foo.o bar.o
。使用变量背后的指导思想是,如果可以的话,你永远不应该多次说同一件事。因此,您可以将
myprog: $(MyObjects)
作为规则,然后在命令行中使用$+
以避免重复MyObjects
。这提高了局部性和可维护性。Your executable output file and your object file
geomTest.o
have the same name! That's bound to get you into trouble when the linker overwrites the object file.Change it to
$(CXX) -o geomTest ...
, or better even to$(CXX) $@ ...
to avoid such problems in the future.In fact, you are misusing the linker command altogether: you just want to have one single
-o
option, and the objects are listed directly, without flags:Within the Makefile, do yourself a favour and use magic macros:
Here
-o $@
matches the target name, i.e.-o myprog
, and$+
matches all the dependent names, i.e.main.o foo.o bar.o
.The guiding idea behind using variables is that you should never say the same thing more than once if you can help it. So you can have
myprog: $(MyObjects)
as the rule, but then use$+
in the command line to avoid repetition ofMyObjects
. This improves locality and maintainability.在我看来,
geomTest
目标中的-o SASAGeometry.o
非常可疑 - 您正在覆盖依赖项对象之一(实际上是两个依赖项)。试试这个:(
注意
$LDFLAGS
是保存链接器标志的常规变量,而不是与lex
工具一起使用的$LFLAGS
)。The
-o SASAGeometry.o
in thegeomTest
target looks highly suspect to me - you are overwriting one of the dependency objects (actually both dependencies).Try this:
(note that
$LDFLAGS
is the conventional variable in which to hold linker flags, not$LFLAGS
which is used with thelex
tool).