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

problem: multiple files

hallo,
I'm pretty new to C and I'm having some problems while compiling multiple files; here what I do:

I've got 3 files:
1.cpp , 2.cpp , INIT.h

and these are the files:
1.cpp:

#include <INIT.h>
int main (int argc, char *argv[]) {
a=3;
printf("%d",a);
}


2.cpp:

#include <INIT.h>
void funzione(){
a=2;
}


INIT.h:

#ifndef HEADER
#define HEADER
#include <stdio.h>
int a;
#endif



Ok, now I compile them in this way:
g++ -c 1.cpp -I.
g++ -c 2.cpp -I.

but when I type:
g++ 1.o 2.o

it says:
2.o: (.bss+0x0): multiple definition of `a'
1.o: (.bss+0x0): first defined here
collect2: ld returned 1 exit status

why is it so?
thanks a lot for your help
Ale
Sep 8 '08 #1
2 1359
manontheedge
175 100+
you're declaring 'a' in the header file ... and then defining it in '1' AND '2'.

first, your file '2' isn't doing anything except trying to define what is being defined in file '1'

so, you are redefining 'a', which isn't allowed, you can only define that variable 'a' once.

by define, I mean, when you have a = 6, that's a definition ... you can't then say, a = 5 because it's already been defined somewhere else
Sep 8 '08 #2
arnaudk
424 256MB
by define, I mean, when you have a = 6, that's a definition ... you can't then say, a = 5 because it's already been defined somewhere else
That's not true. The following is perfectly valid:
Expand|Select|Wrap|Line Numbers
  1. int a;
  2. int main()
  3. {
  4.   a=5;
  5.   a=6; // changed my mind.
  6.   return 0;
  7. }
  8.  
The problem grandemahatma1 is having is that a is declared twice within the same (global) scope. In this sense, the error message produced by the compiler is somewhat misleading but that's just a question of semantics.

The reason is that the inclusion guard can not work across translation units; INIT.h is included in 1.cpp when it is compiled, and again in 2.cpp when it is compiled since the two are preprocessed and compiled independently thus HEADER will be undefined in both cases when the header is included. After preprocessing, this leaves you with a declaration int a; in both 1.cpp and 2.cpp. Then you link the two together and wham, you get a multiple declaration error. To see what I mean, preprocess the code only using the -E option and you'll see the multiple declarations.

The solution is not to declare variables in header file but to declare "int a;" in one of the compilation units and declare "extern int a;" in the other compilation units to indicate that a has external linkage. Or, even better, not to use global variables at all.
Sep 8 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Primo | last post by:
Hello, I am building a data management application with the following processes: Process 1 is a Windows service which uses FileSystemWatcher to monitor a directory. Process 2 opens a file...
3
by: Stampede | last post by:
Hi, I write an application which waits for incomming files in a specified directory. I thought, that using the FileSystemWatcher would be the best, as it does exactly what I need. But now I have...
1
by: Lauchlan M | last post by:
Hi. I have an ASP.net project that works fine. I copied it to another project using the following steps: << (i) create a new VS.NET ASP.NET project.
3
by: Dan | last post by:
Hi, I have a problem using an aspx page with a Control on it. I get the following error message Compiler Error Message: CS1595: 'Test.Class2' is defined in multiple places; using definition...
1
by: abh1508 | last post by:
Following a release of code the following problem occurs on certain asp ..net pages. This is not a problem on other testing/demo environments. IIS seems to be creating certain files twice in the...
5
by: steve_marjoribanks | last post by:
This isn't strictly an XML problem but I thought someone might be able to help! As part of my degree I am working on a new data format for use in the geotechnical engineering domain. The data...
2
by: SharpCoderMP | last post by:
i'm trying to embed multiple program icons in my executable. the only way so far i managed to do that is to embed native win32 resource file with multiple icons. it works, but... when i create a...
0
by: nt91rx78 | last post by:
Our college changes 18 weeks semester to 16 semester, so our CS professor cannot finish teaching the last important chapter which is related with my problw\em. This is program C problem Anyone...
10
by: michael | last post by:
Hi All, I have the following: ------------ file constants.h--------- #ifndef constants_ #define constants_ const int FOO = 1; const int BAR = 2;
43
by: bonneylake | last post by:
Hey Everyone, Well this is my first time asking a question on here so please forgive me if i post my question in the wrong section. What i am trying to do is upload multiple files like gmail...
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
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
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.