windows动态链接库dll生成和使用

一.生成动态链接库.

MyDll.h

#pragma once

#ifdef _MYDLL_EXPORT

#define DLL_API _declspec(dllexport)

#else

#define DLL_API _declspec(dllimport)

#endif


DLL_API int Add(int, int);

 

MyDll.cpp

#include "MyDll.h"

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

注意:工程设置里预处理器命令加上_MYDLL_EXPORT

编译生成.dll和.lib文件.

 

二.使用动态链接库.

test.cpp

#include "stdafx.h"
#include "MyDll.h"


int _tmain(int argc, _TCHAR* argv[])
{
    printf("10+5=%d\n", Add(10, 5));
    return 0;
}

工程设置 

C/C++ => 常规 => 附加包含目录:MyDll.h所在目录

链接器=> 常规 => 附加库目录:MyDll.lib所在目录

链接器=>输入=>附加依赖项:MyDll.lib

 

posted @ 2018-10-16 19:07  wssw  阅读(1575)  评论(0编辑  收藏  举报