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

Include files with function prototypes and variable declaration/definition

Hello!

I would like to write a C programm with multiple include files, where
one may use another one (to print the value of a variable).
When I try to compile the program below, the linker says, there's a
multiple definition of 'value'.
I'm using Windows XP, MinGW (included with Code::Blocks 1.0RC2) with
GCC 3.4.4.

Can anyone help me to solve the problem?

Thanks a lot!
Kristian Virkus
Make output
=================================================
gcc -c -o func_a.o func_a.c
gcc -c -o func_b.o func_b.c
gcc -c -o main.o main.c
gcc -o main.exe func_b.o func_a.o main.o
func_a.o:func_a.c:(.bss+0x0): multiple definition of `value'
func_b.o:func_b.c:(.bss+0x0): first defined here
main.o:main.c:(.bss+0x0): multiple definition of `value'
func_b.o:func_b.c:(.bss+0x0): first defined here
collect2: ld returned 1 exit status
make: *** [main] Error 1
Makefile
=================================================
CC=gcc
main: main.o func_a.o func_b.o
$(CC) -o main.exe func_b.o func_a.o main.o
main.o: main.c func_a.o func_b.o
$(CC) -c -o main.o main.c
func_a.o: func_a.c
$(CC) -c -o func_a.o func_a.c
func_b.o: func_b.c
$(CC) -c -o func_b.o func_b.c
main.c
=================================================
#include "func_a.h"
#include "func_b.h"

int main()
{
fncA();
fncB();
}
func_a.h
=================================================
#ifndef FUNC_A_H_
#define FUNC_A_H_

int value = 0;
extern void fncA();

#endif
func_a.c
=================================================
#include "func_a.h"

void fncA()
{
}
func_b.h
=================================================
#ifndef FUNC_B_H_
#define FUNC_B_H_

extern void fncB();

#endif
func_b.c
=================================================
#include <stdio.h>
#include "func_a.h"
#include "func_b.h"

void fncB()
{
printf("%d", value);
}

Feb 7 '07 #1
7 2166
Kristian Virkus said:
Hello!

I would like to write a C programm with multiple include files, where
one may use another one (to print the value of a variable).
When I try to compile the program below, the linker says, there's a
multiple definition of 'value'.
If you must use file scope objects with external linkage (which is
sometimes, but rarely, a good idea), do not define them in a header.
Instead, *declare* them in a header, and define them in exactly one
source file. So:

<snip>
func_a.h
=================================================
#ifndef FUNC_A_H_
#define FUNC_A_H_

int value = 0;
Change this to:

extern int value;

That's a declaration, but not a definition, so it reserves no storage.
It just provides necessary information to the compiler about the
object.
extern void fncA();

#endif
func_a.c
=================================================
#include "func_a.h"
Here, add:

int value = 0;

or just:

int value;

since static objects are initialised to 0 anyway. This is a definition,
not merely a declaration, so it reserves storage.

<snip>

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Feb 7 '07 #2
Hello again!
If you must use file scope objects with external linkage (which is
sometimes, but rarely, a good idea), do not define them in a header.
Instead, *declare* them in a header, and define them in exactly one
source file.
Yes, that works. I'm sure I tried that before but it did not work
because of some reason.
How would you normally realize the value-variable? With #define value
0 or how?

Thanks,
Kristian

Feb 7 '07 #3
With #define value 0 or how?

If it shall be a constant.

Feb 7 '07 #4
On 7 Feb 2007 07:01:23 -0800, in comp.lang.c , "Kristian Virkus"
<kr*************@web.dewrote:
>Hello again!
>If you must use file scope objects with external linkage (which is
sometimes, but rarely, a good idea), do not define them in a header.
Instead, *declare* them in a header, and define them in exactly one
source file.

Yes, that works. I'm sure I tried that before but it did not work
because of some reason.
How would you normally realize the value-variable? With #define value
0 or how?
Just like Richard said. Declare it in a header, define it / set its
value in exactly one source file.
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Feb 7 '07 #5
Kristian Virkus wrote:
>With #define value 0 or how?

