{"id":359,"date":"2022-08-30T15:00:02","date_gmt":"2022-08-30T15:00:02","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/06\/compiling-opencv-2-3-1-programs-with-mingw-gcc-g-on-windows-7-64bit-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:00:02","modified_gmt":"2022-08-30T15:00:02","slug":"compiling-opencv-2-3-1-programs-with-mingw-gcc-g-on-windows-7-64bit-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/compiling-opencv-2-3-1-programs-with-mingw-gcc-g-on-windows-7-64bit-collection-of-common-programming-errors\/","title":{"rendered":"Compiling openCV 2.3.1 programs with MinGW gcc\/g++ on Windows 7 64bit-Collection of common programming errors"},"content":{"rendered":"<ul>\n<li>\n<p>In case someone else needs to solve this issue, here&#8217;s how I got the posted OpenCV\/HighGUI sample code to compile in Windows 7 x64 using MinGW, MSYS, and CMake:<\/p>\n<ol>\n<li>\n<p>build OpenCV from source using MinGW\/MSYS\/CMake. This is because I could not get the MinGW compiled version in the OpenCV-win-SuperPack to link properly in MinGW\/MSYS\/Windows 7 x64.<\/p>\n<p>For full reference, here&#8217;s how I compiled OpenCV:<\/p>\n<ul>\n<li>make sure you have an up-to-date CMake (v2.6 or later) and MinGW (with GCC, G++, and MSYS options) installed<\/li>\n<li>if you want the new Qt-based OpenCV HighGUI front-end, you will need to install Qt 4 (SDK).<\/li>\n<li>download a OpenCV source\/superpack version 2.2 or later (I used OpenCV-2.3.1-win-superpack.exe)<\/li>\n<li>unzip the contents to [OPENCV_SOURCE_DIR] (I put it in C:\/opencv, so there should be a file at C:\/opencv\/README for example)<\/li>\n<li>create a [OPENCV_BUILD_DIR] directory elsewhere (I used C:\/opencv\/build\/mingw)<\/li>\n<li>use the CMake-GUI tool, specify the source directory as [OPENCV_SOURCE_DIR], the build directory as [OPENCV_BUILD_DIR], and click &#8220;Configure&#8221;.<\/li>\n<li>you may wish\/need to go tweak the options (e.g. I ticked &#8220;Qt&#8221; and &#8220;Qt-OpenGL&#8221; entries, then clicked &#8220;Configure&#8221; again, then had to provide the path to the qmake executable)<\/li>\n<li>once you have finished configuring OpenCV, click &#8220;Generate&#8221;<\/li>\n<li>in a MSYS terminal, browse to [OPENCV_BUILD_DIR], and run &#8220;make&#8221; to build the code (this may take a while)<\/li>\n<li>once the has been built properly, run &#8220;make install&#8221;, which collects the built code\/libraries\/include dirs into [OPENCV_BUILD_DIR]\/install folder (or a different folder if you changed the corresponding option when using the CMake-GUI tool)<\/li>\n<li>add [OPENCV_BUILD_DIR]\/install\/bin folder to the PATH environmental variable. If you do not know how to do this, then I&#8217;d recommend using the Path Editor GUI tool.<\/li>\n<li>if you end up using Qt, you will also need to put the bin folder of Qt SDK in the PATH environmental variable. This is the folder that includes qmake.exe.<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>put the following sample code into a file called test.c. I modified the includes slightly to make them compatible with OpenCV v2.2 and above.<\/p>\n<pre><code> #include \n #include \n #include \n #include \n #include \n\n int main(int argc, char *argv[])\n {\n   \/\/ Nothing but create a window\n   cvNamedWindow(\"mainWin\", CV_WINDOW_AUTOSIZE); \n   cvMoveWindow(\"mainWin\", 100, 100);\n   cvWaitKey(0);\n   return 0;\n } \n<\/code><\/pre>\n<\/li>\n<li>\n<p>in a MSYS terminal, browse to the folder where you put test.c, and run:<\/p>\n<pre><code>gcc -o test -I\"[OPENCV_BUILD_DIR]\/install\/include\" test.c \\\n  -L\"[OPENCV_BUILD_DIR]\/install\/lib\" \\\n  -lopencv_core[OPENCV_VERSION] \\\n  -lopencv_imgproc[OPENCV_VERSION] \\\n  -lopencv_highgui[OPENCV_VERSION]\n<\/code><\/pre>\n<p>So in my case:<\/p>\n<pre><code>gcc -o test -I\"\/c\/opencv\/build\/mingw\/install\/include\" test.c \\\n  -L\"\/c\/opencv\/build\/mingw\/install\/lib\" \\\n  -lopencv_core231\n  -lopencv_imgproc231\n  -lopencv_highgui231\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p>Path Editor: http:\/\/www.redfernplace.com\/software-projects\/patheditor\/<\/p>\n<\/li>\n<li>\n<p>You have the directory, <code>C:\\opencv\\install\\bin<\/code>, to locate libraries on the <code>gcc<\/code>\/<code>g++<\/code> command line, but I think you&#8217;ll also need to specify the libraries to use as linker inputs as well. I&#8217;m not sure what libraries are part of the OpenCV distribution, but going by the example on the instruction page you linked to, one might be:<\/p>\n<pre><code> -lopencv_calib3d220.dll\n<\/code><\/pre>\n<p>You&#8217;ll probably have to add one or more other ones (that follow the name pattern <code>lib*.a<\/code> in the <code>C:\\opencv\\install\\bin<\/code> directory &#8211; or maybe some other <code>lib<\/code> directory that you should be passing in a <code>-L<\/code> option).<\/p>\n<\/li>\n<\/ul>\n<p id=\"rop\"><small>Originally posted 2013-11-06 03:16:27. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>In case someone else needs to solve this issue, here&#8217;s how I got the posted OpenCV\/HighGUI sample code to compile in Windows 7 x64 using MinGW, MSYS, and CMake: build OpenCV from source using MinGW\/MSYS\/CMake. This is because I could not get the MinGW compiled version in the OpenCV-win-SuperPack to link properly in MinGW\/MSYS\/Windows 7 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-359","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/359","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=359"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/359\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=359"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=359"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=359"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}