๋ฐ˜์‘ํ˜•

์ง€๋‚œ ํฌ์ŠคํŒ…์—์„œ ์™ธ๋ถ€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋ฐ ๋™์ /์ •์  ๋งํ‚น์— ๋Œ€ํ•œ ์ด๋ก ์ ์ธ ์†Œ๊ฐœ๋ฅผ ํ–ˆ์—ˆ์Šต๋‹ˆ๋‹ค.

[C++ DLL] ์™ธ๋ถ€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์— ๋Œ€ํ•œ ์ดํ•ด

 

[C++ DLL] ์™ธ๋ถ€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์— ๋Œ€ํ•œ ์ดํ•ด

์™ธ๋ถ€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ž€? ์ฝ”๋”ฉ์„ ํ•˜๋‹ค ๋ณด๋ฉด ์™ธ๋ถ€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์‚ฌ์šฉ์€ ๊ฑฐ์˜ ํ•„์ˆ˜์ž…๋‹ˆ๋‹ค. ํ•˜๋‚˜๋ถ€ํ„ฐ ์—ด๊นŒ์ง€ ์ผ์ผ์ด ๋งŒ๋“ค์–ด์„œ ํ”„๋กœ๊ทธ๋žจ์„ ์ œ์ž‘ํ•˜๊ธฐ๋ž€ ์‰ฝ์ง€๊ฐ€ ์•Š๊ธฐ ๋•Œ๋ฌธ์ด์ฃ . ์˜ˆ๋ฅผ ๋“ค์–ด, ํ”„๋กœ๊ทธ๋žจ์˜ ๋™์ž‘

luckygg.tistory.com

์ด๋ฒˆ ํฌ์ŠคํŒ…์—์„œ๋Š” Visual Studio C++ DLL ํ”„๋กœ์ ํŠธ๋ฅผ ํ†ตํ•ด dll์„ ์–ด๋–ป๊ฒŒ ์ƒ์„ฑํ•˜๊ณ  ๋ถˆ๋Ÿฌ์˜ค๋Š”์ง€ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋ณธ๋ฌธ์˜ ์˜ˆ์ œ์—์„œ๋Š” ์‚ฌ์น™ ์—ฐ์‚ฐ ํ•จ์ˆ˜๋ฅผ dll์— ๊ตฌํ˜„ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

C++ DLL ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ

Visual Studio ์‹คํ–‰ ํ›„ Visual C++ > DLL(๋™์  ์—ฐ๊ฒฐ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ) ํ”„๋กœ์ ํŠธ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

DLL ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ

ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ ํ›„ MyMath.h ๋ฐ MyMath.cpp ํŒŒ์ผ์„ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.

MyMath.h ์ถ”๊ฐ€
MyMath.cpp ์ถ”๊ฐ€

๊ทธ๋ฆฌ๊ณ  MyMath.h์— ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์„ ์–ธํ•ฉ๋‹ˆ๋‹ค.

#pragma once

#ifdef CREATEDLL_EXPORTS
#define MYMATH_DECLSPEC __declspec(dllexport)
#else
#define MYMATH_DECLSPEC __declspec(dllimport)
#endif

extern "C" MYMATH_DECLSPEC double Sum(double a, double b);
extern "C" MYMATH_DECLSPEC double Sub(double a, double b);
extern "C" MYMATH_DECLSPEC double Mul(double a, double b);
extern "C" MYMATH_DECLSPEC double Div(double a, double b);

MyMath.cpp์—๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค. ์•„์ฃผ ๊ฐ„๋‹จํ•˜์ฃ ?

#include "MyMath.h"

double Sum(double a, double b) {
	return a + b;
}

double Sub(double a, double b) {
	return a - b;
}

double Mul(double a, double b) {
	return a * b;
}

double Div(double a, double b) {
	return a / b;
}

์—ฌ๊ธฐ์„œ ์„ธ ๊ฐ€์ง€ ์„ค๋ช…๋“œ๋ฆด ๋‚ด์šฉ์ด ์žˆ์Šต๋‹ˆ๋‹ค.

