一:c++代碼封裝成dll庫
以下內容是如何讓將vs中寫的c++函數封裝成dll
1.在vs2013中新建一個項目,
項目名
comface
頭文件
face.h
源文件
face.cpp
Source.def
2.main.cpp中複製以下內容
// dllmain.cpp : 定義 DLL 應用程序的入口點。
#include <windows.h>
BOOL APIENTRY DllMain(HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
3.saveTrain.cpp中寫入需要調用的函數
以上是我的部分函數;
saveTrain.h中寫入相關的頭文件,
#include "opencv2/dnn.hpp"
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include <fstream>
#include <iostream>
#include <cstdlib>
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <stdio.h>
#include<direct.h>
#include<sstream>
#include<string>
#include <time.h>
#include<math.h>
using namespace std;
using namespace cv;
using namespace cv::dnn;
void createFile(string dir){//函數的內容}
float cov(const vector<float>& v1, const std::vector<float>& v2){...}
Mat comface(String FileName){...}
4.在源文件右鍵添加新建項,vc++目錄下選擇--代碼--模塊定義文件
LIBRARY "saveTXT"
EXPORTS
fileEmpty @1
inttostr @2
detectAndDisplay @3
init1 @4
train1 @5
寫入函數名並在後面標上@+數字
5.在所建項目的debug文件夾下即生成了dll和lib文件
如果出現報錯,可能的問題是source.def中的函數名的問題,建議改個函數名字。
二、驗證dll庫
新建一個項目,在項目名上右鍵---屬性--vc目錄,找到包含目錄,加入剛剛生成的.h的頭文件目錄;
找到庫目錄,加入dll和lib所在目錄;
-- 屬性--鏈接器,加入lib所在路徑;
即可在新建的項目裏面調用函數了。
如果出現dll報錯,可能的原因是生成的dll沒有放到現在項目所在的debug目錄下。