0

The simple C++ code I wrote below got compile error
"undefined reference to 'featureExtract()'" (using codeblocks under windows),

in FeaureExtract.h

#ifndef FEATUREEXTRACT_H_INCLUDED
#define FEATUREEXTRACT_H_INCLUDED

extern void featureExtract();

#endif // FEATUREEXTRACT_H_INCLUDED

in FeatureExtract.cpp

#include "FeatureExtract.h"
void featureExtract()
{
    some code
}

in main.cpp

#include "FeatureExtract.h"
int main()
{
    featureExtract();
}

I have searched the SO using keyword "C++ undefined reference to" and read tens of entries (many are to class or under Linux), but I could not get my own problem addressed (I don't know why).
Thanks for any hint~

Before solving this problem, I have no choice but put all the staff in FeatureExtract.h, it works, but it seems not good. This post ( Why have header files and .cpp files in C++? ) explains the main merit of .h files is "separating the interface from the implementation", I wonder if putting all interface and implementation in .h files will have other effects?

Thank you~

Community
  • 1
  • 1
vancexu
  • 1,548
  • 3
  • 19
  • 30

2 Answers2

1

It looks like you aren't linking both object files together into your final binary. Unfortunately I'm not familiar with codeblocks, but make sure all the .cpp files are added to your project.

Another less likely possibility is that somehow the name decoration is different between the two cpp files. Make sure you're using C++ to compile both files and that one doesn't compile as C code for example.

Mark B
  • 95,107
  • 10
  • 109
  • 188
  • Thank you all, @Mark B @Zeta @Frobzig, I solved it. I remove the `.h` and `.cpp` from the project and then add them back to the project, no error anymore. I think it is because I once changed the `.h` file name and then changed the name back, something invisible happened. – vancexu Aug 01 '12 at 02:14
0

The code you posted looks fine. As Zeta said, are all files in the same project?

Frobzig
  • 324
  • 1
  • 10