본문 바로가기

컴퓨터 과학 & 영상처리 관련/Visual Studio

.cpp .h 파일로 lib 라이브러리 파일 만들고 사용하기


참고

http://copynull.tistory.com/25


http://ko.wikipedia.org/wiki/%EB%8F%99%EC%A0%81_%EB%A7%81%ED%81%AC_%EB%9D%BC%EC%9D%B4%EB%B8%8C%EB%9F%AC%EB%A6%AC


visual studio 솔루션 경로 설정시 (http://200315193.tistory.com/2193) 참고






평소 exe로 돌아가는 프로그램을

쩌어기 Target Extention, Configuration Type만 .lib 또는 .dll로 해도 컴파일 되고

그 중 lib 파일만 갖다 써도 됨

dll만 쓰는 경우는 c, c++에서는 아직 안해봤고...나한테는 어렵고 c#에서는 dll 추가라는게 있으니 쉽긴 하지만..


---------------------

사용법


1. 헤더파일에 아래 소스코드 추가

#pragma once

#include "../MyLibSet/MyLibrary.h"

#include "../MyLibSet/MyOpenCV.h"


2. 프로젝트 폴더와 같은 노드 레벨에 MyLibSet 폴더 복사 

-> 현재 MyLib는 dll 안쓰고 exe 파일에 합쳐져서 컴파일 됨

-> OpenCV 쓸 때에는 실행 폴더에 dll 추가해야 됨



3. 프로젝트 옵션에서 추가 포함 폴더 추가

-> Configuration Properties -> C/C++ -> General -> Additional Include Directories = ../MyLibSet/opencv/include

->기존 에는 opencv/include라던가 존나게 추가했었는데 다 필요 없음

#include "opencv/include/opencv2/opencv.hpp" 

이거 하나로 추가하면 opencv.hpp 안에서 다 부르니깐



★팁

//프로젝트 경로로부터 상대 경로임..MyLibSet으로부터가 아니라

#pragma comment(lib, "../MyLibSet/MyLib/lib/MyLibrary.lib")    //끝에 lib 추가


//현재 폴더로부터 상대경로임. 아래 소스는 MyLibSet/MyLibrary.h가 MyLibSet/MyLib/src/MyPath.h를 부르고자 한 경우임

#include "MyLib/src/MyPath.h"


x86, x64 변경해서 컴파일 시 OpenCV도 맞게 설정해줘야 함수 못찾는 일 안나옴. 


-------------------------------------------------

함수 못찾는다고 할 때에 아래 처럼 \\로 대체해서 쓰면 되는 경우도 있나? 뭐 꼭 그게 문제는 아닌거 같은데. 

1. 프로토타입 추가 안했거나

2. lib 추가 안했거나

3. x64,x86 이런거 안지켰거나



#include "..\\MyLibSet\\MyLibrary.h"


#pragma once


#pragma comment(lib, "..\\MyLibSet\\MyLib\\lib\\MyLibrary.lib")


#include "MyLib\\src\\MyPath.h"

#include "MyLib\\src\\MyTimeCalc.h"

#include "MyLib\\src\\MyFileIO.h"

#include "MyLib\\src\\MyImgProc.h"

-------------------------------------------------------
debug x86, x64로 만들고 사용 시에는 되는거 같은데 release x64는 작동 안함. 



dll의 경우 어찌하나?




// Force a symbolic reference to the global VisualLeakDetector class object from

// the DLL. This ensures that the DLL is loaded and linked with the program,

// even if no code otherwise imports any of the DLL's exports.

#pragma comment(linker, "/include:__imp_?g_vld@@3VVisualLeakDetector@@A")






// VLDEnable - Enables Visual Leak Detector's memory leak detection at runtime.

//   If memory leak detection is already enabled, which it is by default, then

//   calling this function has no effect.

//

//  Note: In multithreaded programs, this function operates on a per-thread

//    basis. In other words, if you call this function from one thread, then

//    memory leak detection is only enabled for that thread. If memory leak

//    detection is disabled for other threads, then it will remain disabled for

//    those other threads. It was designed to work this way to insulate you,

//    the programmer, from having to ensure thread synchronization when calling

//    VLDEnable() and VLDDisable(). Without this, calling these two functions

//    unsynchronized could result in unpredictable and unintended behavior.

//    But this also means that if you want to enable memory leak detection

//    process-wide, then you need to call this function from every thread in

//    the process.

//

//  Return Value:

//

//    None.

//

__declspec(dllimport) void VLDEnable ();


// VLDRestore - Restore Visual Leak Detector's previous state.

//

//  Return Value:

//

//    None.

//

__declspec(dllimport) void VLDRestore ();




vld.h (Visual Leak Detector 라이브러리) 에서는 위와 같이 dll의 함수를 사용하는 거 같긴 한데. 테스트 해볼까?

vld_x64.dll, dbghelp.dll 이정도만 사용하는데...;

-> Error 1 error LNK2019: unresolved external symbol "__declspec(dllimport) void __cdecl writeText(char *,char *,char *)" (__imp_?writeText@@YAXPAD00@Z) referenced in function _main D:\TestProject\Test.obj


__imp_?writeText@@YAXPAD00@Z) 

__imp_?g_vld@@3VVisualLeakDetector@@A")

-> http://copynull.tistory.com/25 확인해서 보자. calling convention 같은거 __cdecl 문제같으니

-> http://exportidea.blogspot.kr/2013/08/windows-dll.html

->씨벌 복잡한거였네...때려치고 나중에 공부하자