473,387 Members | 1,700 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Linkage Error

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?
Feb 1 '08 #1
3 1859
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
Feb 1 '08 #2
In article <66**********************************@v17g2000hsa. googlegroups.com>,
Tricky <Tr********@gmail.comwrote:
>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
Feb 1 '08 #3
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: <mailto:Th*************@gmail.com>

Feb 1 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: qazmlp | last post by:
const has internal linkage in C++, but external linkage in C. Am I right ? But, linker reports multiply-defined error if the following header is included in multiple .cpp files. //...
4
by: usr2003 | last post by:
I wrote the following test program to test the linkage directives extern "C": #include <stdio.h> extern "C" {
47
by: Richard Hayden | last post by:
Hi, I have the following code: /******************************** file1.c #include <iostream> extern void dummy(); inline int testfunc() {
4
by: Tron Thomas | last post by:
I have read that anonymous namespaces are preferred in C++ over declaring global statics because the namespace can hide information from other translation units and at the same time provide...
10
by: Mark A. Gibbs | last post by:
I have a question about mixing C and C++. In a C++ translation unit, I want to define a function with internal linkage and C calling convention. Here's a sample of what I want to do: //...
3
by: martinbriefcase | last post by:
Compiling a program using ACE + MSVC++8, got lots of errors and some are listed as below: Error 2 error C2894: templates cannot be declared to have 'C' linkage c:\program files\microsoft visual...
13
by: fctk | last post by:
source: http://rm-f.net/~orange/devel/specifications/c89-draft.html#3.1.2.2 there are two passages in this paragraph i can't fully understand: 1) "If the declaration of an identifier for an...
6
by: The Architect | last post by:
Hi, If I have the same symbol in the .data section of 2 obj files, LD gives a multiple declaration error on linking? Would like to know the reason for this (diab only issues a warning) Also...
12
by: Taras_96 | last post by:
Hi everyone, AFAIK external linkage allows you to refer to variables/functions outside of the current translation unit. A variable in an unnamed namespace is similar to declaring a static...
0
by: ramasubramanian.rahul | last post by:
i am trying to use memwatch (2.71) to track c++ bugs in my project. i initially tried to run it using a small test program and it worked properly without any hitch. but then when i try a platform...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.