๋ฐ˜์‘ํ˜•

๋ณธ๋ฌธ์„ ์ฝ๊ธฐ ์ „์— ์•„๋ž˜์˜ ๋งํฌ๋ฅผ ์ฐธ๊ณ ํ•˜๋ฉด ์ดํ•ดํ•˜๋Š”๋ฐ ๋„์›€์ด ๋ฉ๋‹ˆ๋‹ค.

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

[C++ DLL] Visual Studio C++ ์•”์‹œ์  ๋งํฌํ•˜๊ธฐ (์˜ˆ์ œ ํฌํ•จ)

[C++ DLL] Visual Studio C++ ๋ช…์‹œ์  ๋งํฌํ•˜๊ธฐ (์˜ˆ์ œ ํฌํ•จ)

C++ ํด๋ž˜์Šค๋ฅผ DLL๋กœ ๋งŒ๋“ค๊ธฐ

์ง€๊ธˆ๊นŒ์ง€ C++ ํ•จ์ˆ˜๋ฅผ DLL๋กœ ๋งŒ๋“ค์–ด(Export) ์™ธ๋ถ€ ํ”„๋กœ์ ํŠธ์—์„œ ํ˜ธ์ถœ(Import)ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณด์•˜๋Š”๋ฐ์š”. ๊ทธ๋Ÿฐ๋ฐ Class ๊ฐ์ฒด๋Š” ์–ด๋–ป๊ฒŒ DLL๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ์„๊นŒ์š”? ์•„๋ž˜ ์ฝ”๋“œ ์˜ˆ์‹œ์™€ ๊ฐ™์ด ๋ง์ด์ฃ .

//DLL ์ƒ์„ฑ ํ”„๋กœ์ ํŠธ์˜ ํ—ค๋”
class Person {
public:
	Person(std::string name, int age, std::string address);
	void ShowInfo();

private:
	int age;
	std::string name;
	std::string address;
};

//DLL์„ ํ˜ธ์ถœํ•˜๋Š” ์™ธ๋ถ€ ํ”„๋กœ์ ํŠธ
Person person1("ํ™๊ธธ๋™", 26, "์„œ์šธ์‹œ");
Person1.ShowInfo();

๋ณดํ†ต ํด๋ž˜์Šค ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด 14๋ฒˆ์งธ ์ค„๊ณผ ๊ฐ™์ด ๊ฐ์ฒด๋ฅผ ์„ ์–ธํ•˜๊ณ , 15๋ฒˆ์งธ ์ค„๊ณผ ๊ฐ™์ด ๊ฐ์ฒด์— ์ ‘๊ทผํ•˜์—ฌ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋Š”๋ฐ์š”. ํ•จ์ˆ˜ ์ด๋ฆ„ ์•ž์— __declspec(dllexport) ํ‚ค์›Œ๋“œ๋ฅผ ๋ถ™์ด๋“ฏ์ด, ํด๋ž˜์Šค ์ด๋ฆ„์— ๋ถ™์—ฌ ๋ณด์•˜์ง€๋งŒ ์•ˆ๋˜๋”๊ตฐ์š”. ๋‚˜์ค‘์— ์†Œ๊ฐœํ•  MFC ํ™•์žฅ DLL์—์„œ๋Š” AFX_EXT_CLASS ํ‚ค์›Œ๋“œ๋ฅผ ํด๋ž˜์Šค ์ด๋ฆ„ ์•ž์— ๋ถ™์ด๋ฉด, ์œ„ ์ฝ”๋“œ ์˜ˆ์‹œ์™€ ๊ฐ™์ด ์„ ์–ธํ•˜๊ณ  ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ด๋ฒˆ ํฌ์ŠคํŒ…์—์„œ๋Š” ์ƒ๊ฐ๊ณผ๋Š” ์กฐ๊ธˆ ๋‹ค๋ฅด์ง€๋งŒ, C++ ํด๋ž˜์Šค๋ฅผ DLL๋กœ Export ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์†Œ๊ฐœํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์˜ˆ์ œ ์ฝ”๋“œ๋Š” ๋ณธ๋ฌธ์˜ ํ•˜๋‹จ์— ์ฒจ๋ถ€๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.

C++ Class DLL Export ๋ฐฉ๋ฒ•

๋ณธ๋ฌธ์—์„œ ์ œ์‹œํ•  ๋ฐฉ๋ฒ•์€ ์•„์ฃผ ๊ฐ„๋‹จํ•ฉ๋‹ˆ๋‹ค. ํด๋ž˜์Šค ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ๋ฐ˜ํ™˜ํ•˜๋Š” ํŒฉํ† ๋ฆฌ ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค๊ณ , ๋ฐ˜ํ™˜๋œ ๊ฐ์ฒด๋กœ ์ธ์ž๋ฅผ ๋ฐ›๊ณ  ํŠน์ • ๋ฉค๋ฒ„ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ํ•จ์ˆ˜๋“ค์„ ์ •์˜ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

MyDLL.h ์ฝ”๋“œ๋ฅผ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

#pragma once

#include <string>
#include <iostream>

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

class Person {
public:
	Person(std::string name, int age, std::string address);
	void ShowInfo();

private:
	int age;
	std::string name;
	std::string address;
};

extern "C" {
	MYDLL_DECLSPEC Person* CreatePerson(char* name, int age, char* address);
	MYDLL_DECLSPEC void ShowInfo(Person* obj);
	MYDLL_DECLSPEC void DeletePerson(Person* obj);
}

๋‹ค์Œ์€ MyDLL.cpp์ž…๋‹ˆ๋‹ค.

