All of lore.kernel.org
 help / color / mirror / Atom feed
* Multiple declaration problem
@ 2007-02-24 15:49 Shriramana Sharma
  2007-02-27 18:42 ` Glynn Clements
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Shriramana Sharma @ 2007-02-24 15:49 UTC (permalink / raw)
  To: linux-c-programming

[-- Attachment #1: Type: text/plain, Size: 1689 bytes --]

Hello. I have a problem with multiple declaration in a project I am 
working on. I have constructed a similar testcase which throws the same 
kind of error. Please see the files in the attachment. -g or -g3 did not 
give any useful debugging symbols, I don't know why. The following is 
the session transcript:

$ ls
main.cpp  myheader.cpp  myheader.h
$ g++ -c main.cpp
$ g++ -c myheader.cpp
$ g++ -o main main.o myheader.o
myheader.o:(.data+0x0): multiple definition of `b'
main.o:(.data+0x0): first defined here
collect2: ld returned 1 exit status
$ mv main.cpp main.c
$ mv myheader.cpp myheader.c
$ rm *.o
$ gcc -c main.c
$ gcc -c myheader.c
$ gcc -o main main.o myheader.o
myheader.o:(.rodata+0x0): multiple definition of `a'
main.o:(.rodata+0x0): first defined here
myheader.o:(.data+0x0): multiple definition of `b'
main.o:(.data+0x0): first defined here
collect2: ld returned 1 exit status

In my project, I need to put the following statement in a header file:

int lawCurrentEngine = NONE ; // NONE has been enum-med previously

because I need to declare a function

int lawCloseEngine ( int engineID = lawCurrentEngine ) ;

