0

I wrote a piece of software in C++ OpenCV, that's so structured:

  • main.cpp
  • testfps.cpp
  • testfps.hpp

The problem is that I get the these two errors

undefined reference to "myTestfps1(int, int)"
undefined reference to "myTestfps2(int, int)"

These two method are written in testfps.cpp and declared in testfps.hpp.

In main.cpp are declared all the necessary #include <name>, and after them there is #include "testfps.hpp"


main.cpp

    #include <stdio.h>
    #include <stdlib.h>
    #include <iostream>

    #include "testfps.hpp"

int main(int argc, char** argv[]){
    ....
    switch(c){
    case 1: myTestfps1(a,b);break;
    case 2: myTestfps2(a,b);break;
    }
...
}

testfps.cpp

#include <opencv2/opencv.hpp>
#include <time.h>
#include <stdio.h>

#include "testfps.hpp"

int myTestfps1(int a, int b){
...
}
int myTestfps2(int a, int b){
...
}

testfps.hpp

#ifndef TESTFPS_HPP
#define TESTFPS_HPP

int myTestfps1(int a, int b);
int myTestfps2(int a, int b);

#endif

What's wrong with this?

Mitro
  • 1,230
  • 8
  • 32
  • 61

1 Answers1

4

The main issue you might be having, as Samuel pointed out, is that you're not including testfps.cpp in your compilation.

If you're running on linux with g++, try this:

g++ -o testfps main.cpp testfps.cpp

In any case, since you're working on C++ I recommend you don't use C headers such as stdio.h and stdlib.h. Instead use cstdio and cstdlib:

#include <cstdlib>
#include <cstdio>

Finally, your definition of main is wrong, argv is not a pointer3:

int main(int argc, char* argv[])

or:

int main(int argc, char** argv)
AnilM3
  • 261
  • 1
  • 9
  • OK, good! But now I get errors 'undefined reference to openCV methods'. Should I link libraries in CLI after g++ -o testfps main.cpp testfps.cpp ? – Mitro Jan 25 '15 at 13:18
  • 1
    Thank you I solved using also this answer http://stackoverflow.com/questions/5971206/codeblocks-how-to-compile-multiple-file-projects – Mitro Jan 25 '15 at 13:34