473,785 Members | 2,867 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Linkage Error

When I try and link together 3 object files, I get the following
problem:

gcc fusionFileIO.o fusionAlgorithm s.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_SIZ E 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 1888
Tricky wrote:
>
When I try and link together 3 object files, I get the following
problem:

gcc fusionFileIO.o fusionAlgorithm s.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_SIZ E 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************ *************** *******@v17g200 0hsa.googlegrou ps.com>,
Tricky <Tr********@gma il.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_SIZ E 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 indistinguishab le 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 fusionAlgorithm s.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
12144
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. // test_const.h #ifndef HEADER_TEST #define HEADER_TEST
4
3267
by: usr2003 | last post by:
I wrote the following test program to test the linkage directives extern "C": #include <stdio.h> extern "C" {
47
3885
by: Richard Hayden | last post by:
Hi, I have the following code: /******************************** file1.c #include <iostream> extern void dummy(); inline int testfunc() {
4
2459
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 external linkage allowing members of the namespace to be used as template arguments. I'm not quite sure I'm clear on what this really means. What is an example of this anoymous namespace benefit over the use of global statics?
10
6199
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: // main.cpp // This is defined in a C module extern "C" void fake_qsort(void*, std::size_t, std::size_t, int (*compare)(const void*, const void*));
3
4640
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 studio 8\vc\include\iosfwd 39 Error 39 error C2733: second C linkage of overloaded function 'ACE_OS::atoi' not allowed c:\tmp\ace+tao+ciao\ace_wrappers\ace\os_ns_stdlib.h 79 Error 41 error C2733: second C linkage of overloaded function
13
2097
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 object or a function contains the storage-class specifier extern , the identifier has the same linkage as any visible declaration of the identifier with file scope. If there is no visible declaration with file scope, the identifier has external...
6
1761
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 what if the symbols were in any other section (.rdata or .text)? Or which sections of obj files can be merged even if they have overlapping symbol names?
12
5411
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 variable, but according to the standard there is a difference: "While this is essentially true in practical effect, there are subtle differences. Using static as shown here causes "i" to have internal
0
1140
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 wide complation then i get the following error /opt/toolchains/arm-linux-4.1.1/bin/arm-linux-g++ -Wall -mabi=aapcs- linux -D_DEBUG_MSG_ -fPIC `pkg-config --cflags dbus-1 glib-2.0 debug- message-service` -D_ARCH_ARM -D__LIMO_SEC_EXT__...
0
9480
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10152
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9950
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8974
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7500
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6740
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3650
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2880
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.