Connecting Tech Pros Worldwide Help | Site Map

Problem with multiple definition

Antonio Rivas
Guest
 
Posts: n/a
#1: Feb 1 '08
Hello all.
I've got a problem of multiple definition in a program that at first
glance looks correct (I won't type the whole code, just the relevant one
and as examples since seems is a linkage problem):

main.cpp
========
using namespace std;
....

#include "myHeader.h" // This is the header that causes the problem

....

int main()
{
...

Function_in_myHeader();

...
}

myHeader.h
==========
#ifndef MYHEADER_H
#define MYHEADER_H

....

void Function_in_myHeader(void);

#endif

myHeader.cpp
============
....

#include "myHeader.h" // I suspect the problem is here

....

void Function_in_myHeader(void)
{
- do something -
}

As I said, this structure arises a multiple definition error of
Function_in_myHeader (also of other identifiers declared in myHeader.h)
The problem is that I need to include myHeader.h in myHeader.cpp because
when I remove it the compiler throws errors of undeclared identifiers.
I tried also compile the program embedding the line within #ifndef
#define #endif but the result is the same as if I remove the line: not
declared identifiers/functions errors arise.

I have no clue about what is wrong.
Btw, I use gcc with CodeBlocks frontend and the program is compiled as C++

Thank you in advanced.
[rob desbois]
Guest
 
Posts: n/a
#2: Feb 1 '08

re: Problem with multiple definition


As I said, this structure arises a multiple definition error of
Quote:
Function_in_myHeader (also of other identifiers declared in myHeader.h)
The problem is that I need to include myHeader.h in myHeader.cpp because
when I remove it the compiler throws errors of undeclared identifiers.
I tried also compile the program embedding the line within #ifndef
#define #endif but the result is the same as if I remove the line: not
declared identifiers/functions errors arise.
Are you compiling myHeader.cpp into myHeader.o then linking that with
main.cpp?

If you have implemented (i.e. provided a function body for)
function_in_myHeader IN the header itself then the function will be
defined in myHeader.o and you will get a multiple definition error
when you then link that with main.cpp
Any help? If not can you post the contents of myHeader.h and the
makefile/commands you use to compile it.

--rob
tragomaskhalos
Guest
 
Posts: n/a
#3: Feb 1 '08

re: Problem with multiple definition


On Feb 1, 11:34*am, Antonio Rivas <cha...@telefonica.netwrote:
Quote:
Hello all.
>
main.cpp
========
using namespace std;
...
#include "myHeader.h" // This is the header that causes the problem
...
An unrelated point - you want to get into the habit
of putting your "using namespace ..."s *after* your
#includes; you may save yourself hours of head-scratching
one day.

Antonio Rivas
Guest
 
Posts: n/a
#4: Feb 1 '08

re: Problem with multiple definition


tragomaskhalos escribió:
Quote:
On Feb 1, 11:34 am, Antonio Rivas <cha...@telefonica.netwrote:
Quote:
>Hello all.
>>
>main.cpp
>========
>using namespace std;
>...
>#include "myHeader.h" // This is the header that causes the problem
>...
>
An unrelated point - you want to get into the habit
of putting your "using namespace ..."s *after* your
#includes; you may save yourself hours of head-scratching
one day.
>
I know, sorry, in the source code is correctly placed after not before :)
But thanx for the reminder :)
Closed Thread