中午在宿舍闲来没事,看到网上一篇帖子,关于静态链接库的英文示例。它在.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. 基于Go语言快速构建RESTful API服务

    In this post, we will not only cover how to use Go to create a RESTful JSON API, but we will also ta ...

  2. Win10商店芒果TV UWP版更新,新增后台视频下载

    湖南卫视旗下唯一官方视频平台<芒果TV>近日向Win10商店提交了芒果TV UWP V3.0.0版,这次为广大Win10平台用户带来了期待已久的重大功能和更新,可谓是良心厂商,值得鼓励和支 ...

  3. CREATE CSS3是一款在线集成众多CSS3功能的生成器,可以在线生成常用的CSS3效果

    CREATE CSS3是一款在线集成众多CSS3功能的生成器,可以在线生成常用的CSS3效果 CREATE CSS3 彩蛋爆料直击现场 CREATE CSS3是一款在线集成众多CSS3功能的生成器,可 ...

  4. QList使用下标[index]才可以获得可修改的item的引用(估计QStringList也是如此)

    QList算是最常用的集合了,今儿偶然间需要修改QList中的值,结果郁闷了.QList中提供了replace函数来替换item,但不是修改.而at().value()操作均返回的是const的ite ...

  5. 从此Redis是路人

    从此Redis是路人 序言:Redis(Remote DIctionary Server)作为一个开源/C实现/高性能/基于内存的key-value存储系统,相信做Java的小伙伴都不会陌生.Redi ...

  6. Liferay6.1 配置友好的URL映射

    说明:以下内容和官方文档相差不大,如果您英文较好,建议直接去读官方文档,地址是:https://dev.liferay.com/develop/tutorials/-/knowledge_base/6 ...

  7. 深入解析Hyperledger Fabric启动的全过程

    在这篇文章中,使用fabric-samples/first-network中的文件进行fabric网络(solo类型的网络)启动全过程的解析.如有错误欢迎批评指正. 至于Fabric网络的搭建这里不再 ...

  8. 基于Common.Logging + Log4Net实现的日志管理

    前言 Common.Logging 是Commons-Logging(apache最早提供的日志门面接口,提供了简单的日志实现以及日志解耦功能) 项目的.net版本.其目的是为 "所有的.n ...

  9. eclipse 工具在工作中实用方法

    不断更新记录工作中用到的实用技巧 1.快捷方式管理多个工作空间 参数: -showlocation 设置eclipse顶部显示工作空间位置 -data 文件位置 设置打开的工作空间位置 创建eclip ...

  10. c# 自己实现可迭代的容器

    在c#中我们经常使用到foreach语句来遍历容器,如数组,List,为什么使用foreach语句能够遍历一个这些容器呢,首先的一个前提是这些容器都实现了IEnumerable接口,通过IEnumer ...