VC win32 static library静态链接库简单示例
中午在宿舍闲来没事,看到网上一篇帖子,关于静态链接库的英文示例。它在.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静态链接库简单示例的更多相关文章
- vc下的静态链接库与动态链接库(一)
一.静态库与动态库的区别 目前以lib后缀的库有两种,一种为静态链接库(Static Libary,以下简称“静态库”),另一种为动态连接库(DLL,以下简称“动态库”)的导入库(Import Lib ...
- VC++:创建,调用Win32静态链接库
概述 DLL(Dynamic Linkable Library)动态链接库,Dll可以看作一种仓库,仓库中包含了可以直接使用的变量,函数或类. 仓库的发展史经历了"无库" ---& ...
- C运行时库(C Run-time Library)详解(提供的另一个最重要的功能是为应用程序添加启动函数。Visual C++对控制台程序默认使用单线程的静态链接库,而MFC中的CFile类已暗藏了多线程)
一.什么是C运行时库 1)C运行时库就是 C run-time library,是 C 而非 C++ 语言世界的概念:取这个名字就是因为你的 C 程序运行时需要这些库中的函数. 2)C 语言是所谓的“ ...
- VC++中的C运行时库浅析(控制台程序默认使用单线程的静态链接库,而MFC中的CFile类已暗藏了多线程)
1.概论 运行时库是程序在运行时所需要的库文件,通常运行时库是以LIB或DLL形式提供的.C运行时库诞生于20世纪70年代,当时的程序世界还很单纯,应用程序都是单线程的,多任务或多线程机制在此时还属于 ...
- 介绍静态链接库和动态链接库的差别,及在VC++6.0中的建立和使用
首先介绍一下链接库:链接库分为动态链接库和静态链接库两种 LIB是静态链接库,在程序编译连接的时候是静态链接,其相应的文件格式是.lib. 即当程序採用静态链接库的时候..lib文件里的函数被链接到终 ...
- 目前以lib后缀的库有两种,一种为静态链接库(Static Libary,以下简称“静态库”),另一种为动态连接库(DLL,以下简称“动态库”)的导入库(Import Libary,以下简称“导入库”)。静态库是一个或者多个obj文件的打包
前以lib后缀的库有两种,一种为静态链接库(Static Libary,以下简称“静态库”),另一种为动态连接库(DLL,以下简称“动态库”)的导入库(Import Libary,以下简称“导入库”) ...
- VC++ DLL 2 静态链接库
这一篇以VS2013为例子介绍怎样编写一个静态链接库和调用. 1.打开VS2013,新建Visual C++ 的win32项目: 新建后工程分支如下: 添加头文件和源文件: 编写头文件和源文件内容: ...
- 动态链接库dll,导入库lib,静态链接库lib
目前以lib后缀的库有两种,一种为静态链接库(Static Libary,以下简称“静态库”),另一种为动态连接库(DLL,以下简称“动态库”)的导入库(Import Libary,以下简称“导入库” ...
- c语言静态链接库
1 获得lib文件 vc++ 6.0中 新建 Win32 Static Library项目,命名为libTest 新建lib.h文件,代码如下 #ifndef LIB_H #define LIB_H ...
随机推荐
- Windows软件在Linux上的等价/替代/模仿软件列表 (抄一个)
Last update: 16.07.2003, 31.01.2005, 27.05.2005 您可在以下网站发现本列表最新版:http://www.linuxrsp.ru/win-lin-soft/ ...
- Win8Metro(C#)数字图像处理--2.22二值图像膨胀
原文:Win8Metro(C#)数字图像处理--2.22二值图像膨胀 [函数名称] 二值图像膨胀函数DilationProcess(WriteableBitmap src) [算法说明] 膨胀 ...
- Dynamic proxy (good-原创)
import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflec ...
- 在Visual Studio2015中使用单元测试
所谓的单元测试(Unit Test),就是对软件的一些模块进行测试以检查其正确性和可靠性,这些模块可以是一个类或者是一个方法等.在Visual studio中,这十分容易实现. 打开Visual st ...
- 在Window和Linux下使用Zthread库(跨平台高级面向对象的线性和sycnchronization 库)
ZThread库是一个开源的跨平台高级面向对象的线性和sycnchronization 库,以运行POSIX 和Win32 系统中的C++程序. ZThread库的主页:http://zthread. ...
- 腾讯云直播录制遇到的bug
1.录制方式应用: 初始化方法 [[TXUGCRecordshareInstance] startCameraCustom:param preview:_showPlayerView]; ID ...
- spring+rabbitmq+stomp搭建websocket消息推送(非spring boot方式)
前言: 两年前做过spring+activemq+stomp的ws推送,那个做起来很简单,但现在公司用的mq中间件是rabbitmq,因此需要通过rabbitmq去做ws通信.仔细搜了搜百度/谷歌,网 ...
- web的seo
摘要:搜索引擎优化是一种具有很高技术性的活动,也是一种营销功能,必须将它作为营销活动处理.SEO从业者必须理解公司的服务.产品.总体业务战略.竞争形势.品牌.未来网站开发目标和相关的业务构成. SEO ...
- ORACLE(系统表student) 基本与深入学习
(一).首先我们先创建student表(系统有的可以跳过往下看)没有直接复制运行即可. create table student(sno varchar2(3) not null, --学号sname ...
- Python的魔术方法详解
构造和初始化 __init__我们很熟悉了,它在对象初始化的时候调用,我们一般将它理解为"构造函数". 实际上, 当我们调用x = SomeClass()的时候调用,__init_ ...