中午在宿舍闲来没事,看到网上一篇帖子,关于静态链接库的英文示例。它在.Net上开发,我将其移到VC上开发,因此对其代码做了相应修改。帖子内容如下:(代码我已修改)。原帖见:http://msdn.microsoft.com/en-us/library/ms235627

     

The next type of library we will create is a static library (LIB). Using static libraries is a great way to reuse code. Rather than re-implementing the same routines in every program that you create, you write them one time and reference them from applications
that need the functionality.

This walkthrough covers the following:

Creating a new static library project.

Adding a class to the static library.

Creating an application that references the static library.

Using the functionality from the static library in the console application.

Running the application.

Prerequisites

This topic assumes that you understand the fundamentals of the C++ language. If you are just getting started learning C++, we recommend the "C++ Beginner's Guide," written by Herb Schildt, available online at http://go.microsoft.com/fwlink/?LinkId=115303.

To create a new static library project

From the File menu, select New and then Project….

On the Project types pane, under Visual C++, select Win32.

On the Templates pane, select Win32 Console Application.

Choose a name for the project, such as MathFuncsLib, and enter it in the Name field. Choose a name for the solution, such as StaticLibrary, and enter it in the Solution Name field.

Press OK to start the Win32 application wizard. On the Overview page of the Win32 Application Wizard dialog box, press Next.

On the Application Settings page of the Win32 Application Wizard, under Application type, select Static library.

On the Application Settings page of the Win32 Application Wizard, under Additional options, clear the Precompiled header check box.

Press Finish to create the project.

To add a class to the static library

To create a header file for a new class, from the Project menu, select Add New Item…. The Add New Item dialog box will be displayed. From the Categories pane, under Visual C++, select Code. From the Templates pane, select Header File (.h). Choose a name for
the header file, such as MathFuncsLib.h, and press Add. A blank file will be displayed.

Add a simple class named MyMathFuncs to do common mathematical operations, such as addition, subtraction, multiplication, and division. The code should resemble the following:

class MyMathFuncs{

public:

    // Returns a + b

    static double Add(double a, double b);

    // Returns a - b

    static double Subtract(double a, double b);

    // Returns a * b

    static double Multiply(double a, double b);

    // Returns a / b

    // Throws DivideByZeroException if b is 0

    static double Divide(double a, double b);

};

To create a source file for a new class, from the Project menu, select Add New Item…. The Add New Item dialog box will be displayed. From the Categories pane, under Visual C++, select Code. From the Templates pane, select C++ File (.cpp). Choose a name for
the source file, such as MathFuncsLib.cpp, and press Add. A blank file will be displayed.

Implement the functionality for MyMathFuncs in the source file. The code should resemble the following:

// MathFuncsLib.cpp

// compile with: /c /EHsc

// post-build command: lib MathFuncsLib.obj





#include "MathFuncsLib.h"

double MyMathFuncs::Add(double a, double b)

{

    return a + b;

}





double MyMathFuncs::Subtract(double a, double b)

{

    return a - b;

}





double MyMathFuncs::Multiply(double a, double b)

{

    return a * b;

}





double MyMathFuncs::Divide(double a, double b)

{

    return a / b;

}

To build the project into a static library, from the Project menu, select MathFuncsLibProperties…. On the left pane, under Configuration Properties, select General. On the right pane, change the Configuration Type to Static Library (.lib). Press OK to save
the changes.

NoteNote

When you build from the command line, you must build the program in two steps. First, compile the code by using Cl.exe with the /c compiler option (cl /c /EHsc MathFuncsLib.cpp). This will create an object file that is named MathFuncsLib.obj. For more information,
see /c (Compile Without Linking). Second, link the code by using the Library Manager Lib.exe (lib MathFuncsLib.obj). This will create the static library MathFuncsLib.lib. For more information about the Library Manager, see LIB Reference.

Compile the static library by selecting Build Solution from the Build menu. This creates a static library that can be used by other programs.

To create an application that references the static library

To create an application that will reference and use the static library that was just created, from the File menu, select New and then Project….

On the Project types pane, under Visual C++, select Win32.

On the Templates pane, select Win32 Console Application.

