A template for C-Language Library Creation-a template for creating a C Language Runtime Library

Source: Internet
Author: User

A template for C-Language Library Creation

A template for creating a C Language Runtime Library

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 encoded ded, which has
Encoded Ded 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 recommend strate here some key steps using Visual C ++ to create
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
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
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)"
"$ (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 shoshould 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
* Merchantability or fitness for a special purpose. See
* GNU lesser General Public License for more details.

* You shoshould have written ed 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;
} Telecpole;

# Endif
--------------------------------------------------------------------------------
/**
* Elecwire. h
*/
# Ifndef elecwire_h_included
# Define elecwire_h_included

/*************************************** ***************************************
**
* Type definition *
**
**************************************** **************************************/
Typedef struct _ telecwire
{
Int;
} 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.


 

Related Article

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.