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

defining variable

Hello,

I have a small piece of code, compiled by two 'gcc' and 'borland builder
compiler'. The latter one produces warnings:

[Linker Warning] Public symbol '_freq' defined in both module
C:\WRK\ISDB-T\XML\PROBE_XML.OBJ and C:\WRK\ISDB-T\XML\XML.OBJ
[Linker Warning] Public symbol '_power' defined in both module
C:\WRK\ISDB-T\XML\PROBE_XML.OBJ and C:\WRK\ISDB-T\XML\XML.OBJ

Code structure is the following:

my_hdr.h
....
long freq[NR];
long power[NR];
int i, j;

extern void ParseParams(FILE *fd);
------------

my_hdr.c
#include "my_hdr.h"
/* here follows implementation of ParseParams() */
-------------

main.c (main app)
#include "my_hdr.h" /* because I need ParseParams() */
....

What's wrong with this for borland builder? Do I violate standards or this
is "feature" of borland compiler? Gcc is quite happy.

Thanks in advance.

--
Best regards, Roman
Aug 22 '07 #1
7 2140
Roman Mashak wrote:
Hello,

I have a small piece of code, compiled by two 'gcc' and 'borland builder
compiler'. The latter one produces warnings:

[Linker Warning] Public symbol '_freq' defined in both module
C:\WRK\ISDB-T\XML\PROBE_XML.OBJ and C:\WRK\ISDB-T\XML\XML.OBJ
[Linker Warning] Public symbol '_power' defined in both module
C:\WRK\ISDB-T\XML\PROBE_XML.OBJ and C:\WRK\ISDB-T\XML\XML.OBJ

Code structure is the following:

my_hdr.h
....
long freq[NR];
long power[NR];
int i, j;
By declaring these in the header, they symbols are defined in every
compilation unit that include the header. They should only be defined once.

Use

extern long freq[NR];
extern long power[NR];
extern int i, j;

in the header and define them once in my_hdr.c.

--
Ian Collins.
Aug 22 '07 #2
Roman Mashak said:

<snip>
Code structure is the following:

my_hdr.h
...
long freq[NR];
long power[NR];
int i, j;

extern void ParseParams(FILE *fd);
------------

my_hdr.c
#include "my_hdr.h"
/* here follows implementation of ParseParams() */
-------------

main.c (main app)
#include "my_hdr.h" /* because I need ParseParams() */
...

What's wrong with this for borland builder? Do I violate standards or
this is "feature" of borland compiler? Gcc is quite happy.
The code is broken, so gcc has no business being happy. Crank on the
warning level handle until it screams with pain.

Once you've done that, make either one change or two. If you decide to
make one change, it's this: replace all file scope object definitions
in your code with parameter-passing. If you prefer to make two changes,
the first is to convert the object definitions in the header into mere
declarations by preceding them with 'extern', and the second is to
define the objects exactly once, in exactly one source file (not a
header).

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Aug 22 '07 #3
On Wed, 22 Aug 2007 09:53:09 +0000, Richard Heathfield
<rj*@see.sig.invalidwrote in comp.lang.c:
Roman Mashak said:

<snip>
Code structure is the following:

my_hdr.h
...
long freq[NR];
long power[NR];
int i, j;

extern void ParseParams(FILE *fd);
------------

my_hdr.c
#include "my_hdr.h"
/* here follows implementation of ParseParams() */
-------------

main.c (main app)
#include "my_hdr.h" /* because I need ParseParams() */
...

What's wrong with this for borland builder? Do I violate standards or
this is "feature" of borland compiler? Gcc is quite happy.

The code is broken, so gcc has no business being happy. Crank on the
warning level handle until it screams with pain.
Actually the program violates a "shall" outside of a constraint
clause, rendering the behavior undefined with no diagnostic required.

C99 6.9 P9:

"An external definition is an external declaration that is also a
definition of a function (other than an inline definition) or an
object. If an identifier declared with external linkage is used in an
expression (other than as part of the operand of a sizeof operator
whose result is an integer constant), somewhere in the entire program
there shall be exactly one external definition for the identifier;
otherwise, there shall be no more than one."

One possible consequence of this undefined behavior is for the program
to work as though the external object had only been defined once. In
fact this is quite common with tool sets that use linker models based
on a particular 60-year-old language and implement external objects
using common blocks, as long as no more than one of the definitions
has an explicit initializer.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
Aug 22 '07 #4
Jack Klein said:

<snip>
Actually the program violates a "shall" outside of a constraint
clause, rendering the behavior undefined with no diagnostic required.
Silly of me. One translation unit at a time, Richard, remember? (Duh.)

<snip>

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Aug 22 '07 #5

"Richard Heathfield" <rj*@see.sig.invalidwrote in message
news:m-******************************@bt.com...
>>
What's wrong with this for borland builder? Do I violate standards or
this is "feature" of borland compiler? Gcc is quite happy.

