Repeated Multiple Definition Errors from Including Same Header in Multiple Cpps

Multple c++ files causes multiple definition error?

You have two options to solve this multiple definition problem: Mark the method inline, or put the definition in a .cpp file.

1) Mark the method inline:

// Foo.h

inline bool foo(int i) { return i = 42; }

2) Put the definition in a .cpp file:

// Foo.h

inline bool foo(int i); // declaration

// Foo.cpp
bool foo(int i) { return i = 42; } // definition

Whether the method is actually inlined by the compiler in the first case is irrelevant here: inline allows you to define a non-member function in a header file without breaking the one definition rule.

Strange multiple definitions error with headers

The problem is in the header - you need:

#ifndef IP_HPP_INCLUDED
#define IP_HPP_INCLUDED

extern unsigned char LUTColor[2]; // Declare the variable

#endif // IP_HPP_INCLUDED
  • Do not define variables in headers!

You also need to nominate a source file to define LUTColor (IP.cpp is the obvious place).

See also: What are extern variables in C, most of which applies to C++ as well as C.

Multiple definition error while linking two object files with same definition

You need to make sure the person variable is only defined once. To do that, you need to only declare that variable in foo.h:

extern human *person;

Then, in foo.cpp, you define it:

human *person = NULL;

You then include foo.o in the object files that make up game.so.

Linker does not point out errors; multiple definition warnings pointed to the same line

Most likely you are including a header which implements methods non-inline in multiple translation units. The Makefile has nothing to with it. You'll need to find the definition of the methods and see how they end up being included into multiple files. If they are actually in a header file the easiest fix is probably to make them all inline.

The compiler doesn't see that you are including the header into multiple translation units as it always only processes one at a time. When the linker sees the various object files, it just see many definitions of the same thing and complains. I would have thought that the linker pointer at the location of the definition, though.

Multiple definition of several functions in two different .o files.

It might be because you header file is included multiple times. What you can do is define guards like this:

#ifndef SOMEVAR - *make sure the file is included only once in the current scope*
#define SOMEVAR
//Symbol definitions
#endif

or you could include #pragma once in your header file, if your compiler supports it.



Related Topics



Leave a reply



Submit