By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,918 Members | 2,258 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,918 IT Pros & Developers. It's quick & easy.

undefined reference to library

P: n/a
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
Jul 23 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Christian Christmann wrote:
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?

Jul 23 '05 #2

P: n/a
>> 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?

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
Jul 23 '05 #3

P: n/a
Christian Christmann wrote:
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?


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

--
Artie Gold -- Austin, Texas
http://it-matters.blogspot.com (new post 12/5)
http://www.cafepress.com/goldsays
Jul 23 '05 #4

P: n/a
>> 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

Jul 23 '05 #5

P: n/a
Christian Christmann wrote:
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?

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

--
Artie Gold -- Austin, Texas
http://it-matters.blogspot.com (new post 12/5)
http://www.cafepress.com/goldsays
Jul 23 '05 #6

P: n/a
Christian Christmann wrote:
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?

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.

Jul 23 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.