473,406 Members | 2,467 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,406 software developers and data experts.

Link Error 'Multiply Define Symbol'

"Hi,

This is a very simple question that confuses me (probably due to some
lack of knowledge...)

I will illustrate my problem with a smiple example:
My project has the following files:
file1.c
#include file3.h
file2.c
#include file3.h
file3.h
#ifndef _file3
#define _file3
char a[4]; //this one causes no error
char b[]="Hello"; //this one causes the error
#endif
Can anybody explain why I'm getting the link error upon compilation.
Thanks"
Jul 22 '05 #1
4 2489
Rafi Kfir wrote:
This is a very simple question that confuses me (probably due to some
lack of knowledge...)

I will illustrate my problem with a smiple example:
My project has the following files:
file1.c
#include file3.h
file2.c
#include file3.h
file3.h
#ifndef _file3
#define _file3
char a[4]; //this one causes no error
char b[]="Hello"; //this one causes the error
#endif
Can anybody explain why I'm getting the link error upon compilation.


Because your symbol 'b' is defined in more than one compilation unit.

Rule: never put definitions of non-const data in headers.

Hint: double inclusion guards have no effect across compilation units.

V
Jul 22 '05 #2

"Rafi Kfir" <ra*******@telrad.co.il> wrote in message
news:f7**************************@posting.google.c om...
"Hi,

This is a very simple question that confuses me (probably due to some
lack of knowledge...)

I will illustrate my problem with a smiple example:
My project has the following files:
file1.c
#include file3.h
file2.c
#include file3.h
file3.h
#ifndef _file3
#define _file3
char a[4]; //this one causes no error
It should do
char b[]="Hello"; //this one causes the error
#endif
Can anybody explain why I'm getting the link error upon compilation.


Do it like this

// file3.h
extern char a[];
extern char b[];

// either file1.c or file2.c not both
char a[4]; //this one causes no error
char b[]="Hello"; //this one causes the error

You need to understand the difference between a declaration and a
definition. You can only have one definition. What you had before put the
definition in the header file. When you include that header file in more
that one .c file you get multiple definitions and linker errors.

The way I did it, you have a declaration in the header file, you can have as
many declarations as you like. But I put the definitions in one of the .c
files.

john
Jul 22 '05 #3
Thanks Victor,
Hint: double inclusion guards have no effect across compilation units.


What is double inclusion guards? Can you explain with an example please?

Thanks
Rafi
Jul 22 '05 #4
Rafi Kfir wrote:
Thanks Victor,

Hint: double inclusion guards have no effect across compilation units.

What is double inclusion guards? Can you explain with an example please?

It's that stuff that you have in your header in the beginning and at
the end:

#ifndef _file3
#define _file3

....

#endif

V
Jul 22 '05 #5

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

Similar topics

3
by: Falk Fassmann | last post by:
Hello people, I have some problem with Debugging in MS Visual C++. I want to generate a TCP/IP-Socket. The compiling-process is fine but when I debugg the project there's occuring some errors...
4
by: firegun9 | last post by:
I am not a newbie C++, but I haven't coded multiple files project under g++. Here is my quesiont: b.h //////////////////// #ifndef b_h #define b_h class b{
2
by: Helen | last post by:
Hi I am trying to compile a package of avi to mpeg1 C source codes But I got the link error I searched actually my VFW.h and Vfw32.lib are all in the directory what should I do thanks a lot...
0
by: Krishnan | last post by:
Hi I am trying to build a small client-server program using OpenSSL functions. I get errors on trying to build the program using Visual Studio .Net (Visual C++ 7.0). I am new to Visual Studio...
3
by: Leith Bade | last post by:
I have been trying to use the new Visual C++ Toolkit 2003 with the VC6 IDE I set up the executable, inlcude, and library directories to point to the new compilers I had to fix a few errors in the...
10
by: k.jayachandran | last post by:
I have a very curious and unique problem here. I'm creating a parser using bison and flex. i did all the development work in a linux environment. the project includes the source files output from...
2
by: Angus | last post by:
Hello I am using some classes from a third party and have included them in my projecxt and am compiling and linking with them. Everything compiles ok but I get these link errors: I added...
1
by: girays | last post by:
I have a template class which name is EntityRepository and when I compile this class I get no error. But when I use this class in a main method I get LNK2019 linking error. std::map object is used...
2
by: akhilesh.noida | last post by:
I am trying to compile glibc-2.5 for ARM based board. But I am getting errors while configuring it. Please check and give your inputs for resolving this. configure command : $...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
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,...
0
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...

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.