undefined reference to library

  • Thread starter Christian Christmann
  • Start date
C

Christian Christmann

Hi,

I've created a file htable.cpp which I compiled to htable.o and
than added with "ar" to a library libbasics.a in the directory
lib/Linux/. Now I want to compile another file ir3tst.cpp which
uses the functions of htable.cpp. I used the command:

"g++ -Wall ir3tst.cpp -o bin/Linux/ir3tst -Llib/Linux -lbasics"

and get some errors "undefined reference to ..." the functions from htable.cpp.
What's wrong?

Chris
 
R

Rolf Magnus

Christian said:
Hi,

I've created a file htable.cpp which I compiled to htable.o and
than added with "ar" to a library libbasics.a in the directory
lib/Linux/. Now I want to compile another file ir3tst.cpp which
uses the functions of htable.cpp. I used the command:

"g++ -Wall ir3tst.cpp -o bin/Linux/ir3tst -Llib/Linux -lbasics"

and get some errors "undefined reference to ..." the functions from
htable.cpp. What's wrong?

Which error exactly? Is it generated by the compiler or the linker?
You don't write anything about the header. Did you #include it?
 
C

Christian Christmann

I've created a file htable.cpp which I compiled to htable.o and than
Which error exactly? Is it generated by the compiler or the linker? You
don't write anything about the header. Did you #include it?


I get eg. the error message
" undefined reference to `HashTable<char*>::Keylist()'"
where Keylist() is a function in htable.cpp.

I don't know if it's a compiler or a linker problem? How can
I figure this out? However, I suppose it's a linker error since I
can compile the file with "g++ -c ..." without any problems.

Yes, I included the header file of htable.cpp in the file
ir3tst.cpp.

htable.cpp is a template class. Could this be a problem?

Thanks

Chris
 
A

Artie Gold

Christian said:
I get eg. the error message
" undefined reference to `HashTable<char*>::Keylist()'"
where Keylist() is a function in htable.cpp.

I don't know if it's a compiler or a linker problem? How can
I figure this out? However, I suppose it's a linker error since I
can compile the file with "g++ -c ..." without any problems.

Yes, I included the header file of htable.cpp in the file
ir3tst.cpp.

htable.cpp is a template class. Could this be a problem?

Yes. Absent implementation of the `export' keyword, templated code must
be visible to any translation unit that makes use of it (you can think
of templated code as instructions to the compiler to generate
appropriate concrete code as needed).

The long and short of it is that -- with the above caveat -- you need to
put templates in headers.

HTH,
--ag
 
C

Christian Christmann

htable.cpp is a template class. Could this be a problem?
The long and short of it is that -- with the above caveat -- you need to
put templates in headers.

Thank you for your answer. Ok, I followed your advise with the templates
in headers as follows:

For a class ir3tst.cpp (header file "ir3.def.h" included) I want to use
the functions of a template class "htable.cpp". The header file of
"htable.cpp" is "basic.h" and is included to "htable.cpp" . Since
the template has to be available to the compiler at compile time, I use a
header file "ir3.def.h" which contains "#include "basic.h" and "#include
"htable.cpp".
However, compiling "ir3tst.cpp" I still get error messages like

"undefined reference to `HashTable<char*>::Keylist()'"

where Keylist() is a function of the template class "htable.cpp".
Did I forget something?
HTH,
--ag

Chris
 
A

Artie Gold

Christian said:
Thank you for your answer. Ok, I followed your advise with the templates
in headers as follows:

For a class ir3tst.cpp (header file "ir3.def.h" included) I want to use
the functions of a template class "htable.cpp". The header file of
"htable.cpp" is "basic.h" and is included to "htable.cpp" . Since
the template has to be available to the compiler at compile time, I use a
header file "ir3.def.h" which contains "#include "basic.h" and "#include
"htable.cpp".
However, compiling "ir3tst.cpp" I still get error messages like

"undefined reference to `HashTable<char*>::Keylist()'"

where Keylist() is a function of the template class "htable.cpp".
Did I forget something?
Evidently. ;-)

Could you post a *minimal* extract of compilable code -- or code that
you think should compile -- that exhibits this? Otherwise we're just
guessing.

HTH,
--ag
 
R

Rolf Magnus

Christian said:
I get eg. the error message
" undefined reference to `HashTable<char*>::Keylist()'"
where Keylist() is a function in htable.cpp.

I don't know if it's a compiler or a linker problem?

That's why you should post the original and _complete_ error message.
How can I figure this out? However, I suppose it's a linker error since I
can compile the file with "g++ -c ..." without any problems.

Then it is a linker error.
Yes, I included the header file of htable.cpp in the file
ir3tst.cpp.

htable.cpp is a template class. Could this be a problem?

Yes. Instead of linking it to your program, try to #include it at the end of
the header.
 

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,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top