Ask Your Question

Revision history [back]

Open CMake, and point it to the OpenCV sources directory.

Create a new folder under OpenCV called my_build.

Point CMake's "Where to build the binaries" field to the my_build folder.

Hit Configure. CMake will ask you about the compiler. If you want the CodeBlocks build, you should specify that it's MinGW for CodeBlocks, because OpenCV 's cmake file will output a CodeBlocks file that will help with compiling. You make need to specify the native compiler to match the compiler used for your project.

Once the Configure process has finished, everything should be in red. This does not indicate an error. CMake needs you to verify the configuration data.

1 - Find the "With" drop-down, and uncheck WITH_IPP (and IPP_A, if it is checked). These only work under Visual Studios. 2 - Under "Build", check BUILD_SHARED_LIBS for dlls, uncheck for .a 3 - Check under the "CMake" tab to make sure CMake hasn't changed any of your compiler options, as it often does. It changed 2 of mine. Check: CMAKE_AR, CMAKE_LINKER, CMAKE_MAKE_PROGRAM, NM, OBJCOPY, OBJDUMP, RANLIB, RC_COMPILER, STRIP

Click "Configure" again.

Any time you click configure, you should check all of the above options because CMake sometimes loses track of your settings. Once you have nothing else in red, and every option is correct, click Generate.

Navigate to the my_build directory under the OpenCV folder. You should see an OpenCV.cbp. Open it in CodeBlocks.

Configure CodeBlocks to use the same compiler that was specified in CMake.

Compile.

You will most likely encounter 4 errors in 'opencv/sources/modules/videoio/src/cap_dshow.cpp' pertaining to ISampleGrabberCB, ISampleGrabber, IEnumPIDMap, and IMPEG2PIDMap. Each of these interfaces needs a virtual destructor added to them (for example, in IEnumPIDMap, add virtual ~IEnumPIDMap(){} ). Adding these fixes the issues.

Hit compile. Sit back and wait.

If you are using TDM as the compiler, you'll most likely encounter ANOTHER issue with libstdc++ claiming a whole mess of things are redefined/have multiple definitions. If this happens, go to your my_build directory in Cygwin, and run the following command:

grep -rl 'lstdc' ./ | xargs sed -i 's/lstdc/static-libstdc/g'

The command replaces a bunch of dynamic linking instances with statically linked instances of libstdc.

Hit compile to start compiling from where you left off.

Testing

Once the compile is done, in the my_build libraries, you will have a /lib/ with the .a's, and a /bin/ with the dlls

Create a new console project in CodeBlocks, and use the following code:

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main( int argc, char** argv )
{
    if( argc != 2)
    {
     cout <<" Usage: display_image ImageToLoadAndDisplay" << endl;
     return -1;
    }

    Mat image;
    image = imread(argv[1], IMREAD_COLOR); // Read the file

    if(! image.data ) // Check for invalid input
    {
        cout << "Could not open or find the image" << std::endl ;
        return -1;
    }

    namedWindow( "Display window", WINDOW_AUTOSIZE ); // Create a window for display.
    imshow( "Display window", image ); // Show our image inside it.

    waitKey(0); // Wait for a keystroke in the window
    return 0;
}

Before this code will work, you will need to enter the libraries needed. Right click on your project name and click on "Build Options". Under the "Linker Settings" tab, add these libraries:

libopencv_calib3d300.a libopencv_flann300.a libopencv_ml300.a libopencv_objdetect300.a libopencv_features2d300.a libopencv_video300.a libopencv_photo300.a libopencv_imgcodecs300.a libopencv_imgproc300.a libopencv_core300.a libopencv_hal300.a libopencv_shape300.a libopencv_stitching300.a libopencv_superres300.a libopencv_videostab300.a liblibtiff.a liblibpng.a libIlmImf.a liblibjasper.a liblibjpeg.a liblibwebp.a libzlib.a libopencv_highgui300.a

(It may be advantageous, if using CodeBlocks, to define a symbol called OCV_Vers set to 300, and then define the libraries as "libopencv_highgui$(#OCV_Vers).a", which would allow for quickly switching between versions)

In the "Other linker options", add -mwindows (this tells the linker to link in Windows necessities like GDI)

In the "Search directories" tab, under compiler, you'll need to add:

OPEN_CV_DIRECTORY\opencv\build\include

In the "Search directories" tab, under linker, you'll need to add:

OPEN_CV_DIRECTORY\dajac_build\lib OPEN_CV_DIRECTORY\dajac_build\3rdparty\lib

Where OPEN_CV_DIRECTORY is replaced by the actual base directory where opencv is located.

Apparently, there is a known bug with gcc that affects OpenCV. Go to the "Compiler Settings" tab, and under #defines, add:

_GLIBCXX_ATOMIC_BUILTINS

Then, under the compiler flags sub-tab, make sure that the latest Intel instruction set, which for me was: "Intel Core2 (...) [-march=core2] " is checked. This clears up an issue with some atomic operations that OpenCV uses for efficiency.

