๋ฐ˜์‘ํ˜•

์ง€๋‚œ ํฌ์ŠคํŒ…์—์„œ C++ DLL ์ƒ์„ฑ ๋ฐฉ๋ฒ•์„ ์†Œ๊ฐœํ–ˆ์—ˆ์Šต๋‹ˆ๋‹ค.

[C++ DLL] Visual Studio C++ DLL ์ƒ์„ฑํ•˜๊ธฐ (์˜ˆ์ œ ํฌํ•จ)

์ด๋ฒˆ ํฌ์ŠคํŒ…์—์„œ๋Š” ์ƒ์„ฑ๋œ DLL์„ ์•”์‹œ์  ๋งํฌํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์†Œ๊ฐœํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

์•”์‹œ์  ๋งํ‚น(Implicit Linking)

์•”์‹œ์  ๋งํ‚น์€ ์‹คํ–‰ ํŒŒ์ผ(*.exe) ์ž์ฒด์— DLL์˜ ํ•จ์ˆ˜ ์ •๋ณด๋ฅผ ํฌํ•จ์‹œํ‚ค๋Š” ๋ฐฉ์‹์ด๋ฉฐ, ์‹คํ–‰ ํŒŒ์ผ๊ณผ ๋™์ผํ•œ ๊ฒฝ๋กœ์— dllํŒŒ์ผ์ด ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

์ง€๋‚œ ํฌ์ŠคํŒ…์—์„œ ๋‹ค๋ค˜๋˜ ์˜ˆ์ œ ์ฝ”๋“œ์— ์ฝ˜์†” ํ”„๋กœ์ ํŠธ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์†”๋ฃจ์…˜์„ ์„ ํƒํ•˜๊ณ  ์ƒˆ ํ”„๋กœ์ ํŠธ๋ฅผ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.

์ƒˆ ํ”„๋กœ์ ํŠธ ์ถ”๊ฐ€ํ•˜๊ธฐ

์ฝ˜์†” ์•ฑ์„ ์„ ํƒํ•˜๊ณ  MainDLL ์ด๋ผ๋Š” ์ด๋ฆ„์œผ๋กœ ํ”„๋กœ์ ํŠธ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

MainDLL ํ”„๋กœ์ ํŠธ ์ƒ์„ฑํ•˜๊ธฐ

ํ”„๋กœ์ ํŠธ๊ฐ€ ์ƒ์„ฑ๋˜๋ฉด MainDLL.cpp์— ์•„๋ž˜์™€ ๊ฐ™์ด ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค.

#include <iostream>

#include "../CreateDLL/MyMath.h"
#pragma comment(lib, "../x64/Debug/CreateDLL.lib")

int main()
{
	std::cout << "Sum: " << Sum(5.0, 3.0) << std::endl;
	std::cout << "Mul: " << Mul(5.0, 3.0) << std::endl;
}

๊ทธ๋ฆฌ๊ณ  MainDLL ํ”„๋กœ์ ํŠธ๋ฅผ ์‹œ์ž‘ ํ”„๋กœ์ ํŠธ๋กœ ์„ค์ •ํ•˜๊ณ  ๋นŒ๋“œ ๋ฐ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค.

MainDLL ํ”„๋กœ์ ํŠธ๋ฅผ ์‹œ์ž‘ ํ”„๋กœ์ ํŠธ๋กœ ์„ค์ •

์‹คํ–‰ ๊ฒฐ๊ณผ๋Š” ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

์‹คํ–‰ ๊ฒฐ๊ณผ

์ž˜ ๋”ฐ๋ผ์˜ค์…จ๋‚˜์š”? ์•”์‹œ์  ๋งํ‚น์€ ํ”„๋กœ์ ํŠธ ์„ค์ •์— include, library, dll ๊ฒฝ๋กœ๋ฅผ ์„ค์ •ํ•˜๋ฉด ๋๋‚ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿผ ๊ฐ ํ•ญ๋ชฉ์˜ ์˜๋ฏธ๋Š” ๋ฌด์—‡์ผ๊นŒ์š”? ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

ํ•ญ๋ชฉ ์„ค๋ช…
include dll ์ƒ์„ฑ ํ”„๋กœ์ ํŠธ์—์„œ ์‚ฌ์šฉํ–ˆ๋˜ ํ—ค๋” ํŒŒ์ผ
library dll ํ”„๋กœ์ ํŠธ ๋นŒ๋“œ ํ›„ ์ƒ์„ฑ๋œ lib ํŒŒ์ผ
dll dll ํ”„๋กœ์ ํŠธ ๋นŒ๋“œ ํ›„ ์ƒ์„ฑ๋œ dll ํŒŒ์ผ

๊ฐ ํ•ญ๋ชฉ๋ณ„๋กœ ์ข€ ๋” ์ž์„ธํžˆ ์„ค๋ช…ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

ํ—ค๋” ํŒŒ์ผ ์ถ”๊ฐ€ํ•˜๊ธฐ

MainDLL.cpp์—์„œ ์•„๋ž˜์˜ ์ฝ”๋“œ ํ•œ ์ค„์ด, dll ํ—ค๋” ํŒŒ์ผ์„ include ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

#include "../CreateDLL/MyMath.h"

