g++ 对构造函数的未定义引用
我正在针对预编译的库编译和链接 cpp 文件,并且收到“未定义的引用”错误。
首先,这是命令(有问题的库是quicknet3,我正在编译的程序是trapper):
g++ -w -g -I。 -g -O3 -pipe -Wall -I/home/install/x86_64/include/quicknet3 -L/home/install/x86_64/lib -lquicknet3 -lintvec -lfltvec -o trapper trapper.cpp CMyException.cpp
这是未定义的引用错误:
/tmp/ccFuVczF.o:在函数“main”中: trapper.cpp:1731: 对 'QN_InFtrLabStream_PFile::QN_InFtrLabStream_PFile(int, char const*, _IO_FILE*, int)' 的未定义引用
trapper.cpp(第 1731 行)中的调用是:
IN_PFILE = new QN_InFtrLabStream_PFile (0, "", fp, 1);
其中 fp
是 FILE *
,分配为 fopen
的结果提前打电话。
被调用的构造函数在相关头文件 (QN_Pfile.h) 中定义,如下所示:
class QN_InFtrLabStream_PFile : public QN_InFtrLabStream
{
公众:
QN_InFtrLabStream_PFile(int a_debug, const char* a_dbgname, FILE* a_file, int a_indexed);
(...其他声明...) }
构造函数的定义确实在 QN_Pfile.cc 中给出:
QN_InFtrLabStream_PFile::QN_InFtrLabStream_PFile(int a_debug,const char* a_dbgname, FILE* a_file, int a_indexed) : log(a_debug, "QN_InFtrLabStream_PFile", a_dbgname),文件(a_file),索引(a_indexed),缓冲区(NULL),sentind(NULL) {
(...通常的构造函数:P ...) 所以
我自己编译了quicknet3库,没有错误,并将其安装到/home/install/x86_64/lib/libquicknet3.a
,我不明白为什么trapper.cpp的调用无法找到对此构造函数定义的引用。 -L/home/install/x86_64/lib -lquicknet3
的 g++ 参数应该可以解决问题,对吗?
有任何想法吗?
谢谢, 罗伊
I'm compiling and linking a cpp file against a pre-compiled library, and I'm getting an "undefined reference" error.
Firstly, this is the command (the library in question is quicknet3, the program I'm compiling is trapper):
g++ -w -g -I. -g -O3 -pipe -Wall -I/home/install/x86_64/include/quicknet3 -L/home/install/x86_64/lib -lquicknet3 -lintvec -lfltvec -o trapper trapper.cpp CMyException.cpp
Here's the undefined reference error:
/tmp/ccFuVczF.o: In function 'main':
trapper.cpp:1731: undefined reference to 'QN_InFtrLabStream_PFile::QN_InFtrLabStream_PFile(int, char const*, _IO_FILE*, int)'
The call in trapper.cpp (line 1731) is:
IN_PFILE = new QN_InFtrLabStream_PFile(0, "", fp, 1);
where fp
is a FILE *
, assigned as the result of an fopen
call beforehand.
The constructor being called is defined in the relevant header file (QN_Pfile.h), as follows:
class QN_InFtrLabStream_PFile : public
QN_InFtrLabStream
{
public:
QN_InFtrLabStream_PFile(int a_debug, const char* a_dbgname, FILE* a_file, int a_indexed);
(... other declarations ...)
}
The definition of the constructor is indeed given in QN_Pfile.cc:
QN_InFtrLabStream_PFile::QN_InFtrLabStream_PFile(int a_debug,const char* a_dbgname, FILE* a_file, int a_indexed) : log(a_debug, "QN_InFtrLabStream_PFile", a_dbgname),file(a_file),indexed(a_indexed),buffer(NULL),sentind(NULL)
{
(... the usual constructor stuff :P ...)
}
I compiled the quicknet3 library myself, without error, and installed it to /home/install/x86_64/lib/libquicknet3.a
So, I can't understand why the call from trapper.cpp is unable to find the reference to this constructor definition. The g++ arguments of -L/home/install/x86_64/lib -lquicknet3
should do the trick, right?
Any ideas?
Thanks,
Roy
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我注意到您正在混合
FILE*
和_IO_FILE*
。 我对后者不熟悉,你确定它们是同一个吗?I notice that you're mixing
FILE*
and_IO_FILE*
. I'm not familiar with the latter, are you sure they're one and the same?一个快速的解决方法是将 /home/install/x86_64/lib/libquicknet3.a 添加到 g++ 命令行。
我想进一步调查,如果 g++ 选择 libquicknet3 的另一个副本,您可以将 -v 传递给 g++,以便它输出其搜索路径。
A quick workaround is to add /home/install/x86_64/lib/libquicknet3.a to g++ commandline.
I you want to investigate further, if g++ is picking another copy of libquicknet3, you can pass -v to g++ so it will output its searching paths.
FILE 是
_IO_FILE
的类型定义。 您的链接器将其视为独特的类型。您可以尝试:
看看这是否可以解决您的构造函数。
(FILE 在 stdio.h 中定义,_IO_FILE 在 libio.h 中定义,如果您有兴趣)
FILE is a typedef of
_IO_FILE
. Your linker is treating it as a unique type.You could try:
to see if this resolve your constructor.
(FILE is defined in stdio.h, _IO_FILE in libio.h if you're interested)