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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM