VS2008 生成静态链接库并使用

1.启动VS2008创建一个Win32控制台程序

   

2.选择静态库

3.创建两个文件lib.h和lib.cpp

//lib.h

#ifndef LIB_H 
#define LIB_H 
int add(int x,int y);
void print();
#endif

//lib.cpp

 1 #include"lib.h"
 2 #include "stdafx.h"
 3 #include<iostream>
 4 using namespace std;
 5 
 6 int add(int x,int y)
 7 {
 8     return x+y;
 9 }
10 
11 void print()
12 {
13     cout << "LCLCLC
";
14 }

4.点击“生成”中的”生成解决方案“

5.另创建一个工程Test1,将Test中的lib.h和Test.lib 放到Test1目录下

6.在Test1中创建一个cpp文件,代码如下

 1 #include "lib.h"//函数原型声明 
 2 #pragma comment(lib,"Test.lib") //将静态库导入
 3 
 4 #include<iostream>
 5 using namespace std;
 6 
 7 int main()
 8 {
 9     cout <<add(1,2)<<endl;
10     print();
11     return 0;
12 }

执行如下:

原文地址:https://www.cnblogs.com/LCCRNblog/p/3624871.html