์ „์ฒ˜๋ฆฌ๊ธฐ CREATEDLL_EXPORTS?

#ifdef CREATEDLL_EXPORTS๋Š” CREATEDLL_EXPORTS๋ผ๋Š” ์ „์ฒ˜๋ฆฌ๊ธฐ๊ฐ€ ์ •์˜๋˜์–ด์žˆ๋Š”์ง€ ํ™•์ธํ•˜๋Š” ์กฐ๊ฑด๋ฌธ์ž…๋‹ˆ๋‹ค. ๋งŒ์•ฝ ์ •์˜๋˜์–ด ์žˆ๋‹ค๋ฉด, MYMATH_DECLSPEC์€ __declspec(dllexport)๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์ •์˜๋˜์–ด ์žˆ์ง€ ์•Š๋‹ค๋ฉด, MYMATH_DECLSPEC์€ __declspec(dllimport)๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.

์ „์ฒ˜๋ฆฌ๊ธฐ๊ฐ€ ์ •์˜๋˜์–ด ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ํ”„๋กœ์ ํŠธ ์„ค์ • > C/C++ > ์ „์ฒ˜๋ฆฌ๊ธฐ > ์ „์ฒ˜๋ฆฌ๊ธฐ ์ •์˜์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์—์„œ ์‚ฌ์šฉ์ž๊ฐ€ ์ž„์˜๋กœ CREATEDLL_EXPORTS ์ด๋ฆ„์„ ๋ณ€๊ฒฝํ•˜๊ณ , ์ „์ฒ˜๋ฆฌ๊ธฐ ์กฐ๊ฑด๋ฌธ์˜ ์ „์ฒ˜๋ฆฌ๊ธฐ๋ฅผ ๋ณ€๊ฒฝํ•ด๋„ ์ƒ๊ด€์—†์Šต๋‹ˆ๋‹ค.

์ „์ฒ˜๋ฆฌ๊ธฐ ์ •์˜ ํ™”๋ฉด

์™œ ์ด๋ ‡๊ฒŒ ๊ตฌ๋ถ„ ์ง€์—ˆ๋ƒ๋ฉด, ๋™์ผํ•œ *.h ํŒŒ์ผ์„ dll ์ƒ์„ฑ ํ”„๋กœ์ ํŠธ์™€ dll ํ˜ธ์ถœ ํ”„๋กœ์ ํŠธ์—์„œ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ์ž…๋‹ˆ๋‹ค. ๋งŒ์•ฝ, dll ์ƒ์„ฑ ํ”„๋กœ์ ํŠธ ์ „์šฉ *.h ํŒŒ์ผ๊ณผ dll ํ˜ธ์ถœ ํ”„๋กœ์ ํŠธ ์ „์šฉ *.h ํŒŒ์ผ์„ ๋”ฐ๋กœ ๊ด€๋ฆฌํ•œ๋‹ค๋ฉด ๊ท€์ฐฎ์•„์ง€๊ฒ ์ฃ ? ๊ทธ๋ž˜์„œ ์ด๋ ‡๊ฒŒ ์ „์ฒ˜๋ฆฌ๊ธฐ ์ •์˜๋ฅผ ์ถ”๊ฐ€ํ•˜์—ฌ ํ•˜๋‚˜์˜ ํŒŒ์ผ๋กœ ๊ด€๋ฆฌ๊ฐ€ ๊ฐ€๋Šฅํ•ด์ง‘๋‹ˆ๋‹ค.

__declspec(dllexport)์™€ __declspec(dllimport) ํ‚ค์›Œ๋“œ?

๊ทธ๋Ÿผ __declspec(dllexport)์™€ __declspec(dllimport)๋Š” ๋ฌด์—‡์ผ๊นŒ์š”?