์œ„์—์ฒ˜๋Ÿผ ๊ฒฝ๋กœ๋ฅผ ์ง์ ‘ ์ž…๋ ฅํ•ด๋„ ๋˜๊ณ , ์•„๋‹ˆ๋ฉด ์•„๋ž˜์ฒ˜๋Ÿผ ํ”„๋กœ์ ํŠธ ์†์„ฑ์˜ C/C++ > ์ผ๋ฐ˜ > ์ถ”๊ฐ€ ํฌํ•จ ๋””๋ ‰ํ„ฐ๋ฆฌ์— ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 

์ถ”๊ฐ€ ํฌํ•จ ๋””๋ ‰ํ„ฐ๋ฆฌ ์„ค์ •

๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ(lib)ํŒŒ์ผ ์ถ”๊ฐ€ํ•˜๊ธฐ

MainDLL.cpp์—์„œ ์•„๋ž˜์˜ ์ฝ”๋“œ ํ•œ ์ค„์ด, lib ํŒŒ์ผ์„ ๋งํฌํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

#pragma comment(lib, "../x64/Debug/CreateDLL.lib")

ํ—ค๋” ํŒŒ์ผ๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ, ๊ฒฝ๋กœ๋ฅผ ์ง์ ‘ ์ž…๋ ฅํ•˜๊ฑฐ๋‚˜ ํ”„๋กœ์ ํŠธ ์†์„ฑ์— ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ง์ปค > ์ผ๋ฐ˜ > ์ถ”๊ฐ€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋””๋ ‰ํ„ฐ๋ฆฌ์— ๊ฒฝ๋กœ๋ฅผ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค.

์ถ”๊ฐ€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋””๋ ‰ํ„ฐ๋ฆฌ ์„ค์ •

๊ทธ๋ฆฌ๊ณ  ๋ง์ปค > ์ž…๋ ฅ > ์ถ”๊ฐ€ ์ข…์†์„ฑ์— CreateDLL.lib๋ฅผ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค.

์ถ”๊ฐ€ ์ข…์†์„ฑ ์ž…๋ ฅ

DLL ๊ฒฝ๋กœ ์ถ”๊ฐ€ํ•˜๊ธฐ

๋””๋ฒ„๊น… ๋ชจ๋“œ์—์„œ ์‹คํ–‰ ํŒŒ์ผ ๊ฒฝ๋กœ์— dll ํŒŒ์ผ์„ ์˜ฎ๊ฒจ๋†“๊ธฐ ๊ท€์ฐฎ๋‹ค๋ฉด, ์•„๋ž˜์˜ ์„ค์ •์œผ๋กœ ํ•ด๋‹น ๊ฒฝ๋กœ์˜ dll์„ ๋ถˆ๋Ÿฌ์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ”„๋กœ์ ํŠธ ์†์„ฑ > ๋””๋ฒ„๊น… > ํ™˜๊ฒฝ์— path=๊ฒฝ๋กœ;%PATH%์™€ ๊ฐ™์ด ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค.

๋””๋ฒ„๊น… dll ๊ฒฝ๋กœ ์ž…๋ ฅ

์ฐธ๊ณ ๋กœ ๋ณธ๋ฌธ์˜ ์˜ˆ์ œ ์ฝ”๋“œ์˜ ๊ฒฝ์šฐ, CreateDLL ํ”„๋กœ์ ํŠธ์—์„œ ์ƒ์„ฑ๋œ dll ๋ฐ lib ํŒŒ์ผ ๊ฒฝ๋กœ์™€ MainDLL ํ”„๋กœ์ ํŠธ์˜ ์‹คํ–‰ ํŒŒ์ผ ๊ฒฝ๋กœ๊ฐ€ ๋™์ผํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ณ„๋„์˜ dll ๊ฒฝ๋กœ ์„ค์ • ์—†์ด๋„ ๋™์ž‘ํ•ฉ๋‹ˆ๋‹ค.

์—ฌ๊ธฐ๊นŒ์ง€ ์ž˜ ๋”ฐ๋ผ์˜ค์…จ๋‚˜์š”? ์œ„์—์„œ ์–ธ๊ธ‰ํ•œ ๋Œ€๋กœ ํ”„๋กœ์ ํŠธ์— ์„ค์ •์„ ์ž…๋ ฅํ–ˆ๋‹ค๋ฉด, ์•„๋ž˜์™€ ๊ฐ™์ด ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•ด์ง‘๋‹ˆ๋‹ค.

#include <iostream>

//#include "../CreateDLL/MyMath.h" //<-- ํ”„๋กœ์ ํŠธ ์†์„ฑ์— ์ž…๋ ฅ
//#pragma comment(lib, "../x64/Debug/CreateDLL.lib") //<-- ํ”„๋กœ์ ํŠธ ์†์„ฑ์— ์ž…๋ ฅ

#include "MyMath.h"

int main()
{
	std::cout << "Sum: " << Sum(5.0, 3.0) << std::endl;
	std::cout << "Mul: " << Mul(5.0, 3.0) << std::endl;
}

๋ณธ๋ฌธ์˜ ์˜ˆ์ œ๋Š” ์•„๋ž˜ ์ฒจ๋ถ€๋œ ํŒŒ์ผ์„ ์ฐธ๊ณ ํ•ด ์ฃผ์„ธ์š”.

ExamDLL.zip
0.01MB

๋‹ค์Œ ํฌ์ŠคํŒ…์—์„œ๋Š” ๋ช…์‹œ์  ๋งํ‚น(Explicit Linking)์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

๋ฐ˜์‘ํ˜•