Re: Multiple declaration problem

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxxxxxxxxxxxxxx>
-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Assembler]     [Git]     [Kernel List]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [C Programming]     [Yosemite Campsites]     [Yosemite News]     [GCC Help]

  Powered by Linux