昨天看了dll的相關知識,打算自己來實踐下,於是結合OpenCV寫了一個實現RGB2gray的函數,代碼如下:
imgprocess.h
#pragma once
#include <string>
#define DLL_EXPORT_API extern "C" _declspec(dllexport)
//Function
DLL_EXPORT_API int Add(int a,int b);
DLL_EXPORT_API int huidu(string sImagePath);
//Class
class _declspec(dllexport) Math
{
public:
int Multiply(int a,int b);
};
imgprocess.cpp
//#include "stdafx.h"
#include "imgprocess.h"
#include <cv.h>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;
int Add(int a,int b)
{
return a+b;
}
int huidu(string sImagePath)
{
Mat img=imread("Lena.png");
Mat grayimg;
cvtColor(img,grayimg,CV_BGR2GRAY);
namedWindow("src");
namedWindow("dst");
imshow("src",img);
imshow("dst",grayimg);
waitKey(600);
return 0;
}
int Math::Multiply(int a,int b)
{
return a*b;
}
然後編譯的時候,一直報錯, error C2365: “huidu”: 重定義;以前的定義是“數據變量”,將鼠標放在huidu這個函數上,提示鏈接規範與前面的“huidu”(已聲明:cpp裏面的第12行)不兼容,檢查了代碼,沒有問題,於是就開始度娘了,給出的好多都是error C2365: “XXX“: 重定義;以前的定義是“函數”,回答都是說函數重定義或者說是與MFC內存管理衝突,都沒有解決我的問題。再看我的錯誤,發現還提示string未聲明的標識符,咦,頭文件加了啊,難道是沒加命名空間?於是在imgprocess.h中加上:
#pragma once
#include <string>
#define DLL_EXPORT_API extern "C" _declspec(dllexport)
using namespace std;
//Function
DLL_EXPORT_API int Add(int a,int b);
DLL_EXPORT_API int huidu(string sImagePath);
//Class
class _declspec(dllexport) Math
{
public:
int Multiply(int a,int b);
};
編譯成功了!成功生成了dll和lib文件。我的思維定勢一直將命名空間加在cpp中,因爲這裏定義了一個string的變量,所以也需要!特此記錄!