problem with ar and C++ (?)

S

satya

hi,
I am using GNU g++ and GNU ar to create a C++ class library. I am
facing the following problem:

1.My class library has 3 classes say x,y,z organized in (x.h,x.cpp)
(y.h,y.cpp) (z.h,z.cpp).
2. Now I compiled all the classes using g++ -c x.cpp etc. obtaining
x.o y.o and z.o
3. I wrote a test file test.cpp using these classes.
4. I compiled the test file like this: g++ x.o y.o z.o test.cpp --
this was succesful -- produced an a.out
5. Now I made a library: ar -cr libmyclass.a x.o y.o z.o
6. g++ libmyclass.a test.cpp --> Undefined reference to x() !
(constructor)
7. I tried this too: g++ -L. -lmyclass test.cpp --> same error!

Does anyone know why ?

thanks!!
satya.
 
V

Victor Bazarov

satya said:
I am using GNU g++ and GNU ar to create a C++ class library. I am
facing the following problem:

1.My class library has 3 classes say x,y,z organized in (x.h,x.cpp)
(y.h,y.cpp) (z.h,z.cpp).
2. Now I compiled all the classes using g++ -c x.cpp etc. obtaining
x.o y.o and z.o
3. I wrote a test file test.cpp using these classes.
4. I compiled the test file like this: g++ x.o y.o z.o test.cpp --
this was succesful -- produced an a.out
5. Now I made a library: ar -cr libmyclass.a x.o y.o z.o
6. g++ libmyclass.a test.cpp --> Undefined reference to x() !
(constructor)
7. I tried this too: g++ -L. -lmyclass test.cpp --> same error!

Does anyone know why ?

Yes, somebody in 'gnu.g++.help' knows. Here your g++-specific question
is off-topic.

V
 
G

Grizlyk

satya said:
6. g++ libmyclass.a test.cpp --> Undefined reference to x() !

switch objects
g++ test.cpp libmyclass.a

Linker serching library only for requested name, so first place "user"
objects to declare externals, then library as "provider of requested
externals".
 
R

Ron Natalie

satya said:
6. g++ libmyclass.a test.cpp --> Undefined reference to x() !
(constructor)
7. I tried this too: g++ -L. -lmyclass test.cpp --> same error!

Does anyone know why ?
The order is important. Put test.cpp before the libraries.
 

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,533
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top