vs2010下编译DLL库和使用

来源:互联网 发布:dice-51单片机开发系统 编辑:程序博客网 时间:2024/06/12 21:21

一、创建DLL 文件

1 vs2010下选择win32应用程序,创建DLL 工程

2 创建头文件testdll.h

#ifndef TestDll_H_#define TestDll_H_#ifdef MYLIBDLL#define MYLIBDLL extern "C" _declspec(dllimport) #else#define MYLIBDLL extern "C" _declspec(dllexport) #endifMYLIBDLL int Add(int plus1, int plus2);//You can also write like this://extern "C" {//_declspec(dllexport) int Add(int plus1, int plus2);//};#endif

3 创建源文件

#include "stdafx.h"#include "testdll.h"#include <iostream>using namespace std;int Add(int plus1, int plus2){int add_result = plus1 + plus2;return add_result;}
4 创建模块文件

LIBRARY "MyDLL"EXPORTSAdd @1
5 选择release版本进行编译

二、调用DLL文件中的函数

#include "stdafx.h"#include <windows.h>#include <stdio.h>#pragma comment (lib , "MyDll.lib" )extern "C"_declspec (dllimport) int Add(int plus1, int plus2);int _tmain(int argc, _TCHAR* argv[]){printf("%d\n",Add(6,4));return 0;}





0 0