virutal function in static library

P

pipehappy

Hi there,

I have came across a problem to make a library with virtual function.
I have example code listed below and if I compile this way it works:
g++ main.cpp a.cpp -o main
But when I want to have a static library it reports error:
g++ -c a.cpp -o liba.o
ar rcs liba.a liba.o
g++ -L. -la main.cpp -o main
/tmp/ccqQZO7f.o:(.rodata._ZTV4Base[vtable for Base]+0x10): undefined
reference to `__cxa_pure_virtual'

Anyone has any suggestion about this?

Thx

Here is the code example:

a.h:

#ifndef HE
#define HE
class Base {
public:
virtual double func(double)=0;
};

class Child: public Base{
public:
virtual double func(double);
};
#endif


a.cpp:

#include "a.h"

double Child::func(double a) {
return a+ 3;
}


main.cpp:

#include "a.h"

int
main (void) {
Child child1;
child1.func(0.3);
}
 
I

Ian Collins

Hi there,

I have came across a problem to make a library with virtual function.
I have example code listed below and if I compile this way it works:
g++ main.cpp a.cpp -o main
But when I want to have a static library it reports error:
g++ -c a.cpp -o liba.o
ar rcs liba.a liba.o
g++ -L. -la main.cpp -o main
/tmp/ccqQZO7f.o:(.rodata._ZTV4Base[vtable for Base]+0x10): undefined
reference to `__cxa_pure_virtual'

This is a tool question, rather than a C++ one.
Anyone has any suggestion about this?

Link in the correct order!

g++ -L. main.cpp -la -o main
 
P

pipehappy

Hi there,
I have came across a problem to make a library with virtual function.
I have example code listed below and if I compile this way it works:
g++ main.cpp a.cpp -o main
But when I want to have a static library it reports error:
g++ -c a.cpp -o liba.o
ar rcs liba.a liba.o
g++ -L. -la main.cpp -o main
/tmp/ccqQZO7f.o:(.rodata._ZTV4Base[vtable for Base]+0x10): undefined
reference to `__cxa_pure_virtual'

This is a tool question, rather than a C++ one.
Anyone has any suggestion about this?
Thx for quick reply.
Link in the correct order!

g++ -L. main.cpp -la -o main
 

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,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top