I include the header file containing these two lines in two cpp files, 
and I get a multiple definition error for lawCurrentEngine just like in 
the given test case. If I push the lawCurrentEngine declaration to one 
of the cpp-s (it's not needed in the other cpp) I am unable to provide 
the default argument for the lawCloseEngine which can be done only in 
the header.

I don't understand how I am getting such an error when I have used the 
#ifndef #define #endif technique as per good programming practice.

Please help,

Thanks.

Shriramana Sharma.

[-- Attachment #2: multiple-declaration-problem.tar.gz --]
[-- Type: application/gzip, Size: 611 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Multiple declaration problem
  2007-02-24 15:49 Multiple declaration problem Shriramana Sharma
@ 2007-02-27 18:42 ` Glynn Clements
  2007-02-27 18:57 ` Jesse Ruffin
  2007-02-28 15:31 ` Shriramana Sharma
  2 siblings, 0 replies; 6+ messages in thread
From: Glynn Clements @ 2007-02-27 18:42 UTC (permalink / raw)
  To: Shriramana Sharma; +Cc: linux-c-programming


Shriramana Sharma wrote:

> Hello. I have a problem with multiple declaration in a project I am 
> working on. I have constructed a similar testcase which throws the same 
> kind of error. Please see the files in the attachment. -g or -g3 did not 
> give any useful debugging symbols, I don't know why. The following is 
> the session transcript:
> 
> $ ls
> main.cpp  myheader.cpp  myheader.h
> $ g++ -c main.cpp
> $ g++ -c myheader.cpp
> $ g++ -o main main.o myheader.o
> myheader.o:(.data+0x0): multiple definition of `b'
> main.o:(.data+0x0): first defined here
> collect2: ld returned 1 exit status
> $ mv main.cpp main.c
> $ mv myheader.cpp myheader.c
> $ rm *.o
> $ gcc -c main.c
> $ gcc -c myheader.c
> $ gcc -o main main.o myheader.o
> myheader.o:(.rodata+0x0): multiple definition of `a'
> main.o:(.rodata+0x0): first defined here
> myheader.o:(.data+0x0): multiple definition of `b'
> main.o:(.data+0x0): first defined here
> collect2: ld returned 1 exit status
> 
> In my project, I need to put the following statement in a header file:
> 
> int lawCurrentEngine = NONE ; // NONE has been enum-med previously
> 
> because I need to declare a function
> 
> int lawCloseEngine ( int engineID = lawCurrentEngine ) ;
> 
> I include the header file containing these two lines in two cpp files, 
> and I get a multiple definition error for lawCurrentEngine just like in 
> the given test case. If I push the lawCurrentEngine declaration to one 
> of the cpp-s (it's not needed in the other cpp) I am unable to provide 
> the default argument for the lawCloseEngine which can be done only in 
> the header.

Default parameter values need to be constants; if you declare
lawCloseEngine as "const int", you won't get an error.

If you want the default value to be a variable rather than a constant,
you need to use something like:

	int lawCloseEngine ( int engineID = -1 ) ;


	int lawCloseEngine ( int engineID )
	{
		if (engineID == -1)
			engineID = lawCurrentEngine ;
		...
	}

Alternatively, define separate zero-arguments and one-argument
versions of lawCloseEngine.

-- 
Glynn Clements <glynn@gclements.plus.com>

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Multiple declaration problem
  2007-02-24 15:49 Multiple declaration problem Shriramana Sharma
  2007-02-27 18:42 ` Glynn Clements
@ 2007-02-27 18:57 ` Jesse Ruffin
  2007-02-28 15:31 ` Shriramana Sharma
  2 siblings, 0 replies; 6+ messages in thread
From: Jesse Ruffin @ 2007-02-27 18:57 UTC (permalink / raw)
  To: linux-c-programming

[-- Attachment #1: Type: text/plain, Size: 3283 bytes --]

The problem, as I understand it, is that you are instantiating a variable in a 
header and you need it's value in another header.


It seems that the best solution would instead be to move the variable 
instantiation into the source file, keep the declaration in the header if you 
still need visibility. To synchronize the value of the variable and the 
function, you should use a #define or const. Using a variable will not give 
you a dynamic default anyway, as it is evaluated at compile time. Try 
something like this:

in the header:
#define DEFAULT_ENGINE = NONE //could use const int CURRENT_ENGINE
int lawCurrentEngine; //declaration only, if you need it

in source:
lawCurrentEngine = DEFAULT_ENGINE; // NONE has been enum-med previously

in other header:
int lawCloseEngine ( int engineID = DEFAULT_ENGINE );

Now that I've written this, it looks like you may be trying to get a dynamic 
default. You can't actually do that, but you can get close with a file static 
variable. i.e.:

in source:

static currentEngine = NONE;

int lawCloseEngine( int engineID = NONE ) {
	if( engineID == NONE ){
		if (currentEngine != NONE) lawRealCloseEngine(currentEngine);
		currentEngine = NONE;
		return SUCCESS;
	} else {
		lawRealCloseEngine(engineID);
		return SUCCESS;
	}
	return FAIL;	//Should never happen.
}

Hope this helps

Jesse Ruffin
AJP Services

On Saturday 24 February 2007 10:49, Shriramana Sharma wrote:
> Hello. I have a problem with multiple declaration in a project I am
> working on. I have constructed a similar testcase which throws the same
> kind of error. Please see the files in the attachment. -g or -g3 did not
> give any useful debugging symbols, I don't know why. The following is
> the session transcript:
>
> $ ls
> main.cpp  myheader.cpp  myheader.h
> $ g++ -c main.cpp
> $ g++ -c myheader.cpp
> $ g++ -o main main.o myheader.o
> myheader.o:(.data+0x0): multiple definition of `b'
> main.o:(.data+0x0): first defined here
> collect2: ld returned 1 exit status
> $ mv main.cpp main.c
> $ mv myheader.cpp myheader.c
> $ rm *.o
> $ gcc -c main.c
> $ gcc -c myheader.c
> $ gcc -o main main.o myheader.o
> myheader.o:(.rodata+0x0): multiple definition of `a'
> main.o:(.rodata+0x0): first defined here
> myheader.o:(.data+0x0): multiple definition of `b'
> main.o:(.data+0x0): first defined here
> collect2: ld returned 1 exit status
>
> In my project, I need to put the following statement in a header file:
>
> int lawCurrentEngine = NONE ; // NONE has been enum-med previously
>
> because I need to declare a function
>
> int lawCloseEngine ( int engineID = lawCurrentEngine ) ;
>
> I include the header file containing these two lines in two cpp files,
> and I get a multiple definition error for lawCurrentEngine just like in
> the given test case. If I push the lawCurrentEngine declaration to one
> of the cpp-s (it's not needed in the other cpp) I am unable to provide
> the default argument for the lawCloseEngine which can be done only in
> the header.
>
> I don't understand how I am getting such an error when I have used the
> #ifndef #define #endif technique as per good programming practice.
>
> Please help,
>
> Thanks.
>
> Shriramana Sharma.

[-- Attachment #2: Type: application/pgp-signature, Size: 189 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Multiple declaration problem
  2007-02-24 15:49 Multiple declaration problem Shriramana Sharma
  2007-02-27 18:42 ` Glynn Clements
  2007-02-27 18:57 ` Jesse Ruffin
@ 2007-02-28 15:31 ` Shriramana Sharma
  2007-03-07 16:36   ` Glynn Clements
  2 siblings, 1 reply; 6+ messages in thread
From: Shriramana Sharma @ 2007-02-28 15:31 UTC (permalink / raw)
  To: linux-c-programming

[-- Attachment #1: Type: text/plain, Size: 448 bytes --]

Thanks to all who replied.

I found a different solution, which is by declaring the variables 
static. Find attached solutions.

But still the C parser (or what is it?) did throw up an error with the
const variable, which C++ did not.

@Andre: Is there any I should not declare a variable in a header file if
I want that variable to be visible to many cpp files? I can avoid
writing many "extern" lines by using static.

Thanks again,

Shriramana.

[-- Attachment #2: multiple-declaration-problem-solutions.tar.gz --]
[-- Type: application/gzip, Size: 704 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Multiple declaration problem
  2007-02-28 15:31 ` Shriramana Sharma
@ 2007-03-07 16:36   ` Glynn Clements
  0 siblings, 0 replies; 6+ messages in thread
From: Glynn Clements @ 2007-03-07 16:36 UTC (permalink / raw)
  To: Shriramana Sharma; +Cc: linux-c-programming


Shriramana Sharma wrote:

> I found a different solution, which is by declaring the variables 
> static. Find attached solutions.
> 
> But still the C parser (or what is it?) did throw up an error with the
> const variable, which C++ did not.

In C, "const" is only valid for pointer and array targets, not for
primitive types or structures.

> @Andre: Is there any I should not declare a variable in a header file if
> I want that variable to be visible to many cpp files? I can avoid
> writing many "extern" lines by using static.

Presumably, you mean "define a variable"; a declaration (e.g.
"extern int foo") merely tells the compiler that the variable exists,
and its type, while a definition actually creates the variable.

If you define a variable in a header file, each object file will get a
separate copy of the variable. All of the variables will have the same
name, but they will be different variables; modifying one won't affect
any of the others.

This is fine for a constant (unless it occupies a lot of memory, in
which case you don't want to include a separate copy in each object
file), but not for a variable.

-- 
Glynn Clements <glynn@gclements.plus.com>

^ permalink raw reply	[flat|nested] 6+ messages in thread

* RE: Multiple declaration problem
@ 2007-02-27 10:50 cyon.john
  0 siblings, 0 replies; 6+ messages in thread
From: cyon.john @ 2007-02-27 10:50 UTC (permalink / raw)
  To: samjnaa, linux-c-programming

Hi,

#ifdef ... #define ... #endif protects you from accidently incuding a
header file twice in a source file.
But since you are including it in different source files, compiler will
not complain, but while linking linker will find that 'b' is defined at
two places and complain.

Can't you use  
		int lawCloseEngine ( int engineID = NONE ) ;

I think that should solve your problem.

Regards,
Cyon P.J.


-----Original Message-----
From: linux-c-programming-owner@vger.kernel.org
[mailto:linux-c-programming-owner@vger.kernel.org] On Behalf Of
Shriramana Sharma
Sent: Saturday, February 24, 2007 9:19 PM
To: linux-c-programming@vger.kernel.org
Subject: Multiple declaration problem

Hello. I have a problem with multiple declaration in a project I am
working on. I have constructed a similar testcase which throws the same
kind of error. Please see the files in the attachment. -g or -g3 did not
give any useful debugging symbols, I don't know why. The following is
the session transcript:

$ ls
main.cpp  myheader.cpp  myheader.h
$ g++ -c main.cpp
$ g++ -c myheader.cpp
$ g++ -o main main.o myheader.o
myheader.o:(.data+0x0): multiple definition of `b'
main.o:(.data+0x0): first defined here
collect2: ld returned 1 exit status
$ mv main.cpp main.c
$ mv myheader.cpp myheader.c
$ rm *.o
$ gcc -c main.c
$ gcc -c myheader.c
$ gcc -o main main.o myheader.o
myheader.o:(.rodata+0x0): multiple definition of `a'
main.o:(.rodata+0x0): first defined here
myheader.o:(.data+0x0): multiple definition of `b'
main.o:(.data+0x0): first defined here
collect2: ld returned 1 exit status

In my project, I need to put the following statement in a header file:

int lawCurrentEngine = NONE ; // NONE has been enum-med previously

because I need to declare a function

int lawCloseEngine ( int engineID = lawCurrentEngine ) ;

I include the header file containing these two lines in two cpp files,
and I get a multiple definition error for lawCurrentEngine just like in
the given test case. If I push the lawCurrentEngine declaration to one
of the cpp-s (it's not needed in the other cpp) I am unable to provide
the default argument for the lawCloseEngine which can be done only in
the header.

I don't understand how I am getting such an error when I have used the
#ifndef #define #endif technique as per good programming practice.

Please help,

Thanks.

Shriramana Sharma.

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2007-03-07 16:36 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-02-24 15:49 Multiple declaration problem Shriramana Sharma
2007-02-27 18:42 ` Glynn Clements
2007-02-27 18:57 ` Jesse Ruffin
2007-02-28 15:31 ` Shriramana Sharma
2007-03-07 16:36   ` Glynn Clements
2007-02-27 10:50 cyon.john

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.