__declspec(dllexport) ํ‚ค์›Œ๋“œ๊ฐ€ ๋ถ™์€ ํ•จ์ˆ˜๋Š” DLL ์™ธ๋ถ€๋กœ ๋…ธ์ถœ๋ฉ๋‹ˆ๋‹ค. ์ฆ‰, DLL์„ ์‚ฌ์šฉํ•˜์—ฌ __declspec(dllexport) ํ‚ค์›Œ๋“œ๊ฐ€ ๋ถ™์€ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. ์ด์™€ ๋ฐ˜๋Œ€๋กœ, ํ•ด๋‹น ํ‚ค์›Œ๋“œ๊ฐ€ ์—†๋Š” ํ•จ์ˆ˜๋Š” ์ •์˜๋˜์–ด ์žˆ์–ด๋„ DLL ์™ธ๋ถ€์—์„œ ํ˜ธ์ถœํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.

__declspec(dllimport) ํ‚ค์›Œ๋“œ๋Š” DLL์— ์ •์˜๋œ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ์ฆ‰, DLL์—์„œ __declspec(dllexport)๋กœ ์ •์˜๋œ ํ•จ์ˆ˜๋ฅผ __declspec(dllimport)๋กœ ํ˜ธ์ถœํ•œ๋‹ค๋Š” ๊ฒƒ์ด์ฃ .

extern "C" ํ‚ค์›Œ๋“œ?

extern "C" ํ‚ค์›Œ๋“œ๋ฅผ ์„ค๋ช…ํ•˜๊ธฐ์— ์•ž์„œ, ๋„ค์ž„ ๋งน๊ธ€๋ง(Name Mangling)์— ๋Œ€ํ•ด ์„ค๋ช…ํ•˜์ง€ ์•Š์„ ์ˆ˜ ์—†๋Š”๋ฐ์š”. ๋„ค์ž„ ๋งน๊ธ€๋ง์ด๋ž€, ์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ ํ•จ์ˆ˜์˜ ์ด๋ฆ„์„ ์ผ์ •ํ•œ ๊ทœ์น™์„ ๊ฐ€์ง€๊ณ  ๋ณ€ํ˜•ํ•˜๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. C++์—์„œ๋Š” C์™€๋Š” ๋‹ค๋ฅด๊ฒŒ ํ•จ์ˆ˜ ์˜ค๋ฒ„๋กœ๋”ฉ์„ ์ง€์›ํ•˜๋Š”๋ฐ์š”. ์ฆ‰, ๋™์ผํ•œ ํ•จ์ˆ˜์˜ ์ด๋ฆ„์ด๋ผ๋„ ๋งค๊ฐœ๋ณ€์ˆ˜์— ๋”ฐ๋ผ ๋ง์ปค๋Š” ๋‹ค๋ฅด๊ฒŒ ๊ตฌ๋ถ„ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๊ฐ™์€ ์ด๋ฆ„์˜ ํ•จ์ˆ˜๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐœ ์กด์žฌํ•  ๋•Œ ์–ด๋–ค ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ• ์ง€ ๊ตฌ๋ถ„ํ•ด์„œ ๋งํฌํ•˜๊ธฐ ์œ„ํ•ด ์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ ์ด๋ฆ„์„ ๋ณ€ํ˜•ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ๋งน๊ธ€๋ง์ด ์ ์šฉ๋ฉ๋‹ˆ๋‹ค.

๊ทธ๋Ÿฐ๋ฐ DLL์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ช…์‹œ์  ๋งํ‚น(Explicit Linking)์„ ํ•  ๊ฒฝ์šฐ ํ˜ธ์ถœํ•  ํ•จ์ˆ˜์˜ ์ด๋ฆ„์„ ์•Œ์•„์•ผ ํ•˜๋Š”๋ฐ์š”. ํ•จ์ˆ˜์˜ ์ด๋ฆ„์ด DLL๋กœ ์ƒ์„ฑ๋˜๋ฉด์„œ ๋งน๊ธ€๋ง ๋˜๋ฉด ํ˜ธ์ถœํ•˜๊ธฐ๊ฐ€ ์–ด๋ ต๊ฒ ์ฃ ? ๊ทธ๋ž˜์„œ extern "C" ํ‚ค์›Œ๋“œ๋ฅผ ๋ถ™์—ฌ ๋„ค์ž„ ๋งน๊ธ€๋ง์„ ์ˆ˜ํ–‰ํ•˜์ง€ ์•Š๊ฒ ๋‹ค๊ณ  ์ •์˜ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

