`
love19820823
  • 浏览: 934264 次
文章分类
社区版块
存档分类
最新评论

A Template for C-Language Library Creation - 一个创建C语言运行库的模板

 
阅读更多

A Template for C-Language Library Creation

一个创建C语言运行库的模板

We suppose the library we want to create is libElec.


1)Create a folder in your disk, such as D:/libElec
2)Create a sub-directory in D:/libElec, such as D:/libElec/src.
We put all codes files in this folder.
3)Create some files in src folder. In this example, we create
the following files,

public header files:
libElec.h - the only file to be included, which has
included the below files:
libElecType.h
libElecErrno.h
libElecPole.h
libElecWire.h

So we can look all files prefixed with libElec- as public files
that must be redistributed to the end users.

Internal headers and sources which are invisible to users:
Elec.h
ElecErrno.h
ElecPole.h
ElecWire.h

Elec.c
ElecErrno.c
ElecPole .c
ElecWire.c


4)We illustrate here some key steps using Visual C++ to create a
win32 DLL project for compiling the libElec.dll on the Windows
platform:

Open Microsoft Visual Studio 2005/2008/2010, select command menus
or buttons by the following sequence:

File->New->Project->Win32/Win32 Project:
Note that:
Name=libElec32
Location=D:/libElec
and then press next button, select Application Settings on the
left, make sure the following two items are checked:
DLL
Empty project
To this point we have created a empty dll project named libElec32.
5)We have to do some a little tedious jobs since we dislike what VC++
has done for us,
First add all existing .h/.c files listed above to the project.

Open project property pages by click menu project->properties, select
general in Congiuration Properties on the left, and notice the
settings showed on the right, change the Character Set from "Use
Unicode Character Set" to "Not Set" which means use ansi default.

Trim Ouptput Directory form "$(SolutionDir)$(ConfigurationName)" to
"$(ConfigurationName)".

Select Code Generation under C/C++ branch on the left and select
"Multi-threaded Debug (/MTd)" for Runtime Library on the right.
Note that in release Configuration this selection is "Multi-threaded (/MT)".

Select Preprocessor under C/C++ branch, and modify the Preprocessor
Definitions setting:
LIBELEC32_EXPORTS -> LIBELEC_EXPORTS
which must be same as defined in the file libElec.h.

Make sure that the above changes are made for Debug and Release.

6) It's time to unmask what should happen to our files,

--------------------------------------------------------------------------------
/*******************************************************************************
* libElec.h - a demo for c lib
*
* Copyright (C) ?-? by cheungmine <cheungmine@gmail.com>,
* All rights reserved.

* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation, either version 2.1 of the License, or
* (at your option) any later version.

* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.

* You should have received a copy of the GNU Lesser General Public License
* along with this program.
******************************************************************************/
#ifndef INCLUDED_LIBELEC_H
#define INCLUDED_LIBELEC_H

#ifndef LIBELECAPI
#ifdef _MSC_VER
#if defined(LIBELEC_EXPORTS)||(defined(_WIN32_WCE)&&defined(LIBELEC_WCE_EXPORTS))
#define LIBELECAPI __declspec(dllexport)
#else
#define LIBELECAPI __declspec(dllimport)
#endif
#else
#define LIBELECAPI
#endif
#endif

#ifdef __cplusplus
extern "C" {
#endif

#include "libElecType.h"

#include "libElecErrno.h"

#include "libElecPole.h"

#include "libElecWire.h"

#ifdef __cplusplus
}
#endif

#endif /* INCLUDED_LIBELEC_H */
--------------------------------------------------------------------------------
/**
* libElecType.h - public types definitions
*/
#ifndef INCLUDED_LIBELECTYPE_H
#define INCLUDED_LIBELECTYPE_H

#include <math.h> /* libm.a MUST be linked on linux */

#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif

#ifndef ELEC_PI
#define ELEC_PI M_PI
#endif

#ifndef ELEC_2PI
#define ELEC_2PI (M_PI*2)
#endif

#ifndef IN
#define IN
#endif

#ifndef OUT
#define OUT
#endif

/******************************************************************************
* *
* Public Types Definitions *
* *
******************************************************************************/

typedef struct _tElecPole *ELEC_POLE;

typedef struct _tElecWire *ELEC_WIRE;





#endif /* INCLUDED_LIBELECTYPE_H */
--------------------------------------------------------------------------------
/**
* libElecErrno.h - Error Codes & Messages Definitions
* implemented in ElecErrno.h/ElecErrno.c
*/
#ifndef INCLUDED_LIBELECERRNO_H
#define INCLUDED_LIBELECERRNO_H

/******************************************************************************
* *
* Error Codes & Messages Definitions *
* *
******************************************************************************/
#define ELEC_SUCCESS 0
#define ELEC_E_ERROR (-1)

#define ELEC_E_OUTOFMEMORY (-10)




/******************************************************************************
* *
* Error Codes & Messages Wrapper APIs *
* *
******************************************************************************/







#endif /* INCLUDED_LIBELECERRNO_H */
--------------------------------------------------------------------------------
/**
* libElecPole.h - Pole Interface APIs implemented in Pole.h/Pole.c
*/
#ifndef INCLUDED_LIBELECPOLE_H
#define INCLUDED_LIBELECPOLE_H

#include "libElec.h"

/******************************************************************************
* *
* Pole Interface APIs *
* *
******************************************************************************/
extern LIBELECAPI void LibElec_CreatePole(IN ELEC_POLE *pole);

extern LIBELECAPI void LibElec_FreePole(IN ELEC_POLE pole);






#endif /* INCLUDED_LIBELECPOLE_H */
--------------------------------------------------------------------------------
/**
* libElecWire.h - Wire Interface APIs
* implemented in ElecWire.h/ElecWire.c
*/
#ifndef INCLUDED_LIBELECWIRE_H
#define INCLUDED_LIBELECWIRE_H

#include "libElec.h"

/******************************************************************************
* *
* Wire Interface APIs *
* *
******************************************************************************/
extern LIBELECAPI void LibElec_CreateWire(IN ELEC_WIRE *wire);

extern LIBELECAPI void LibElec_FreeWire(IN ELEC_WIRE wire);


#endif /* INCLUDED_LIBELECWIRE_H */
================================================================================
/**
* Elec.h
*/
#ifndef ELEC_H_INCLUDED
#define ELEC_H_INCLUDED

#include "libElec.h"

#include <stdlib.h> /* linux? */
#include <malloc.h>
#include <assert.h>


#endif
--------------------------------------------------------------------------------
/**
* ElecErrno.h
*/
#ifndef ELECERRNO_H_INCLUDED
#define ELECERRNO_H_INCLUDED

#include "libElecErrno.h"


#endif
--------------------------------------------------------------------------------
/**
* ElecPole.h
*/
#ifndef ELECPOLE_H_INCLUDED
#define ELECPOLE_H_INCLUDED

#include "libElecPole.h"

/******************************************************************************
* *
* Type Definition *
* *
******************************************************************************/
typedef struct _tElecPole
{
int a;
} tElecPole;


#endif
--------------------------------------------------------------------------------
/**
* ElecWire.h
*/
#ifndef ELECWIRE_H_INCLUDED
#define ELECWIRE_H_INCLUDED

/******************************************************************************
* *
* Type Definition *
* *
******************************************************************************/
typedef struct _tElecWire
{
int a;
} tElecWire;

#endif
================================================================================
/**
* Elec.c
*/
#include "Elec.h"
--------------------------------------------------------------------------------
/**
* ElecErrno.c
*/
#include "Elec.h"
#include "ElecErrno.h"
--------------------------------------------------------------------------------
/**
* ElecPole.c
*/
#include "Elec.h"
#include "ElecPole.h"

void LibElec_CreatePole(
IN ELEC_POLE *pole)
{
tElecPole *p = (tElecPole*) calloc(1, sizeof(tElecPole));
if (!p)
exit(ELEC_E_OUTOFMEMORY);

*pole = p;
}

void LibElec_FreePole(
IN ELEC_POLE pole)
{
assert(pole);
free(pole);
}
--------------------------------------------------------------------------------
/**
* ElecWire.c
*/
#include "Elec.h"
#include "ElecWire.h"

void LibElec_CreateWire(
IN ELEC_WIRE *wire)
{
tElecWire *p = (tElecWire*) calloc(1, sizeof(tElecWire));
if (!p)
exit(ELEC_E_OUTOFMEMORY);

*wire = p;
}

void LibElec_FreeWire(
IN ELEC_WIRE wire)
{
assert(wire);
free(wire);
}
--------------------------------------------------------------------------------

7)You can also use $gcc command to complile libElec.so on the linux paltform.





分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics