打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Visual?Studio下建立并隐式调用自己的动态链接库dll

Visual Studio下建立并隐式调用自己的动态链接库dll

(2011-06-05 08:53:03)
分类: VS
在工程或科研中,我们经常要使用自己编写的函数库。比较直接的方法是,我们可以在每个工程中把相应的头文件和源代码文件增添进去(Project-> Add ExistingItem),但这样比较麻烦。尤其当自己的函数库包含众多文件是,这个方法非常浪费时间。另一种方法是,我们可以把自己的函数库生成dll,使用的时候结合头文件来调用。这样省时省力。本文主要描述了后者的实现与使用过程。

   1). 首先创建自己的dll项目。打开VisualStudio,新建一个Win32 Console Application,项目名为ZWang_library。

   2). 在ApplicationSetting中,选择DLL和Empty Project。

   3).添加头文件ZWang_library.h,内容后附。

   4).添加源文件ZWang_library.cpp,内容后附。

   5). 在project ->ZWang_library properties -> Configuration Properties-> General -> ConfigurationType中选择Dynamic Library (.dll)

   6).编译ZWang_library,生成dll和lib

   7). 然后创建另一个Win32 ConsoleApplication项目,这个项目将调用生成的dll文件。这里项目名为ZWang_calldll

   8).ZWang_calldll.cpp内容见后附。

   9). 在project ->ZWang_calldll properties -> Configuration Properties-> C++ -> General ->Additional Include Directories中添加ZWang_library.h的路径。

   10). 在project-> ZWang_calldll properties ->Configuration Properties -> Linker ->General -> Additional LibraryDirectories中添加ZWang_library.lib的路径。


   11). 在project-> ZWang_calldll properties ->Configuration Properties -> Linker ->Input -> AdditionalDependencies中添加ZWang_library.lib。

   12).编译ZWang_calldll,将ZWang_library.dll放到包含ZWang_calldll.exe的文件夹中。运行程序。


代码1 ZWang_library.h
#ifndef _ZWANG
#define _ZWANG
#include <iostream>
#include <string>
namespace ZWANG
{
    usingnamespace std;
   __declspec(dllexport) void call_from_dll(const string&str = "Call the funtion from dll.");
}
#endif _ZWANG

代码2 ZWang_library.cpp
#include "ZWang_library.h"
namespace ZWANG
{
    voidcall_from_dll(const string &str)
    {
       cout<< str<< endl;
       cout<< "Success!"<< endl;
    }
}

代码3 ZWang_calldll.cpp
#include "stdafx.h"
#include "ZWang_library.h"
using namespace std;
using namespace ZWANG;
int _tmain(int argc, _TCHAR* argv[])
{
   call_from_dll("Hello world!");
   call_from_dll();
   cin.get();
    return0;
}
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Creating C# Class Library (DLL) Using Visual Studio .NET
Creating a Basic Web Part
SDL入门教程(一):2、Visual C++ 下的安装与设置
Microsoft Visual C Static and Dynamic Libraries
如何创建和使用dll及lib文件
R或Rstudio载入包时报错:……r Error:maximal number of DLLs reached...(R已达到了DLL数目的上限)问题的解决
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服