Question

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

Was it helpful?

Solution

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.

OTHER TIPS

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?

FILE is a typedef of _IO_FILE. 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)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top