The code is broken, so gcc has no business being happy. Crank on the
warning level handle until it screams with pain.
I've done that before posting to the group (-W -Wall -ansi -pedantic), I got
no warnings.
Once you've done that, make either one change or two. If you decide to
make one change, it's this: replace all file scope object definitions
in your code with parameter-passing. If you prefer to make two changes,
the first is to convert the object definitions in the header into mere
declarations by preceding them with 'extern', and the second is to
define the objects exactly once, in exactly one source file (not a
header).
--
Best regards, Roman
Aug 23 '07 #6
Roman Mashak said:
>
"Richard Heathfield" <rj*@see.sig.invalidwrote in message
news:m-******************************@bt.com...
>>>
Gcc is quite happy.

The code is broken, so gcc has no business being happy. Crank on the
warning level handle until it screams with pain.

I've done that before posting to the group (-W -Wall -ansi -pedantic),
I got no warnings.
That's good. If you can get a clean compile on -W -Wall -ansi -pedantic
-Wformat-nonliteral -Wcast-align -Wpointer-arith -Wbad-function-cast
-Wmissing-prototypes -Wstrict-prototypes -Wmissing-declarations
-Winline -Wundef -Wnested-externs -Wcast-qual -Wshadow -Wconversion
-Wwrite-strings -ffloat-store -O2 I'll be even more impressed. (That's
the set of switches I use regularly.)

In any case, your next step is to move the object definitions into a
single C source file, and to declare (not define) them in a header, as
I outlined in my previous reply. (Alternatively, ditch them completely
and use parameters instead - my own preferred option.)

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Aug 23 '07 #7
Richard Heathfield wrote:
Roman Mashak said:
>>
"Richard Heathfield" <rj*@see.sig.invalidwrote in message
news:m-******************************@bt.com...
>>>>
Gcc is quite happy.

The code is broken, so gcc has no business being happy. Crank on the
warning level handle until it screams with pain.

I've done that before posting to the group (-W -Wall -ansi -pedantic),
I got no warnings.

That's good. If you can get a clean compile on -W -Wall -ansi -pedantic
-Wformat-nonliteral -Wcast-align -Wpointer-arith -Wbad-function-cast
-Wmissing-prototypes -Wstrict-prototypes -Wmissing-declarations
-Winline -Wundef -Wnested-externs -Wcast-qual -Wshadow -Wconversion
-Wwrite-strings -ffloat-store -O2 I'll be even more impressed. (That's
the set of switches I use regularly.)

In any case, your next step is to move the object definitions into a
single C source file, and to declare (not define) them in a header, as
I outlined in my previous reply. (Alternatively, ditch them completely
and use parameters instead - my own preferred option.)
Mmm, you just compiling or linking? Because it will compile without any
problems, but will not link.

P.S. and defining variables in the headers file is a bad tone, it is a place
for the declarations not for definitions.
Aug 23 '07 #8

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

Similar topics

2
by: Sriram Chadalavada | last post by:
Hello everyone, I am a newbie to Python with experience in C programming. For my project, I am re-writing C routines as Python functions. I am currently using raw numerical values and was...
0
by: Dotnetified | last post by:
Reposting after about 2 weeks of no response ... thanks if you can help... ---------------------------------------------------------------------------- -------------- To anyone who thinks they...
2
by: Hamish Symington | last post by:
Hello, I'm trying to re-code a site which I wrote in PHP into ASP.NET using Visual Basic .NET. Something PHP has which has proved invaluable is the concept of variable variables. I haven't...
10
by: nambissan.nisha | last post by:
I am facing this problem.... I have to define a structure at runtime as the user specifies... The user will tell the number of fields,the actual fields...(maybe basic or array types or...
11
by: Rahul | last post by:
Hi, Is the following correct? Here i have declaread a char array in case 1: and am using the same in case 2: first I tried another definition of ch in case 2: assuming that if i=2 then char ch...
1
by: Gary Wessle | last post by:
Hi can I declare an ofstream in a class declaration in .h file and define it inside a method in the .cpp file? if so, what does the syntax of defining it looks like. I tried .... ofstream...
26
by: Cliff Williams | last post by:
Can someone explain the pros/cons of these different ways of creating a class? // 1 function myclass() { this.foo1 = function() {...} } // 2a
11
by: whirlwindkevin | last post by:
I saw a program source code in which a variable is defined in a header file and that header file is included in 2 different C files.When i compile and link the files no error is being thrown.How is...
1
by: mrsmith221189 | last post by:
Hello everyone. I'm a novice to XML/XSL so it would be great to learn from the experts. The ouput of my xml file is BBC EPG(Electronic Programme Guide) table. I'm having problems defining a...
2
by: ismailc | last post by:
Hi, I don't know xml. The stylesheet identifies all the objects from DB & as it reads through the DB, it checks the next object "for-each", I need to check the third object as well & define within...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.