Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

multiple definition error including c++ header file with inline code from multiple sources

Tags:

c++

I have a c++ header file containing a class. I want to use this class in several projects, bu I don't want to create a separate library for it, so I'm putting both methods declarations and definitions in the header file:

// example.h #ifndef EXAMPLE_H_ #define EXAMPLE_H_ namespace test_ns{  class TestClass{ public:     void testMethod(); };  void TestClass::testMethod(){     // some code here... }  } // end namespace test_ns #endif 

If inside the same project I include this header from more than one cpp file, I get an error saying "multiple definition of test_ns::TestClass::testMethod()", while if I put the method definition inside the class body this does not happen:

// example.h #ifndef EXAMPLE_H_ #define EXAMPLE_H_ namespace test_ns{  class TestClass{ public:     void testMethod(){         // some code here...     } };  } // end namespace test_ns #endif 

Since the class is defined inside a namespace, shouldn't the two forms be equivalent? Why is the method considered to be defined twice in the first case?

like image 976
Paolo Tedesco Avatar asked Oct 17 '08 12:10

Paolo Tedesco


People also ask

How do you prevent the same header file being included multiple times in C?

To avoid multiple inclusions of the same header file we use the #ifndef, #define and #endif preprocessor directives. Just write the entire program in only file and include headers once only. You can use the conditional preprocessor directive named #ifndef to check whether that symbolic name has already been assigned.

What happens if we include a header file twice in C?

If a header file happens to be included twice, the compiler will process its contents twice. This is very likely to cause an error, e.g. when the compiler sees the same structure definition twice. Even if it does not, it will certainly waste time. This construct is commonly known as a wrapper #ifndef.

Can you have multiple header files in C?

The output of the above program. Including Multiple Header Files: You can use various header files in a program.

Can you have multiple header files?

Yes, you can use #include<bits/stdc++. h> This header file includes all the standard header files. The only problem is, it would include a lot of unnecessary files which you don't require for a program, and also, since it includes a lot of header files your compilation time will increase.


1 Answers

These are not equivalent. The second example given has an implicit 'inline' modifier on the method and so the compiler will reconcile multiple definitions itself (most likely with internal linkage of the method if it isn't inlineable).

The first example isn't inline and so if this header is included in multiple translation units then you will have multiple definitions and linker errors.

Also, headers should really always be guarded to prevent multiple definition errors in the same translation unit. That should convert your header to:

#ifndef EXAMPLE_H #define EXAMPLE_H  //define your class here  #endif 
like image 82
workmad3 Avatar answered Sep 30 '22 09:09

workmad3