[Backup use] Makefile write dynamic link libraries and static link libraries Demo

Source: Internet
Author: User

The corresponding. C and dot. h files are as follows:

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;
}


C.h

#ifndef _c_h_
#define _C_H_

#ifdef __cplusplus
extern "C" {
#endif
	void Test ();
#ifdef __cplusplus
}
#endif

#endif


Cplus.hpp

Class a{public
:
	virtual void Test ();

Class B:public a{public
:
	void Test ();


The makefile for generating the dynamic link library is as follows

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)


The makefile for generating a static link library is as follows

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)




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.