The infamous undefined reference to error when library is available

P

pauls631

Hi,
I am using a compiled .a library file in Linux (particularly HTK/ATK
library, and using QTCreator GUI)
When I try to compile the code, I get the "undefined reference to
somefunction" error. However, I add the library with -l parameter, and
using nm I see that symbols for the function is available in the .a
file. What step may I be missing?
Thanks...
 
N

Neelesh

Hi,
I am using a compiled .a library file in Linux (particularly HTK/ATK
library, and using QTCreator GUI)
When I try to compile the code, I get the "undefined reference to
somefunction" error. However, I add the library with -l parameter, and
using nm I see that symbols for the function is available in the .a
file. What step may I be missing?
Thanks...


This is off-topic in this group. However understand that "-l" option
specifies libraired to link with, wheereas you are getting a
compilation error. This error is most likely due to the fact that you
haven't included the header file that declares this function, or
haven't declared the function at all before its usage.
 
J

James Kanze

This is not really a C++ question. You may have better luck
in a group that discusses your tool chain. (You're probably
using the GNU tool chain.)

Or one for the library in question---the "installation"
instructions should cover the steps necessary to link it.
The archive's (.a file's) directory may not be in your library
path,

With g++, at least, he'd get an error from the linker saying
that the library could not be found.
or the library may be seen by the linker before the
object (.o file) that needs somefunction. Order counts; don't
list the library until after the objects that use it.

Order counts with some linkers, not with others. (I've even
used one where order of the object files in the library
counted.)
For static archives, you don't really need -l, anyway.
Include the archive on the command-line, just as though it
were an object or source file:
g++ -o main some_source.cc some_object.o some_archive.a
"Undefined reference" is a linker error, not a compiler error.
(You could write a compiler that complained of undefined
references, but you'd have to be a special kind of mean to do
it. The terms "undefined" and "reference" both have technical
meaning in C++.)

Whereas "linker" doesn't:). The C++ standard doesn't speak of
a separate preprocessor, compiler or linker; just an
"implementation". (Practically speaking, of course: phases 1-6
are commonly called the "preprocessor", phase 7 is the compiler
and phase 9 the linker. Where phase 8 fits in is somewhat open,
in that it is only relevant when templates are involved, and the
commun terminology was established before templates. Most
current implementations do phase 8 in the "compiler", however.)

(Note that the above are really technical details. For a user
lambda, Jerry's statement is correct.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,054
Latest member
LucyCarper

Latest Threads

Top