If it shall be a constant.
#define VALUE 0
That's the common way to do it in C. It's common to use upper case
names for these symbolic constants, just to differentiate them from
variables.
Feb 7 '07 #6
Tydr Schnubbis wrote:
Kristian Virkus wrote:
With #define value 0 or how?
If it shall be a constant.
#define VALUE 0

That's the common way to do it in C. It's common to use upper case
names for these symbolic constants, just to differentiate them from
variables.
Usually better would be enum { value = 0 }, possibly using the
uppercase spelling. This makes sure value obeys normal scope rules and
doesn't cause value to be replaced with 0 when you're not using it in
an expression. It does mean you can't use it in #if expressions,
though, so sometimes it's not the right way to go.

Feb 7 '07 #7
Kristian Virkus wrote:
>
How would you normally realize the value-variable? With #define value
0 or how?
Kristian Virkus wrote:
>>With #define value 0 or how?

If it shall be a constant.
Trimming quotations is good, but please provide sufficient context.

As far as specifying a constant for your program, there are several ways
to do this. The technique I use often depends on my overall program
structure.

With an initial value of 0, if 0 is the natural value for the
application, such as initial value for a summation, a constant 0 where
the variable is defined is fine, assuming it doesn't need to be
initialized in multiple places.

If the number has special significance, such as the maximum number of
iterations allowed for an algorithm, I add a comment explaining the
significance.

In many cases there are several system parameters, such as minimum
frequency, number of frequency channels, and channel spacing, that I
want to define in one place. I will usually group them together and
probably define them with a macro name. The definition will have a
comment explaining the significance of the value and the units, such as
kHz for frequency. Those special values will be defined in _only one
place_, which prevents problems with inconsistent data whenever changes
are made. Derived values, such as maximum frequency, is computed from
the fundamental parameters. Since the parameters are constants, the
arithmetic expression is a constant expression and is as efficient at
run time as a single constant. These derived values are usually
assigned to additional macros:

#define FREQ_MAX_KHZ (FREQ_MIN_KHZ + NCHANNELS * CHANNEL_SEP_KHZ)
/* maximum transmission frequency, kHz */

My own experience is that describing constants and variable use
accurately, where defined, makes the program much easier to understand,
and thus, less likely to have errors.

--
Thad
Feb 8 '07 #8

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

Similar topics

0
by: Tom Lee | last post by:
Hi, I'm new to .NET 2003 compiler. When I tried to compile my program using DEBUG mode, I got the following errors in the C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7 \include\xdebug...
16
by: matthurne | last post by:
I just started learning C++ on my own...I'm using Accelerated C++. Something it hasn't explained and I keep wondering about is how header files actually work. I suspect it doesn't get into it...
7
by: johny smith | last post by:
Can someone please explain to me the difference between these two: function1( const int a) function2( int const a) Both seemed to compile, but what is the difference between the two above....
6
by: atv | last post by:
Alright, i have some questions concerning include files en global variables.I hope someone is willing to answer these. 1).Why is it that if i define a global variable in a file, say main.c, and...
28
by: Michael B. | last post by:
I tend to use rather descriptive names for parameters, so the old style of declaration appeals to me, as I can keep a declaration within 80 chars: void * newKlElem...
44
by: Neil Cerutti | last post by:
In Rob Pike's style guide he urges the following: Simple rule: include files should never include include files. If instead they state (in comments or implicitly) what files they need...
5
by: Daniel Nichols | last post by:
I've noticed that in a C module (.c, .h file combination) that if you create a function's definition before it is used in other functions than a declaration is not necessary. I believe if the...
2
by: key9 | last post by:
Hi all look at the organize tree main.c ------ #include lib_adapter.c main() { foo();
29
by: Ravishankar S | last post by:
Dear C Experts, While prepating a content for a C course,I made section on function prototypes. Could you kindly provide me your comments on its correctness. Thank you ! Q12: What is the...
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...
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
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.