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 ...
随机推荐
- socket上http协议应用(使用socket进行http通信的例子,准备好报头以后,简单read/write就可以了)
前几天看socket本有点晕, 好不容易弄明白了,才发现公司服务器用的是http的. 找了好久也没发现linux下直接用http的api, 不过今日偶然发现了使用socket进行http通信的例子, ...
- 正则表达式-Csharp
原文:正则表达式-Csharp 学习笔记:正则表达式 一. 正则表达式 正则表达式(Regex)是用来进行文本处理的技术,是语言无关的,在几乎所有语言中都有实现. 一个正则表达式就是由普通的字符及特殊 ...
- shell产生随机数
#!/bin/bash # 每次调用$RANDOM都会返回不同的随机整数. # 一般范围为: - (有符号的16-bit整数). MAXCOUNT= count= echo echo "$M ...
- 【Qt】一劳永逸解决UAC问题(修改mkspecs\win32-msvc2012\qmake.conf)
如果你的程序跑在一个开启了UAC保护的系统中,而你的程序又没有"盾牌"的话,程序总是会受到各种阻挠的,比如读写文件,写注册表等. 有了"盾牌"的话就不会出现一些 ...
- QT运行cmd指令(两种办法:QProcess.start然后waitForFinished,运行cmd /c命令)
QProcess p(); p.start("route");//写入要运行的指令即可 p.waitForStarted(); p.waitForFinished(); qDebu ...
- 注册表Demo
一.获取安装程序信息 #include <windows.h> #include <iostream> #include <string> #include < ...
- flask(四)
1.Flask-Session from flask import session,Flask from flask_session import Session #导入 from redis imp ...
- Mariadb的安装与使用
一.安装Mariadb 参考博客:https://www.cnblogs.com/pyyu/p/9467289.html 安装软件的三中方式 yum原码编译安装下载rpm安装 yum与原码编译安装安装 ...
- python实现常用查找算法
http://www.cnblogs.com/feixuelove1009/p/6148357.html
- Netty源码分析--Channel注册(中)(六)
接上一篇,我们继续看 不知道大家第一次看这段代码的时候有没有一脸懵逼,反正我是一脸懵,为什么这个if else 最终都是调用的register0方法,都是一样的. 其实这里就是为什么Netty是线程安 ...