A Template for C-Language Library Creation - 一个创建C语言运行库的模板
2010-10-09 11:13
417 查看
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.
一个创建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.
相关文章推荐
- A Template for C-Language Library Creation - 一个创建C语言运行库的模板
- C语言测试一个函数运行时间的模板
- 一个链表创建、反转、打印的C语言代码
- OpenCms JSP 模板开发——创建一个“完全的”JSP模板
- 创建并控制一个asp.net程序!(for delphi.net )
- 陆续放给大家一些拿钱买的收费的模板,今天先给一个在某销售网站上排行第一的管理端模板 Ace Responsive Admin Template
- 多线程之二:创建一个多线程管理器(Creating a manager for Multiple Threads)
- c语言:创建一个数组,分别实现函数初始化数组、 清空数组、数组元素的逆置
- IronPython for ASP.NET:使用IronPython创建一个简单的Web页面
- 最全Pycharm教程(9)——创建并运行一个基本的Python测试程序 --待整理
- C语言创建一个文件,写入数据并换行,再依次读取每行的数据,将读取的行数据以一定的格式命名为另一文件的文件名
- 64位Eclipse运行提示“Failed to load the JNI shared library”的一个解决方案
- 如何使用th:each属性迭代模板-原标题:How To Use th:each For Iteration In Thymeleaf Template?
- Python web入门:Django学习与实践二(简单页面实现和创建一个模板)
- for 创建一个方法:键盘录入一个数 ,求它的 阶乘 及 阶乘的和
- c语言:创建一个数组,分别实现函数初始化数组、 清空数组、数组元素的逆置
- Cocos2d-x 2.2 - VS 2010 - Win7 环境配置 - 配置项目模板,保证创建即可无错运行
- 1.3、Android Studio创建一个Android Library
- 在创建一个MVC控制器,显示运行所选代码生成器时出错(带读写,使用EF)
- OpenCms JSP 模板开发——创建一个含有多个可编辑元素的JSP模板