2019년 2월 21일 목요일

DLL ( C++ for unity)

1. Library project 만들기






Header & cpp 추가 ( project 이름 / Add / New Item )




프로젝트 만들고 나서 확인 해야 할 사항
property -> C/C++ -> Preprocessor -> Preprocessor Definitions 에

MATHLIBRARY_EXPORTS 가 있는지 확인( project명_EXPORTS )













우선 Header 부터 생성




내용 추가






















내용

#pragma once

//MATHLIBRARY_EXPORTS 가 Preprocessor Definitions 에 추가 되있기 때문에 윗줄 활성
#ifdef MATHLIBRARY_EXPORTS
#define MATHLIBRARY_API __declspec(dllexport)
#else
#define MATHLIBRARY_API __declspec(dllimport)
#endif

// This function must be called before any other function.
extern "C" MATHLIBRARY_API void Fibonacci_init(const long long a, const long long b);

// Produce the next value in the sequence.
// Returns true on success and updates current value and index;
// false on overflow, leaves current value and index unchanged.
extern "C" MATHLIBRARY_API bool fibonacci_next();

// Get the current value in the sequence.
extern "C" MATHLIBRARY_API unsigned long long fibonacci_current();

// Get the position of the current value in the sequence.
extern "C" MATHLIBRARY_API unsigned fibonacci_index();





다음으로 CPP 파일 수정 ( Source Files / MathLibrary.cpp )



내용
#include "stdafx.h"
#include <utility>
//#include <limits.h>
#include "MathLibrary.h"

// DLL internal state variables:
static unsigned long long previous_;  // Previous value, if any
static unsigned long long current_;   // Current sequence value
static unsigned index_;               // Current seq. position

// Initialize a Fibonacci relation sequence
// such that F(0) = a, F(1) = b.
// This function must be called before any other function.
void fibonacci_init(const unsigned long long a, const unsigned long long b)
{
index_ = 0;
current_ = a;
previous_ = b; // see special case when initialized
}

// Produce the next value in the sequence.
// Returns true on success, false on overflow.
bool fibonacci_next()
{
// check to see if we'd overflow result or position
if ((ULLONG_MAX - previous_ < current_) || (UINT_MAX == index_))
{
return false;
}

// Special case when index == 0, just return b value
if (index_ > 0)
{
// otherwise, calculate next sequence value
previous_ += current_;
}
std::swap(current_, previous_);
++index_;
return true;
}

// Get the current value in the sequence.
unsigned long long fibonacci_current()
{
return current_;
}

// Get the current index position in the sequence.
unsigned fibonacci_index()
{
return index_;
}


이후 빌드
빌드는 메뉴의 Build / Build Solution ( ctrl + shift +B ) 이용
F5 지양

빌드 성공시 해당 프로젝트의 Release / Debug 폴더 내부에


이러한 파일들 생성됨




2. Client project 만들기 (C++)
외부 라이브러리를 사용하기 위해서는
해당 프로젝트의 Properties -> C/C++ -> General -> Additional Include Directories 에서
MathLibrary.h 가 위치한 폴더의 경로를 추가

..\..\MathLibrary\MathLibrary







여기 까지만 하면 함수는 가져다 써지지만
링크에러 발생
해당 프로젝트의 Properties -> Linker -> Input -> Additional Dependencies 에서
Edit



Additional Dependencies 에 MathLibrary.lib 추가





해당 프로젝트의 Properties -> Linker -> General -> Additional Library Directories 에서
Edit





.LIB 파일을 사용하는 세팅 완료

다음은 DLL 파일 사용을 위한 세팅
해당 프로젝트의 Properties -> Build Events -> Post-Build Event -> Command Line 에서
Edit




xcopy /y /d "..\..\MathLibrary\$(IntDir)MathLibrary.dll" "$(OutDir)"

를 복사


여기까지 실수 없이 했다면 될 것이고, 만약 안된다면 그 이유를 찾아 고치고
프로젝트를 재시작 하고 나서 클린 & 빌드 하면 될 것이다.