DLL์„ ํƒ์ƒ‰ํ•  ์ˆ˜ ์žˆ๋Š” Dependency Walker ํ”„๋กœ๊ทธ๋žจ์„ ์‚ฌ์šฉํ•˜์—ฌ, extern "C" ํ‚ค์›Œ๋“œ ์œ ๋ฌด์— ๋”ฐ๋ฅธ ํ•จ์ˆ˜ ์ด๋ฆ„์„ ์ž ์‹œ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

extern "C" ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ DLL์„ ์ƒ์„ฑํ•˜๋ฉด, Sum, Sub, Mul, Div๋ผ๋Š” ํ•จ์ˆ˜ ์ด๋ฆ„์ด ๋ณด์ž…๋‹ˆ๋‹ค(์ดํ›„ ์˜ˆ์ œ์—์„œ ์ƒ์„ฑํ•  ํ•จ์ˆ˜ ์ด๋ฆ„์ž…๋‹ˆ๋‹ค). ๊ทธ๋ฆฌ๊ณ  ์•ž ์—ด์— C์–ธ์–ด๋ผ๊ณ  ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค.

extern "C" ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•œ DLL

extern "C" ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  DLL์„ ์ƒ์„ฑํ•˜๋ฉด, ํŠน์ˆ˜๋ฌธ์ž์™€ ๋Œ€๋ฌธ์ž ์•ŒํŒŒ๋ฒณ์ด ์„ž์ธ ํ•จ์ˆ˜ ์ด๋ฆ„์ด ๋ณด์ž…๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์•ž ์—ด์— C++ ์–ธ์–ด๋ผ๊ณ  ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค.

extern "C" ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์€ DLL

์ด์ฒ˜๋Ÿผ, extern "C" ํ‚ค์›Œ๋“œ๋ฅผ ํ†ตํ•ด ๋„ค์ž„ ๋งน๊ธ€๋ง์„ ์ˆ˜ํ–‰ํ•˜์ง€ ์•Š์„ ์ˆ˜ ์žˆ๊ณ , ๋ช…์‹œ์  ๋งํ‚น์œผ๋กœ ํŠน์ • ํ•จ์ˆ˜ ํ˜ธ์ถœ์ด ๊ฐ€๋Šฅํ•ด์ง‘๋‹ˆ๋‹ค.

๋นŒ๋“œํ•˜์—ฌ DLL ์ƒ์„ฑํ•˜๊ธฐ

์ด์ œ ํ”„๋กœ์ ํŠธ๋ฅผ ๋นŒ๋“œํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๋นŒ๋“œ ํ›„ *.dll ๋ฐ *.lib ํŒŒ์ผ์ด ์ƒ์„ฑ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ํ–ฅํ›„ ์ด ํŒŒ์ผ๊ณผ *.h ํŒŒ์ผ์„ ๊ฐ™์ด ๋ฐฐํฌํ•˜๋ฉด, ์‚ฌ์šฉ์ž๊ฐ€ ํ”„๋กœ์ ํŠธ์— dll์„ ๋ถˆ๋Ÿฌ์™€ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ƒ์„ฑ๋œ dll ๋ฐ lib ํŒŒ์ผ

๋ณธ๋ฌธ์˜ dll ์ƒ์„ฑ ํ”„๋กœ์ ํŠธ ์˜ˆ์ œ ์ฝ”๋“œ๋ฅผ ์ฒจ๋ถ€ํ–ˆ์Šต๋‹ˆ๋‹ค. ์ถ”ํ›„ ์ƒ์„ฑ๋œ dll์„ ์–ด๋–ป๊ฒŒ ํ”„๋กœ์ ํŠธ์— ๋ถˆ๋Ÿฌ์™€ ์‚ฌ์šฉํ•˜๋Š”์ง€ ์ด์–ด์„œ ์†Œ๊ฐœํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

ExamDLL.zip
0.01MB

 

๋ฐ˜์‘ํ˜•