Choose a name for the project, such as MyExecRefsLib, and type it in the Name field. Next to Solution, select Add to Solution from the drop down list. This will add the new project to the same solution as the static library.

Press OK to start the Win32 Application Wizard. On the Overview page of the Win32 Application Wizard dialog box, press Next.

on the Application Settings page of the Win32 Application Wizard, under Application type, select Console application.

On the Application Settings page of the Win32 Application Wizard, under Additional options, clear Precompiled header.

Press Finish to create the project.

To use the functionality from the static library in the console application

After you create a new console application, the wizard creates an empty program for you. The name for the source file will be the same as the name that you chose for the project earlier. In this example, it is named MyExecRefsLib.cpp.

To use the math routines that you created in the static library, you must reference it. To do this, select References… from the Project menu. From the Property Pages dialog box, expand the Common Properties node and select References. Then select the Add New
Reference… button. For more information about the References… dialog box, see Framework and References, Common Properties, <Projectname> Property Pages Dialog Box.

The Add Reference dialog box is displayed. This dialog box lists all the libraries that you can reference. The Project tab lists all the projects in the current solution and any libraries they contain. On the Projects tab, select MathFuncsLib. Then select OK.

To reference the header files of the static library, you must modify the include directories path. To do this, in the Property Pages dialog box, expand the Configuration Properties node, expand the C/C++ node, and then select General. Next to Additional Include
Directories, type the path of the location of the MathFuncsLib.h header file.

You can now use the MyMathFuncs class in this application. Replace the contents of MyExecRefsLib.cpp with the following code:

// MyExecRefsLib.cpp

// compile with: /EHsc /link MathFuncsLib.lib

#include <stdio.h>

#include <iostream>

#include "..\MathFuncsLib.h"

#pragma comment( lib, "..\\debug\\MathFuncsLib.lib" )//指定与静态库一起连接

using namespace std;





int main()

{

    double a = 7.4;

    int b = 99;





    cout << "a + b = " <<MyMathFuncs::Add(a, b) << endl;

    cout << "a - b = " <<MyMathFuncs::Subtract(a, b) << endl;

    cout << "a * b = " <<MyMathFuncs::Multiply(a, b) << endl;

    cout << "a / b = " <<MyMathFuncs::Divide(a, b) << endl;





    return 0;

}

Build the executable by selecting Build Solution from the Build menu.

To run the application

Make sure MyExecRefsLib is selected as the default project. In the Solution Explorer, select MyExecRefsLib, and then select Set As StartUp Project from the Project menu.

To run the project, select Start Without Debugging from the Debug menu. The output should resemble this

