For Programmers: Free Programming Magazines  


Home > Archive > Unix Programming > October 2007 > Definition in Header files









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Definition in Header files
Madhur

2007-10-23, 4:27 am

I have problems faced in adding definitions in the command header
file. I have defined a header file which includes huge set of global
constants and I am using them in all the C files.

For example my sample.h file looks like this

/*************

File : sample.h

****************/


const int a1 = 0;
const int a2 = 0;


/****End of sample.h*********/


My source code looks like this

/*****************
File:sample1.c

***********/
#include "sample.h"

int main()
{
}

/*****End of sample1.c************/

/***************
File : sample2.c
**************/

#include "sample.h

int main()
{
}

/*******End of sample2.c**********/

Now the problem is during linking it says multiple definition of
symbols a1 and a2. I can avoid this problem by having a C file which
contains the definitions and H file for declarations. But is there any
way I can avoid doing this. Please let me know.

Frank Cusack

2007-10-23, 4:27 am

On Mon, 22 Oct 2007 22:40:22 -0700 Madhur <madhurrajn@gmail.com> wrote:
> I have problems faced in adding definitions in the command header
> file. I have defined a header file which includes huge set of global
> constants and I am using them in all the C files.

....
> Now the problem is during linking it says multiple definition of
> symbols a1 and a2. I can avoid this problem by having a C file which
> contains the definitions and H file for declarations. But is there any
> way I can avoid doing this. Please let me know.


No.

But why do you want to avoid it? Maybe we can suggest a better approach.
Ben Bacarisse

2007-10-23, 7:18 pm

Madhur <madhurrajn@gmail.com> writes:

> I have problems faced in adding definitions in the command header
> file. I have defined a header file which includes huge set of global
> constants and I am using them in all the C files.
>
> For example my sample.h file looks like this
>
> /*************
>
> File : sample.h
>
> ****************/
>
>
> const int a1 = 0;
> const int a2 = 0;
>
>
> /****End of sample.h*********/
>
>
> My source code looks like this
>
> /*****************
> File:sample1.c
>
> ***********/
> #include "sample.h"
>
> int main()
> {
> }
>
> /*****End of sample1.c************/
>
> /***************
> File : sample2.c
> **************/
>
> #include "sample.h
>
> int main()
> {
> }
>
> /*******End of sample2.c**********/
>
> Now the problem is during linking it says multiple definition of
> symbols a1 and a2. I can avoid this problem by having a C file which
> contains the definitions and H file for declarations. But is there any
> way I can avoid doing this. Please let me know.


I will repeat the "why avoid the obvious way" advice, but add a
"solution". In comp.lang.c you asked why this is not the same in C
and C++. In C++ const definitions that are not explicitly marked
extern have internal linkages. I.e. you are getting (in C++) two
copies of variable (OK, the linked may elide them into one, but that
is a matter outside the standard).

Mark the definitions "static const" and C behaves much like you seem
to expect.

--
Ben.
Frank Cusack

2007-10-23, 7:18 pm

On Tue, 23 Oct 2007 16:01:10 +0100 Ben Bacarisse <ben.usenet@bsb.me.uk> wrote:
> Madhur <madhurrajn@gmail.com> writes:
>
^^^^^^^^^^^^^^^^

....[color=darkred]
>
> I will repeat the "why avoid the obvious way" advice, but add a
> "solution". In comp.lang.c you asked why this is not the same in C
> and C++. In C++ const definitions that are not explicitly marked
> extern have internal linkages. I.e. you are getting (in C++) two
> copies of variable (OK, the linked may elide them into one, but that
> is a matter outside the standard).
>
> Mark the definitions "static const" and C behaves much like you seem
> to expect.


Not at all. They lose the global [across all modules] property, which
is what I presume to be the intent.

-frank
Ben Bacarisse

2007-10-23, 7:18 pm

Frank Cusack <fcusack@fcusack.com> writes:

> On Tue, 23 Oct 2007 16:01:10 +0100 Ben Bacarisse <ben.usenet@bsb.me.uk> wrote:
> ^^^^^^^^^^^^^^^^
>
> ...
>
> Not at all. They lose the global [across all modules] property, which
> is what I presume to be the intent.


Elsewhere the OP complained that the code worked in C++ but not in C.
In C++, file-scope const definitions have internal linkage, just like
static file-scope definitions in C, hence my odd suggestion.

It is hard to tell what is really intended since the original post
sported code with multiple main functions defined which makes no sense
in either language.

Because they probably do want "global constants" I repeated the
advice already given: "why avoid the obvious way?".

--
Ben.
Barry Margolin

2007-10-24, 4:32 am

In article <m2myu9byw6.fsf@sucksless.local>,
Frank Cusack <fcusack@fcusack.com> wrote:

> On Tue, 23 Oct 2007 16:01:10 +0100 Ben Bacarisse <ben.usenet@bsb.me.uk> wrote:
> ^^^^^^^^^^^^^^^^
>
> ...
>
> Not at all. They lose the global [across all modules] property, which
> is what I presume to be the intent.


Since they're just constants, what difference does it make whether
they're global or local?

The only way you'd be able to tell is if you compare addresses.

--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
shakahshakah@gmail.com

2007-10-24, 7:18 pm

On Oct 23, 1:40 am, Madhur <madhurr...@gmail.com> wrote:
> I have problems faced in adding definitions in the command header
> file. I have defined a header file which includes huge set of global
> constants and I am using them in all the C files.
>
> For example my sample.h file looks like this
>
> /*************
>
> File : sample.h
>
> ****************/
>
> const int a1 = 0;
> const int a2 = 0;
>
> /****End of sample.h*********/
>
> My source code looks like this
>
> /*****************
> File:sample1.c
>
> ***********/
> #include "sample.h"
>
> int main()
> {
>
> }
>
> /*****End of sample1.c************/
>
> /***************
> File : sample2.c
> **************/
>
> #include "sample.h
>
> int main()
> {
>
> }
>
> /*******End of sample2.c**********/
>
> Now the problem is during linking it says multiple definition of
> symbols a1 and a2. I can avoid this problem by having a C file which
> contains the definitions and H file for declarations. But is there any
> way I can avoid doing this. Please let me know.


Can you do something like the following?

jc@jc-ubuntu:~/tmp/externtest$ cat sample.h
#ifdef ALLOC_GLOBALS
# define STCLASS extern
# define I(x)
#else
# define STCLASS
# define I(x) x
#endif

STCLASS const int a1 I(=0) ;
STCLASS const int a2 I(=0) ;

jc@jc-ubuntu:~/tmp/externtest$ cat sample1.c
#define ALLOC_GLOBALS
#include "sample.h"

int main() {
}

jc@jc-ubuntu:~/tmp/externtest$ cat sample2.c
#include "sample.h"

int main_2() {
}

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com