Continuing the previous posts on How to Install OpenCV 2.3.1 in Ubuntu 11.10 Oneiric Ocelot with Python support and How to use CMake ith OpenCV now I'll show you how to use both OpenCV and CMake with Codeblocks IDE. Codeblocks is an open source, cross platform, free, great C++ IDE built to meet the most demanding needs of users. It is very extensible and fully configurable. I have been using codeblocks for about 2 years and I never felt the need to change to another IDE. It supports debugging and code completion which are very useful features.
Setting up CMake with OpenCV and Codeblocks is fairly simple and quick. First, open a terminal and run
sudo apt-get install cmake-qt-gui
This package will install CMake-gui which is a graphical interface to CMake. After installing the package run
cmake-gui
The CMake-gui program will load and you'll see a window with some input fields and some buttons. You'll have to perform the following steps:
- First click "Browse Source" and locate the folder where you have your sourcecode and "CMakeLists.txt" file.
- Then click "Browse Build" and specify the folder where you want to locate the files that result from the build process.
- Finally click Configure and when the popup shows up choose "Codeblocks - Unix Makefiles" and leave the "Use default native compilers" option selected.
- Click Finish, wait for the configuration to end and then click Generate, to generate the Makefile.
At this point if you browse the folder that you chose to build the project you'll see that you have a .cbp (Codeblocks Project) file. Open it with codeblocks and there you go.
There is only one more thing that you have to do. Click Project > Properties > Build targets and on the "Selected build target options" filed "Type" change from "Commands only" to "Console Application" or "GUI application". Otherwise when you try to build and run the project you will get the error "You can't 'run' a commands-only target...".
To use the code completion on Codeblocks just start writing your code and then press Ctrl+Space and the code suggestions will appear.
If you want to use debugging there is one change that you have to do to the file "CMakeLists.txt". In order to generate the debugging symbols you must open it in your favourite text editor and append the following line to the end of the file
SET(CMAKE_CXX_FLAGS "-g -Wall")
Your CMakeLists.txt should look like this (replace "project_name" with your project's name)
CMAKE_MINIMUM_REQUIRED(VERSION x.x.x) PROJECT( project_name ) FIND_PACKAGE( OpenCV REQUIRED ) ADD_EXECUTABLE( ${PROJECT_NAME} main.cpp ) TARGET_LINK_LIBRARIES( ${PROJECT_NAME} ${OpenCV_LIBS} ) ADD_CUSTOM_COMMAND(TARGET ${PROJECT_NAME} POST_BUILD COMMAND cp ${PROJECT_NAME} ${PROJECT_NAME}.o ) SET(CMAKE_CXX_FLAGS "-g -Wall")
Note: The 6th line of code is optional. Use it only if you want to perform any operation after the build process.
After performing this step and reloading the project in codeblocks it might be necessary to change the "Type" field from "Commands only" to "Console Application" or "GUI application". in the "Selected build target options" again.
To build and run, just press F9. To debug, just set your breakpoints and press F8.
Happy programming!
Did you find this post helpful? Do you wish to contribute to other projects regarding computer science, electronics, robotics or mechatronics that will be posted in this blog? If you wish to do so, you can donate via paypal using the button below. Thanks! :)
0 Comments: