2

I have C++ project which consists of multiple (in fact many) .cpp and .h files. While writing header files i have a file as follows

For eg MyHeaderFile.h

#ifndef _MYHEARDERFILE_H #define _MYHEARDERFILE_H // Here i have function defintion. void myFunc() {cout << "my function" << endl; } #endif 

Above file is included in multiple files. While compiling i have getting "multiple definition of "myfunc" error. I am expecting the header is included only once as i have #ifndef check so i am expecting error should not be thrown.

For example in case of templates we have to define in header file, in this case how we can avoid the problem i am facing now?

Can any one please help me why i am seeing the error? is my understanding right?

Thanks!

2
  • Strictly speaking, you get that error not during compiling, but during linking. This is a separate phase that has nothing to do with compiling. You could even be linking units written in different languages compiled with different compilers, although that would require some degree of white magic. Commented Feb 12, 2011 at 10:43
  • 1
    Tangential point: your header-guard macros should probably be MYHEADERFILE_H_, because names with leading underscores are reserved by the implementation. Commented Feb 12, 2011 at 10:58

2 Answers 2

7

Normally, one puts function declarations in header files, and function definitions (i.e. the actual function body/implementation) in source files. Otherwise, you get exactly the issue you're seeing: the header file is #included (i.e. substituted) into multiple source files, so the function ends up being defined multiple times. This confuses the linker, so it complains.

The header-guard #ifndef ... stuff only prevents a header from being substituted into the same source file multiple times. Remember that each source file is compiled completely independently from all the others; all #defines and other definitions are "reset" for each one.

One possible alternative solution is to leave the function definition in the header file, but simply to mark it inline (this has the side-effect of eliminating the linker error). But, personally, I wouldn't do that.

Sign up to request clarification or add additional context in comments.

Comments

0

Every function must be defined only once in a single translation unit (this is called One definition rule, and applies not only to functions). By placing the definition in a header which is then included, you are violating this rule.

You can simply leave the declaration

void myFunc(); 

in the header, and define myFunc in a .cpp which provides the definition. Otherwise you can declare your function inline.

Note that when using templates, instead, you are usually led (for template-related specific issues) to place definitions directly in the headers, which could appear surprising given the problems you are facing now.

3 Comments

how to avoid this in case of template?
No, the problem is not that it's being defined multiple times in a single translation unit, it's that it's being defined in multiple translation units. This is a linker problem, not a compiler problem.
@Oli: true, thanks for clarifying my answer. I have been too quick in my reply.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.