相应的.c及点.h文件如下:
c.c
#include "cplus.hpp"
#include "c.h"
A *a = new B();
void test()
{
// A *a = new B();
a->test();
}
cplus.cpp
#include <iostream>
#include <string>
#include "cplus.hpp"
using namespace std;
void A::test()
{
cout<<"a"<<endl;
}
void B::test()
{
cout<<"b"<<endl;
}
#ifndef _C_H_
#define _C_H_
#ifdef __cplusplus
extern "C"{
#endif
void test();
#ifdef __cplusplus
}
#endif
#endif
class A{
public:
virtual void test();
};
class B:public A{
public:
void test();
};
CXX=g++
CXXFLAGS= -c
#INCLUDE=
#LIB=
#SUBLIB=
TARGET_NAME=libc_test.so
$(TARGET_NAME):c.o cplus.o
$(CXX) -fPIC -shared -o $@ $^
c.o:c.c c.h cplus.hpp
$(CXX) -fPIC -c $^
cplus.o:cplus.cpp cplus.hpp
$(CXX) -fPIC -c $^
clean:
rm *.o
rm *.gch
rm $(TARGET_NAME)
生成静态链接库的Makefile如下
CXX=g++
CXXFLAGS= -c
#INCLUDE=
#LIB=
#SUBLIB=
AR = ar -r
TARGET_NAME=libc_test.a
$(TARGET_NAME):c.o cplus.o
$(AR) $@ $^
c.o:c.c c.h cplus.hpp
$(CXX) -c $^
cplus.o:cplus.cpp cplus.hpp
$(CXX) -c $^
clean:
rm *.o
rm *.gch
rm $(TARGET_NAME)