#include "pch.h"
#include "MyDLL.h"

Person::Person(std::string name, int age, std::string address) {
	this->age = age;
	this->name = name;
	this->address = address;
}

void Person::ShowInfo() {
	std::cout << "์ด๋ฆ„: " << name << std::endl;
	std::cout << "๋‚˜์ด: " << age << std::endl;
	std::cout << "์ฃผ์†Œ: " << address << std::endl;
}

Person* CreatePerson(char* name, int age, char* address)
{
	std::cout << "Person ๊ฐ์ฒด ์ƒ์„ฑ" << std::endl;
	return new Person(name, age, address);
}

void ShowInfo(Person* obj)
{
	obj->ShowInfo();
}

void DeletePerson(Person* obj)
{
	delete obj;
	obj = nullptr;

	std::cout << "Person ๊ฐ์ฒด ํ•ด์ œ" << std::endl;
}

Person Class๊ฐ€ MyDLL.h์— ์ •์˜๋˜์–ด ์žˆ๊ณ , ๋ชธ์ฒด๋Š” MyDLL.cpp์— ๊ตฌํ˜„๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ํŒฉํ† ๋ฆฌ ํ•จ์ˆ˜์ธ CreatePerson() ํ•จ์ˆ˜๋Š” ์ธ์ž๋กœ ๊ธฐ๋ณธ ์ •๋ณด๋ฅผ ๋„˜๊ฒจ๋ฐ›๊ณ , new Person()์„ ํ†ตํ•ด ์ƒ์„ฑ๋œ ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด๋ ‡๊ฒŒ ๋ฐ˜ํ™˜๋œ ๊ฐ์ฒด ํฌ์ธํ„ฐ๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ฐ์ฒด์˜ ๋ฉค๋ฒ„ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ShowInfo(Person* obj)์ฒ˜๋Ÿผ ๋ง์ด์ฃ . ๋งˆ์ง€๋ง‰์œผ๋กœ ๊ฐ์ฒด๋ฅผ ๋” ์ด์ƒ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด DeletePerson(Person* obj)์„ ํ˜ธ์ถœํ•˜์—ฌ ํ•ด์ œํ•ด ์ค˜์•ผ ํ•ฉ๋‹ˆ๋‹ค.

DLL์„ ํ˜ธ์ถœํ•˜๋Š” ํ”„๋กœ์ ํŠธ์˜ MainDLL.cpp ์ฝ”๋“œ๋ฅผ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

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

int main()
{
	char name[] = "ํ™๊ธธ๋™";
	char address[] = "์„œ์šธ์‹œ";
	
	Person* person1 = CreatePerson(name, 35, address);
	ShowInfo(person1);
	DeletePerson(person1);
	
	getchar();
}

ํŒฉํ† ๋ฆฌ ํ•จ์ˆ˜์ธ CreatePerson()์„ ํ˜ธ์ถœํ•˜์—ฌ Person๊ฐ์ฒด์˜ ํฌ์ธํ„ฐ๋ฅผ ๋ฐ˜ํ™˜๋ฐ›์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ทธ ๊ฐ์ฒด ํฌ์ธํ„ฐ๋ฅผ ์ด์šฉํ•˜์—ฌ ShowInfo()๋ฅผ ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ๊ณ , ์‚ฌ์šฉ์ด ๋๋‚˜๋ฉด DeletePerson()์„ ํ˜ธ์ถœํ•˜์—ฌ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ํ•ด์ œํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์‹คํ–‰ ๊ฒฐ๊ณผ๋Š” ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

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

๋งŒ์•ฝ, ์•„๋ž˜์™€ ๊ฐ™์ด ํด๋ž˜์Šค ๋ฉค๋ฒ„ ํ•จ์ˆ˜์— ์ ‘๊ทผํ•˜๋ฉด ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.

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

int main()
{
	char name[] = "ํ™๊ธธ๋™";
	char address[] = "์„œ์šธ์‹œ";
	
	Person* person1 = CreatePerson(name, 35, address);
	person1->ShowInfo(); // LNK2019
}

ํด๋ž˜์Šค ์ •๋ณด ์ž์ฒด๊ฐ€ Export๋œ ๊ฒƒ์ด ์•„๋‹ˆ๊ธฐ ๋•Œ๋ฌธ์— ์ฐธ์กฐํ•  ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.

LNK2019 ์—๋Ÿฌ

์ด๋ ‡๊ฒŒ ๊ตฌํ˜„ํ•˜๋ฉด ํด๋ž˜์Šค ๊ฐ์ฒด๋ฅผ DLL๋กœ ๋งŒ๋“ค์–ด ์™ธ๋ถ€์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ž์„ธํ•œ ์‚ฌ์šฉ ๋ฐฉ๋ฒ•์€ ์•„๋ž˜ ์˜ˆ์ œ ์ฝ”๋“œ๋ฅผ ์ฐธ๊ณ ํ•ด ์ฃผ์„ธ์š”.

ExamDLL.zip
0.01MB

์ด์–ด์„œ ๋‹ค์Œ ํฌ์ŠคํŒ…์—์„œ๋Š” MFC ํ™•์žฅ DLL๋กœ ์‰ฝ๊ฒŒ ํด๋ž˜์Šค ๊ฐ์ฒด๋ฅผ DLL๋กœ ๋งŒ๋“ค๊ณ , ์™ธ๋ถ€์—์„œ ์–ด๋–ป๊ฒŒ ์‚ฌ์šฉํ•˜๋Š”์ง€ ์†Œ๊ฐœํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

๋ฐ˜์‘ํ˜•