xing's Blog

Give me a place to stand on, and I'll move the earth.

OpenCV related DLL in MinGW & VC 2005 Express

I used to MATLAB programming for a long time. I began to find an alternative when the limitations of it arise. I had focused on mixed programming which is still an interest of me. Using C/C++,Fortran,Lisp become a choice when I evaluated various integrated software environments more or less. OpenCV is a wonderful package for image processing and computer vision both in Widnows and in Linux. Linux has a much easier mechanism to develop and use shared library (dynamically linked library) than that in windows. I had planned use Linux as my working platform substituting windows for a long time. But even now I had to return to windows for some work because the surrounding environment is full of date and formats created with products of M$.  Fortunately there are lots of Linux tools which can be used in Windows. MingGW is on

e of them. Creating and Using DLL with MinGW and VC 2005 express , another free (no cost) compiler from M$, is discussed below.

comp.bat:
set path=f:\mingw\bin;%path%
rem mingw32 is used to produce shared library in windowsxp. test.c is the first simple source to succeed.
f:\mingw\bin\gcc  -shared  -DBUILDING_DLL -I"F:\Program Files\OpenCV\cxcore\include" -I"F:\Program Files\OpenCV\cv\include"  -I"F:\Program Files\OpenCV\cvaux\include" -I"F:\Program Files\OpenCV\ml\include" -I"F:\Program Files\OpenCV\otherlibs\highgui" test.c -L"F:\Program Files\OpenCV\lib"   -lcxcore -lcv -lcvaux -lml -lhighgui  -o test.so

rem test1.c is the complete source to create dll with opencv library. Function "main" can be imported from newlisp.
f:\mingw\bin\g++  -shared  -DBUILDING_DLL -I"F:\Program Files\OpenCV\cxcore\include" -I"F:\Program Files\OpenCV\cv\include"  -I"F:\Program Files\OpenCV\cvaux\include" -I"F:\Program Files\OpenCV\ml\include" -I"F:\Program Files\OpenCV\otherlibs\highgui" test1.cpp -L"F:\Program Files\OpenCV\lib"   -lcxcore -lcv -lcvaux -lml -lhighgui  -o test1.so

rem msvc2005express is used to produce dll
cl /LD /nologo /ML /EHsc /D"_MBCS"  /I"F:\Program Files\OpenCV\cxcore\include" /I"F:\Program Files\OpenCV\cv\include" /I"F:\Program Files\OpenCV\cvaux\include" /I"F:\Program Files\OpenCV\ml\include" /I"F:\Program Files\OpenCV\otherlibs\highgui" /I"F:\Program Files\Microsoft Visual Studio 8\VC\include" test1.cpp /link /libpath:"F:\Program Files\OpenCV\lib" /libpath:"F:\Program Files\Microsoft Visual Studio 8\VC\lib" cxcore.lib cv.lib cvaux.lib ml.lib highgui.lib

rem msvc2005express can produce a dll directly from the original source of bezier.cpp, but ther is no funciton can be exported and invoked by other program while mingw32 refused to produce such a dll.
cl /LD /nologo /ML /EHsc /D"_MBCS"  /I"F:\Program Files\OpenCV\cxcore\include" /I"F:\Program Files\OpenCV\cv\include" /I"F:\Program Files\OpenCV\cvaux\include" /I"F:\Program Files\OpenCV\ml\include" /I"F:\Program Files\OpenCV\otherlibs\highgui" /I"F:\Program Files\Microsoft Visual Studio 8\VC\include" bezier.cpp /link /libpath:"F:\Program Files\OpenCV\lib" /libpath:"F:\Program Files\Microsoft Visual Studio 8\VC\lib" cxcore.lib cv.lib cvaux.lib ml.lib highgui.lib


end of comp.bat
the line of invoking compiler is too long to be displayed in one line. But I'm not sure when it can work properly, and I gave up the effort to break it.



test1.c
# define DLLIMPORT __declspec (dllexport)
...
DLLIMPORT int main(int argc, char* argv[])
...

end of test1.c

in newLISP "main" function can be imported and invoked correctly:)