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
2 1376
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
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: -
int a;
-
int main()
-
{
-
a=5;
-
a=6; // changed my mind.
-
return 0;
-
}
-
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. Sign in to post your reply or Sign up for a free account.
Similar topics |
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 copied into the directory and inserts the data into a
data warehouse.
Process 3 queries the data warehouse and transfers the results to a data mart.
|
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 a problem:
I wonna get shure, that really all files are processed and I need to be
shure, that no file is processed twice.
If my application is started while there are already files in the directory
(which is realistic, as the application could...
|
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.
|
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 from
'c:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\Temporary ASP.NET
Files\root\1f575646\ad3a161b\assembly\dl2\57ca505e\044565c0_f84fc401\Test1.DLL'
The problem is that the control is defined in two different assemblies
|
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 temporary internet
files directory. Any advice or suggestions appreciated.
Server Error in '/ContactCentre/Global' Application.
--------------------------------------------------------------------------------
Compilation Error
| |
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 structure we have come up with is based around a single
instance document for 'raw' data and then numerous other associated
instance documents containing 'interpreted' data (ie. containing some
interpretation in an engineering sense of the raw...
|
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 native win32
resource file with the VS 2005 and put there my icons, VS always
converts some of the 32bit icons into 24bit - so i loose alpha blending
what makes my icons look ugly.
I did an experiment and created a icon file with full range of...
|
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 can help me with this problem, please!!!!!!!!!
This is the problem:
3. Several input text files have been provided as input to your program.
a) Write a function to combine these files into a single file.
b) Write a function to take care of...
|
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;
|
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 does. I found a script that does this on easycfm.com (Topic 13543).
But anyway when i try to upload multiple files it will create multiple records in my database (like it should), but it wont upload multiple files. What ever file i choose to upload...
|
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,...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
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...
| |