Open CMake, and point it to the OpenCV sources directory.

Create a new folder under OpenCV called my_build.

Point CMake's "Where to build the binaries" field to the my_build folder.

Hit Configure. CMake will ask you about the compiler. If you want the CodeBlocks build, you should specify that it's MinGW for CodeBlocks, because OpenCV 's cmake file will output a CodeBlocks file that will help with compiling. You make need to specify the native compiler to match the compiler used for your project.

Once the Configure process has finished, everything should be in red. This does not indicate an error. CMake needs you to verify the configuration data.

1 - Find the "With" drop-down, and uncheck WITH_IPP (and IPP_A, if it is checked). These only work under Visual Studios. 2 - Under "Build", check BUILD_SHARED_LIBS for dlls, uncheck for .a 3 - Check under the "CMake" tab to make sure CMake hasn't changed any of your compiler options, as it often does. It changed 2 of mine. Check: CMAKE_AR, CMAKE_LINKER, CMAKE_MAKE_PROGRAM, NM, OBJCOPY, OBJDUMP, RANLIB, RC_COMPILER, STRIP

Click "Configure" again.

Any time you click configure, you should check all of the above options because CMake sometimes loses track of your settings. Once you have nothing else in red, and every option is correct, click Generate.

Navigate to the my_build directory under the OpenCV folder. You should see an OpenCV.cbp. Open it in CodeBlocks.

Configure CodeBlocks to use the same compiler that was specified in CMake.

Compile.

You will most likely encounter 4 errors in 'opencv/sources/modules/videoio/src/cap_dshow.cpp' pertaining to ISampleGrabberCB, ISampleGrabber, IEnumPIDMap, and IMPEG2PIDMap. Each of these interfaces needs a virtual destructor added to them (for example, in IEnumPIDMap, add virtual ~IEnumPIDMap(){} ). Adding these fixes the issues.

Hit compile. Sit back and wait.

If you are using TDM as the compiler, you'll most likely encounter ANOTHER issue with libstdc++ claiming a whole mess of things are redefined/have multiple definitions. If this happens, go to your my_build directory in Cygwin, and run the following command:

grep -rl 'lstdc' ./ | xargs sed -i 's/lstdc/static-libstdc/g'

The command replaces a bunch of dynamic linking instances with statically linked instances of libstdc.

Hit compile to start compiling from where you left off.

Testing

Once the compile is done, in the my_build libraries, you will have a /lib/ with the .a's, and a /bin/ with the dlls

Create a new console project in CodeBlocks, and use the following code:

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main( int argc, char** argv )
{
    if( argc != 2)
    {
     cout <<" Usage: display_image ImageToLoadAndDisplay" << endl;
     return -1;
    }

    Mat image;
    image = imread(argv[1], IMREAD_COLOR); // Read the file

    if(! image.data ) // Check for invalid input
    {
        cout << "Could not open or find the image" << std::endl ;
        return -1;
    }

    namedWindow( "Display window", WINDOW_AUTOSIZE ); // Create a window for display.
    imshow( "Display window", image ); // Show our image inside it.

    waitKey(0); // Wait for a keystroke in the window
    return 0;
}

Before this code will work, you will need to enter the libraries needed. Right click on your project name and click on "Build Options". Under the "Linker Settings" tab, add these libraries:

libopencv_calib3d300.a
libopencv_flann300.a
libopencv_ml300.a
libopencv_objdetect300.a
libopencv_features2d300.a
libopencv_video300.a
libopencv_photo300.a
libopencv_imgcodecs300.a
libopencv_imgproc300.a
libopencv_core300.a
libopencv_hal300.a
libopencv_shape300.a
libopencv_stitching300.a
libopencv_superres300.a
libopencv_videostab300.a
liblibtiff.a
liblibpng.a
libIlmImf.a
liblibjasper.a
liblibjpeg.a
liblibwebp.a
libzlib.a
libopencv_highgui300.a

libopencv_highgui300.a

(It may be advantageous, if using CodeBlocks, to define a symbol called OCV_Vers set to 300, and then define the libraries as "libopencv_highgui$(#OCV_Vers).a", which would allow for quickly switching between versions)

In the "Other linker options", add -mwindows (this tells the linker to link in Windows necessities like GDI)

In the "Search directories" tab, under compiler, you'll need to add:

OPEN_CV_DIRECTORY\opencv\build\include

In the "Search directories" tab, under linker, you'll need to add:

OPEN_CV_DIRECTORY\dajac_build\lib OPEN_CV_DIRECTORY\dajac_build\3rdparty\lib

Where OPEN_CV_DIRECTORY is replaced by the actual base directory where opencv is located.

Apparently, there is a known bug with gcc that affects OpenCV. Go to the "Compiler Settings" tab, and under #defines, add:

_GLIBCXX_ATOMIC_BUILTINS

Then, under the compiler flags sub-tab, make sure that the latest Intel instruction set, which for me was: "Intel Core2 (...) [-march=core2] " is checked. This clears up an issue with some atomic operations that OpenCV uses for efficiency.