c++动态库(一)静态库

静态库lib(vs2005)

 

一,创建一个win32的lib项目,如下图:

 

 

 

二、添加两个文件libtest.h,libtest.cpp,如下:

//libtest.h
#ifndef LIB_H
#define LIB_H
extern "C" int add(int a,int b);
#endif

 

//libtest.cpp
#include "libtest.h"

int add(int a,int b)
{
 return a + b;
}

 

三、创建一个调用主程序

在创建库工程中创建一个调用程序控制台调用程序

1、第一种调用方法

// libcall.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include "libtest.h"
#include <iostream>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
 cout<<"1+2="<<add(1,2)<<endl;
 return 0;
}

这种调用方式需对主程序进行配置,如下图:

1、添加include路径

2、添加附加库目录

3、添加被调用静态库dlltest.lib

 

2、第二种调用方法,直接把链接相关部分用代码指定。

// libcall1.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include "..//dlltest//libtest.h"
#include <iostream>
#pragma comment(lib,"..//debug//dlltest.lib")
using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
 cout<<"1+2="<<add(1,2)<<endl;
 return 0;
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值