VC win32 static library静态链接库简单示例的更多相关文章

  1. vc下的静态链接库与动态链接库(一)

    一.静态库与动态库的区别 目前以lib后缀的库有两种,一种为静态链接库(Static Libary,以下简称“静态库”),另一种为动态连接库(DLL,以下简称“动态库”)的导入库(Import Lib ...

  2. VC++:创建,调用Win32静态链接库

    概述 DLL(Dynamic Linkable Library)动态链接库,Dll可以看作一种仓库,仓库中包含了可以直接使用的变量,函数或类. 仓库的发展史经历了"无库" ---& ...

  3. C运行时库(C Run-time Library)详解(提供的另一个最重要的功能是为应用程序添加启动函数。Visual C++对控制台程序默认使用单线程的静态链接库,而MFC中的CFile类已暗藏了多线程)

    一.什么是C运行时库 1)C运行时库就是 C run-time library,是 C 而非 C++ 语言世界的概念:取这个名字就是因为你的 C 程序运行时需要这些库中的函数. 2)C 语言是所谓的“ ...

  4. VC++中的C运行时库浅析(控制台程序默认使用单线程的静态链接库,而MFC中的CFile类已暗藏了多线程)

    1.概论 运行时库是程序在运行时所需要的库文件,通常运行时库是以LIB或DLL形式提供的.C运行时库诞生于20世纪70年代,当时的程序世界还很单纯,应用程序都是单线程的,多任务或多线程机制在此时还属于 ...

  5. 介绍静态链接库和动态链接库的差别,及在VC++6.0中的建立和使用

    首先介绍一下链接库:链接库分为动态链接库和静态链接库两种 LIB是静态链接库,在程序编译连接的时候是静态链接,其相应的文件格式是.lib. 即当程序採用静态链接库的时候..lib文件里的函数被链接到终 ...

  6. 目前以lib后缀的库有两种,一种为静态链接库(Static Libary,以下简称“静态库”),另一种为动态连接库(DLL,以下简称“动态库”)的导入库(Import Libary,以下简称“导入库”)。静态库是一个或者多个obj文件的打包

    前以lib后缀的库有两种,一种为静态链接库(Static Libary,以下简称“静态库”),另一种为动态连接库(DLL,以下简称“动态库”)的导入库(Import Libary,以下简称“导入库”) ...

  7. VC++ DLL 2 静态链接库

    这一篇以VS2013为例子介绍怎样编写一个静态链接库和调用. 1.打开VS2013,新建Visual C++ 的win32项目: 新建后工程分支如下: 添加头文件和源文件: 编写头文件和源文件内容: ...

  8. 动态链接库dll,导入库lib,静态链接库lib

    目前以lib后缀的库有两种,一种为静态链接库(Static Libary,以下简称“静态库”),另一种为动态连接库(DLL,以下简称“动态库”)的导入库(Import Libary,以下简称“导入库” ...

  9. c语言静态链接库

    1 获得lib文件 vc++ 6.0中 新建 Win32 Static Library项目,命名为libTest 新建lib.h文件,代码如下 #ifndef LIB_H #define LIB_H ...

随机推荐

  1. .NET VS 自定义新建代码文件模板

    参考:http://www.cnblogs.com/fightingtong/p/3765914.html 在VS中新建文件时,可使用模板在文件中生成指定内容.只需要把IDE安装目录下的模板进行修改保 ...

  2. mingw 构建 mysql-connector-c-6.1.9记录(26种不同的编译错误,甚至做了一个windows系统返回错误码与System V错误码的一个对照表)

    http://www.cnblogs.com/oloroso/p/6867162.html

  3. 深度学习概述教程--Deep Learning Overview

          引言         深度学习,即Deep Learning,是一种学习算法(Learning algorithm),亦是人工智能领域的一个重要分支.从快速发展到实际应用,短短几年时间里, ...

  4. Python写的嗅探器——Pyside,Scapy

    使用Python的Pyside和Scapy写的嗅探器原型,拥有基本框架,但是功能并不十分完善,供参考. import sys import time import binascii from PySi ...

  5. PHP XDebug Sublime Text 单步调试

    前置环境:已经安装好LNMP 1. 安装xdebug 可以通过pear包管理来安装 sudo apt-get install php-pear sudo pecl install xdebug 这里我 ...

  6. 中国目前的房地产总市值占GDP的比例为411%,远远高于世界平均水平的260%

    到2015年统计,一线城市空置率22%,二线城市24%.中央开始喊要供应侧改革了. 中国目前的房地产总市值占GDP的比例为411%,远远高于世界平均水平的260%.无疑已经蕴含着比较明显的泡沫. 那么 ...

  7. QML Settings 小的示例

    QML 中使用 Settings 可以保存一些简单的信息,例如用户名,密码,窗口位置,大小等,没有Sqlite那么麻烦,简单易用哦~~~(环境:Qt5.8  for android ,Windows ...

  8. 比较DirectX和OpenGL的区别(比较详细)

    OpenGL是个专业的3D程序接口,是一个功能强大,调用方便的底层3D图形库.OpenGL的前身是SGI公司为其图形工作站开发的IRIS GL.IRIS GL是一个工业标准的3D图形软件接口,功能虽然 ...

  9. delphi的Socket(有两种分别继承TObject和TComponent的方式)

    在Delphi中,对于Windows中的Socket进行了有效的封装.在Delphi中,按其继承关系,可以分层两类:一.TComponent--TAbstractSocket--TCustomSock ...

  10. 文件夹管理工具(MVC+zTree+layer)

    文件夹管理工具(MVC+zTree+layer)(附源码)   写在前 之前写了一篇关于 文件夹与文件的操作的文章  操作文件方法简单总结(File,Directory,StreamReader,St ...