Saturday, August 6, 2016

How to Create a C/C++ Project in Eclipse CDT with Custom Makefile and Debug

Integrated Development Environment (IDE) enables to speed up development process for most of us. In this tutorial, I will go over how to create a C/C++ project in Eclipse and use custom Makefile for compilation.

First, we need to download Eclipse with CDT plugin from here and install. Launch the application when complete.

I will go over C++ project, but it should be very similar to C project. From the menu, select File --> New --> C++ Project. Enter the Project name, say OpenCVExampleProject, and under Project type, select Executable --> Empty Project. Under Toolchains, select the appropriate toolchain, such as Linux GCC. If the toolchain has been automatically detected by Eclipse, it will enable Finish button, but if not you will need to proceed and configure the toolchain by entering the prefix and path.

Next, create src directory by right-clicking OpenCVExampleProject in the navigation pane on the left, New --> Folder and enter Folder name as src. To add a source file, right-click just-created src folder, New --> Source File and enter, say OpenCVExample.cpp, and select Finish.

Now, let's add the following lines of codes into the OpenCVExample.cpp file:

#include <opencv2/opencv.hpp>
#include <cstdio>

using namespace cv;

int main( int argc, char** argv )
  Mat image;
  image = imread( argv[1], 1 );

  if( argc != 2 || ! )
      printf( "No image data \n" );
      return -1;

  namedWindow( "Display Image", CV_WINDOW_NORMAL | CV_WINDOW_KEEPRATIO );
  imshow( "Display Image", image );


  return 0;

Make sure to save the source file so that Eclipse can check errors in the code. If you haven't already installed opencv2 library in your system, do so now. For Ubuntu, simply run
$ sudo apt-get install -y libopencv-dev

Eclipse should not warn you of any errors you have followed well up to this point in the source file. Well, let's compile it and see what happens. 

Build the project by selecting Project --> Build All in the menu. You will see errors, similar to
Building target: OpenCVExampleProject
Invoking: GCC C++ Linker
g++  -o "OpenCVExampleProject"  ./src/OpenCVExample.o   
./src/OpenCVExample.o: In function `main':
/home/linuxnme/workspace/OpenCVExampleProject/Debug/../src/OpenCVExample.cpp:9: undefined reference to `cv::imread(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
makefile:45: recipe for target 'OpenCVExampleProject' failed
/home/linuxnme/workspace/OpenCVExampleProject/Debug/../src/OpenCVExample.cpp:17: undefined reference to `cv::namedWindow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
/home/linuxnme/workspace/OpenCVExampleProject/Debug/../src/OpenCVExample.cpp:18: undefined reference to `cv::_InputArray::_InputArray(cv::Mat const&)'
/home/linuxnme/workspace/OpenCVExampleProject/Debug/../src/OpenCVExample.cpp:18: undefined reference to `cv::imshow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::_InputArray const&)'
/home/linuxnme/workspace/OpenCVExampleProject/Debug/../src/OpenCVExample.cpp:20: undefined reference to `cv::waitKey(int)'
./src/OpenCVExample.o: In function `cv::Mat::~Mat()':
/usr/include/opencv2/core/mat.hpp:278: undefined reference to `cv::fastFree(void*)'
./src/OpenCVExample.o: In function `cv::Mat::operator=(cv::Mat const&)':
/usr/include/opencv2/core/mat.hpp:298: undefined reference to `cv::Mat::copySize(cv::Mat const&)'
./src/OpenCVExample.o: In function `cv::Mat::release()':
/usr/include/opencv2/core/mat.hpp:367: undefined reference to `cv::Mat::deallocate()'
collect2: error: ld returned 1 exit status
make: *** [OpenCVExampleProject] Error 1

23:14:46 Build Finished (took 843ms)

The errors simply tell you that opencv library has not been linked. Now, it is time to create our own Makefile and compile. Select Project --> Properties --> C/C++ Build and uncheck Generate Makefiles automatically under Makefile generation. Next, delete, and files under Debug folder in the project root directory. These files were generated automatically by Eclipse.

Edit Debug/makefile similar to below (ignore makefile edit warning):
g++ -g -o OpenCVExample ../src/OpenCVExample.cpp `pkg-config --libs opencv`

Make sure to save this file, and let's build it again. It should have successfully built the executable with following log:
23:22:53 **** Incremental Build of configuration Debug for project OpenCVExampleProject ****
make all 
g++ -g -o OpenCVExample ../src/OpenCVExample.cpp `pkg-config --libs opencv`

23:22:54 Build Finished (took 1s.35ms)

To test whether it build successfully, copy your favorite image file, say image.jpg into the project root directory. Select Run --> Profile Configuration --> New launch configuration --> Arguments and enter image.jpg

Finally, select Run --> Debug. Eclipse may show you a prompt asking for Confirm Perspective Switch for debugging purposes. Let's choose Yes. Eclipse will automatically break at the beginning of the main function. Press [F8] key to resume. You should be able to see your image window, which disappears after any key press. To go back to the previous perspective, simply click on C/C++ icon on the top-right corner.

No comments:

Post a Comment