Definition in Header files
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > Definition in Header files




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    Definition in Header files  
Madhur


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
10-23-07 06:30 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.






[ Post a follow-up to this message ]



    Re: Definition in Header files  
Frank Cusack


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
10-23-07 12:32 PM

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.





[ Post a follow-up to this message ]



    Re: Definition in Header files  
Ben Bacarisse


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
10-23-07 06:26 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.





[ Post a follow-up to this message ]



    Re: Definition in Header files  
Frank Cusack


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
10-23-07 06:26 PM

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

...[vbcol=seagreen] 
>
> 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





[ Post a follow-up to this message ]



    Re: Definition in Header files  
Ben Bacarisse


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
10-24-07 12:20 AM

Frank Cusack <fcusack@fcusack.com> writes:

> On Tue, 23 Oct 2007 16:01:10 +0100 Ben Bacarisse <ben.usenet@bsb.me.uk> wr
ote: 
>    ^^^^^^^^^^^^^^^^
>
> ... 
>
> 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.





[ Post a follow-up to this message ]



    Re: Definition in Header files  
Barry Margolin


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
10-24-07 06:31 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> wr
ote: 
>    ^^^^^^^^^^^^^^^^
>
> ... 
>
> 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 ***





[ Post a follow-up to this message ]



    Re: Definition in Header files  
shakahshakah@gmail.com


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
10-24-07 06:33 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() {
}






[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 10:57 AM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register