Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Linkage Error

Reply
Thread Tools

Linkage Error

 
 
Morris Dovey
Guest
Posts: n/a
 
      02-01-2008
Tricky wrote:
>
> When I try and link together 3 object files, I get the following
> problem:
>
> gcc fusionFileIO.o fusionAlgorithms.o main.o -o fusionTK.exe
> main.o:main.c.rdata+0x0): multiple definition of `_BAD_IMAGE'
> fusionFileIO.o:fusionFileIO.c.rdata+0x0): first defined here
> collect2: ld returned 1 exit status
> make: *** [fusionTK.exe] Error 1
>
> The problem in question comes from the following header file, which is
> included in all of the object files:
>
> #define FILE_BUFFER_SIZE 256
>
> typedef struct
> {
> //image dimensions
> int pixPerLine;
> int numLines;
>
> //image data
> char *data;
> } image_t;
>
> const image_t BAD_IMAGE = {-1, -1, 0x0};
>
> This is the only time BAD_IMAGE is defined.
>
> Any thoughts on where Im going wrong?


Each object file attempts to produce its own instance of
BAD_IMAGE. The easy way out might be to move it from the header
to its very own .c file (which means that you'll have four object
files to link).

--
Morris Dovey
DeSoto Solar
DeSoto, Iowa USA
http://www.iedu.com/DeSoto
 
Reply With Quote
 
 
 
 
Tricky
Guest
Posts: n/a
 
      02-01-2008
When I try and link together 3 object files, I get the following
problem:

gcc fusionFileIO.o fusionAlgorithms.o main.o -o fusionTK.exe
main.o:main.c.rdata+0x0): multiple definition of `_BAD_IMAGE'
fusionFileIO.o:fusionFileIO.c.rdata+0x0): first defined here
collect2: ld returned 1 exit status
make: *** [fusionTK.exe] Error 1

The problem in question comes from the following header file, which is
included in all of the object files:

#define FILE_BUFFER_SIZE 256

typedef struct
{
//image dimensions
int pixPerLine;
int numLines;

//image data
char *data;
} image_t;

const image_t BAD_IMAGE = {-1, -1, 0x0};



This is the only time BAD_IMAGE is defined.

Any thoughts on where Im going wrong?
 
Reply With Quote
 
 
 
 
Walter Roberson
Guest
Posts: n/a
 
      02-01-2008
In article <(E-Mail Removed)>,
Tricky <(E-Mail Removed)> wrote:
>When I try and link together 3 object files, I get the following
>problem:


>main.o:main.c.rdata+0x0): multiple definition of `_BAD_IMAGE'


>The problem in question comes from the following header file, which is
>included in all of the object files:


>#define FILE_BUFFER_SIZE 256


>typedef struct
>{
> //image dimensions
> int pixPerLine;
> int numLines;
>
> //image data
> char *data;
>} image_t;


>const image_t BAD_IMAGE = {-1, -1, 0x0};


>This is the only time BAD_IMAGE is defined.


>Any thoughts on where Im going wrong?


const does not define a constant: it indicates that the values
are read-only (when accessed through that name.)

You are thus attempting to define actual storage for the object
BAD_IMAGE in each of your files that includes the header,
and you are doing so at file scope and you are not using the
'static' keyword so each of the definitions is a global definition.
So you are ending up with a global definition for BAD_IMAGE in
each of the translation units, and of course your linker is complaining.

To resolve this, you need to give a -definition- for BAD_IMAGE in
only one translation unit, and in each other translation unit,
you need to

extern const image_t BAD_IMAGE;

to -reference- the global BAD_IMAGE without defining it in those other
places.
--
"Any sufficiently advanced bug is indistinguishable from a feature."
-- Rich Kulawiec
 
Reply With Quote
 
Kenneth Brody
Guest
Posts: n/a
 
      02-01-2008
Tricky wrote:
>
> When I try and link together 3 object files, I get the following
> problem:
>
> gcc fusionFileIO.o fusionAlgorithms.o main.o -o fusionTK.exe
> main.o:main.c.rdata+0x0): multiple definition of `_BAD_IMAGE'
> fusionFileIO.o:fusionFileIO.c.rdata+0x0): first defined here
> collect2: ld returned 1 exit status
> make: *** [fusionTK.exe] Error 1
>
> The problem in question comes from the following header file, which is
> included in all of the object files:

[...]
> const image_t BAD_IMAGE = {-1, -1, 0x0};
>
> This is the only time BAD_IMAGE is defined.
>
> Any thoughts on where Im going wrong?


Yes. You are defining it in every file that includes that header
file. Every one of those object files has a global variable called
"BAD_IMAGE", hence the "multiple definition" error. (OT: execute
"nm" on each of those ".o" files, and you will see them.) This is
no different than having "int i = 3;" in the header.

I think your problem is that "const" doesn't do what you think it
does. Perhaps it does in C++, but not in C.

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <(E-Mail Removed)>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
error C2375 - redefinition; different linkage omnia neo C++ 3 04-24-2010 08:24 AM
error C2375 - redefinition; different linkage omnia neo C Programming 6 04-23-2010 09:35 PM
c++ linkage vs c linkage ramasubramanian.rahul@gmail.com C++ 1 09-12-2008 11:41 AM
namespaces, linkage error, operator overloading jakester C++ 11 04-16-2007 08:05 AM
linkage error when initializing static member array Neno C++ 2 10-23-2004 10:15 AM



Advertisments