C++ – Including source files from another C++ project in Eclipse CDT

ceclipseeclipse-cdt

I have a C++ project in Eclipse CDT which uses some functions from OpevCV and it compiles into an executable and works fine. Now I want to have a new C++ project which uses some of the functions defined in the first one. I can't get the two to compile and link together.

I have #include "..." in the relevant files in the new project, I've added the old project to the referenced projects of the new project and in the build settings of the new project, I've added the source folder of the old project as an include (-I gcc option).

I've attached a screenshot of my Eclipse in case it's helpful:

enter image description here

The error given by the compiler is:

Building target: OtherProject
Invoking: GCC C++ Linker
g++ -o "OtherProject" ./src/foo.o
./src/foo.o: In function `main':
/home/max/opencvjni/OtherProject/Debug/../src/foo.cpp:13: undefined reference to threshold()'
collect2: ld returned 1 exit status
make: *** [OtherProject] Error 1

Here is the code in those other files in the other tabs of the screenshot:

Threshold.cpp

#include <cv.h>
#include <highgui.h>
#include "Threshold.h"

using namespace cv;

int threshold(void) {
   Mat img = imread("/home/max/opencvjni/monalisa.jpg", 0);
   threshold(img, img, 127, 255, THRESH_BINARY);
   imwrite("/home/max/opencvjni/monathresh.jpg", img);
   return 0;
}

int main(void) {
  threshold();
  return 0;
}

Threshold.h

#ifndef THRESHOLD_H_
#define THRESHOLD_H_

int threshold(void);

#endif /* THRESHOLD_H_ */[/code]
  • Eclipse version: 3.7.2
  • CDT version: 8.0.2.201202111925
  • OS: Ubuntu 12.04

Thank you for looking at my post, if there is no way to do this, or it is just more sensible to do it another way, please don't hesitate to say so.

Regards

Best Answer

I was looking for an answer too but the previous answers did not solve the problem. (Nick's answer was helpful). You have to define 2 things:

  1. Go to Project->Properties->C/C++ general->Paths and Symbols. Select the Source Location tab and click the Link Folder..., Then select the folder you want to include.
  2. Go to Project->Properties->C/C++ general->Paths and Symbols. Select the Includes tab. Make sure you select the correct language on the left menu (for example: GNU C++) and click Add..., Then select the folder you want to include.

Note: I'm not sure it's necessary but when I created the external project I selected the "Shared Library" option in the new project wizard.

To make sure you did everything OK, go to : Project->Properties->C/C++ Build->Settings. Select the GCC C++ Linker and make sure that the All options contains "-L/your_folder_name". Repeat this for GCC C++ Compiler also.

Related Topic