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 Language Template Library C语言 模板库.zip
工程模版文件用于keil5,本实验为新建工程实验,仅供大家新建工程时参考。
并在该格式中创建一个文件,顶部带有这些注释,然后打开它在vim中创建的文件。 purpose of this file: Date: --------------------------------- template -c -c选项将使用vs代码而不是vim打开文件。 template
普通的stm32f103xxxx模板,有助于自己学习C语言,熟练操作调用库函数,锻炼自己的思维,对每年的高校和齐鲁机器人大赛都很有帮助,
MS VSUAL C++ 6 RUN-TIME LIBRARY REFERENCE 运行库手册.rar
NPM库创建React App模板 (CRA)模板以构建和发布具有rollup , eslint和stylelint配置的NPM库。 请参阅。用法npx create-react-app %PROJECT_NAME% --template npm-library 要么yarn create react-app %PROJECT_...
template-method-demo 模板方法示例
Angular-angular-template-for-threejs.zip,Angular Three.js示例(假人的三个模板)Angular Template for Three.js(ATFT),Angularjs于2016年发布,是Angularjs的重写版。它专注于良好的移动开发、模块化和改进的...
C++graph-template-library项目源码.zipC++graph-template-library项目源码.zipC++graph-template-library项目源码.zipC++graph-template-library项目源码.zip
您可以通过将多个组件添加到src目录并编辑src/index.js来创建一个导出多个组件的程序包,以将它们重新导出为命名导出。该模板包括: 苗条的组件使用svelte-testing-library + Jest进行测试故事书汇总,配置为生成....
Template-Toolkit-2.22.tar.gz Perl 的模板库
您可以使用此模板为应用程序创建自己的组件库。 构建Svelte组件库的基础。 原始工作是 使用 在GitHub上: Use this template右上角的Use this template按钮。 阅读更多: CLI: 用克隆它 npx degit patoi/...
2-85 2D悟空游戏模板-Kong Hero – Platformer Complete Unity Game Template 1.12-85 2D悟空游戏模板-Kong Hero – Platformer Complete Unity Game Template 1.12-85 2D悟空游戏模板-Kong Hero – Platformer ...
此存储库可以用作为kotlin/android库创建新github存储库的模板。
前端开源库-grunt-template-jasmine-requirejsgrunt template茉莉花Requirejs,grunt contrib茉莉花Requirejs模板
2015年黑帽大会上 James Kettle 讲解了《Server-Side Template Injection: RCE for the modern webapp》,从服务端模板注入的形成到检测,再到验证和利用都进行了详细的介绍,本资源为原文。
WIP创建React应用程序模板 该项目在 Github 全球发布: 这个包将全球发布到@designaroni/cra-template-react-from-scratch 手动安装 从github下载$ git clone git@github....
这是我的博客《前端常用插件之art-template模板引擎》文章的示例代码 具体使用可参考文章,或者网上搜索也可以
Windows Template Library - WTL Version 9.1 (build 5321 final) 2015-11-17。VC++下,COM开发用。