简体   繁体   English

g ++对构造函数的未定义引用

[英]g++ undefined reference to constructor

I'm compiling and linking a cpp file against a pre-compiled library, and I'm getting an "undefined reference" error. 我正在针对预编译的库编译并链接cpp文件,但出现“未定义引用”错误。

Firstly, this is the command (the library in question is quicknet3, the program I'm compiling is trapper): 首先,这是命令(有问题的库是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

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: trapper.cpp中的调用(第1731行)为:

IN_PFILE = new QN_InFtrLabStream_PFile(0, "", fp, 1);

where fp is a FILE * , assigned as the result of an fopen call beforehand. 其中fpFILE * ,它是预先通过fopen调用分配的。

The constructor being called is defined in the relevant header file (QN_Pfile.h), as follows: 调用的构造函数在相关的头文件(QN_Pfile.h)中定义,如下所示:

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_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 我自己编译了quicknet3库,没有错误,并将其安装到/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. 因此,我无法理解为什么trapper.cpp的调用无法找到对此构造函数定义的引用。 The g++ arguments of -L/home/install/x86_64/lib -lquicknet3 should do the trick, right? -L/home/install/x86_64/lib -lquicknet3的g ++参数应该可以解决问题,对吗?

Any ideas? 有任何想法吗?

Thanks, Roy 谢谢罗伊

I notice that you're mixing FILE* and _IO_FILE* . 我注意到您正在混合FILE*_IO_FILE* I'm not familiar with the latter, are you sure they're one and the same? 我对后者不熟悉,您确定它们是相同的吗?

FILE is a typedef of _IO_FILE . FILE是_IO_FILE的typedef。 Your linker is treating it as a unique type. 您的链接器将其视为唯一类型。

You could try: 您可以尝试:

IN_PFILE = new QN_InFtrLabStream_PFile(0, "", (FILE *)fp, 1);

to see if this resolve your constructor. 看看这是否可以解决您的构造函数。

(FILE is defined in stdio.h, _IO_FILE in libio.h if you're interested) (如果您有兴趣,则在stdio.h中定义文件,在libio.h中的_IO_FILE中定义文件)

A quick workaround is to add /home/install/x86_64/lib/libquicknet3.a to g++ commandline. 一种快速的解决方法是将/home/install/x86_64/lib/libquicknet3.a添加到g ++命令行。

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. 我想进一步研究,如果g ++选择了另一个libquicknet3副本,则可以将-v传递给g ++,以便输出其搜索路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM