#include issues in testApp.h

Hi,

This is my first post here.

I’ve been trying to write a OF app, and got stuck. I don’t know if this is OF specific or general C++, but here goes:

Whenever I try to #include my own header files in testApp.h, in order to be able to declare variables, I get linking errors.
Most errors look something like that:

  
testApp.obj : error LNK2005: "void __cdecl My::FindTip(unsigned char *,int,int,int *,int *,unsigned char *)" (?FindTip@My@@YAXPAEHHPAH10@Z) already defined in main.obj  
  

And there is also one like that:

  
bin\emptyExample_debug.exe : fatal error LNK1169: one or more multiply defined symbols found  
  

I’m using VC++ Express 2008.

Can anyone give me any hints?

Thanks.

it looks as if you are including code twice – here, the compilation of the app is fine, but in linking, it sees that function defined multiple times, so it doesn’t know which to connect to the app.

it seems that either you have the cpp file in the project multiple times, or you need an include guard on your h file so that it isn’t included twice ?

include guard would look like

  
  
#ifndef MY_FILE_NAME  
#define MY_FILE_NAME  
  
(all the code from the h file)  
  
#endif  
  

can you check if either of these things are the case?

thanks!
zach

Both checked before posting. If I #include more than once in other files the guard works, but when I #include in testApp.h it always gives this bug.

Thanks,
Tamir.

hmmm. I guess then you’ll need to post up a zip that shows the problem for us to debug it.

take care,
zach

I can attach a zip, but it is basically anything I include in the testApp.h file (I’ve copied the emptyExample folder).

Thanks.

in order to give you advice, we need to see the errors or at least the setup – so, uploading would make sense, or a screenshot, or something…

thanks,
zach

I simply took the emptyExample from the examples folder, and modified it a tiny bit (can be seen in the attached screenshot). The file head.h is in the src subfolder, and looks like that:

  
#ifndef _MY_HEAD  
#define _MY_HEAD  
  
namespace My  
{  
	int func()  
	{  
		return 5;  
	};  
};  
#endif  

That’s about it.
Anything I’m doing wrong to justify the error?

Thanks,
Tamir.

you can’t define a function in a .h file unless you mark it as inline:

  
  
#ifndef _MY_HEAD  
#define _MY_HEAD  
  
namespace My  
{  
   inline int func()  
   {  
      return 5;  
   };  
};  
#endif  

even better if you just declare it in the .h and then define it in a .cpp:

func.h:

  
  
#ifndef _MY_HEAD  
#define _MY_HEAD  
  
namespace My  
{  
   int func();  
};  
#endif  

func.cpp:

  
  
#include "func.h"  
  
namespace My  
{  
   int func()  
   {  
      return 5;  
   };  
};  
  

if not it’s going to be defined several times in every file that includes func.h even indirectly. in your case it’s being defined in testApp.h and then in main.cpp as it’s including testApp.h that is including func.h

Thanks a lot!

BTW, after writing the head.cpp file, all I need to do is include head.h and the functions will work as defined in head.cpp?

Thanks,
Tamir.

yes, the definition of functions can be done wherever you want as soon as there’s only one and you just need to include a .h with the declaration to use it. usually you will have a .h and a .cpp with the same name and functions / classes, but it’s not mandatory.