Definition in Header files - C
This is a discussion on Definition in Header files - C ; 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 ...
-
Definition in Header files
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.
-
Re: Definition in Header files
On Oct 22, 10:38 pm, 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.
Global things that don't have to be global is a bad idea.
Nevertheless, you can use them {warning -- untested} as follows:
-- const.c --
#include "const.h"
int gVal = 100;
double gPi = 3.1415926535897932384626433832795;
-- end const.c --
-- const.h --
extern int gVal;
extern double gPi;
-- end const.h --
-- main.c --
#include <stdio.h>
#include "const.h"
int main(void)
{
printf("gVal = %d and gPi = %f\n", gVal, gPi);
return 0;
}
-- end main.c --
-
Re: Definition in Header files
Madhur wrote:
>
> 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.
>
Why avoid doing the obvious?
--
Ian Collins.
-
Re: Definition in Header files
On Oct 23, 11:03 am, user923005 <dcor...@connx.com> wrote:
> On Oct 22, 10:38 pm, 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.
>
> Global things that don't have to be global is a bad idea.
> Nevertheless, you can use them {warning -- untested} as follows:
>
> -- const.c --
> #include "const.h"
> int gVal = 100;
> double gPi = 3.1415926535897932384626433832795;
> -- end const.c --
>
> -- const.h --
> extern int gVal;
> extern double gPi;
> -- end const.h --
>
> -- main.c --
> #include <stdio.h>
> #include "const.h"
> int main(void)
> {
> printf("gVal = %d and gPi = %f\n", gVal, gPi);
> return 0;}
>
> -- end main.c --
Here are a1 and a2 are global variables and I will be using in both
sample1.c and sample2.c files.
This what I wanted to avoid.. as indicated by me
>>>>I can avoid this problem by having a C file which
>>>>contains the definitions and H file for declarations.
-
Re: Definition in Header files
On Oct 23, 11:14 am, Ian Collins <ian-n...@hotmail.com> wrote:
> Madhur wrote:
>
> > 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.
>
> Why avoid doing the obvious?
>
> --
> Ian Collins.
I want those variable to be const. Defining them in a C file , I can
not declare them as const.
Why doesn't C compilation doesn't support Internal linkage as it is
supported by C++??
-
Re: Definition in Header files
On Oct 23, 12:38 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.
Well, no really good ways.
You can do like you suggested and put the definitions in a separate
file. This is probably the best approach, but has the obvious problem
of considerable duplication.
A second possibility is to make all of these constants static as
well. That solves the multiple definition problem, but duplicates the
constants in each module that includes the header. Depending on the
exact definition of "huge" and the number of times the header is
included, this may result in an unacceptable expansion of the
executable.
Third you could keep the definitions in the header, but play
preprocessor games so that when most users of the constants include
the header, the initializations are omitted. For example:
const int a1
#ifdef IN_DEFINING_MODULE
= 0
#endif
;
The you'd have a const.c which included const.h along with the
#define:
/* const.c */
#define IN_DEFINING_MODULE
#include "const.h"
This is obviously ugly, but it does work.
As others have pointed out, it's probably a sign of a design problem
if you have "huge" numbers of global constants, so you may want to
start by rethinking your design.
-
Re: Definition in Header files
On Oct 22, 11:42 pm, Madhur <madhurr...@gmail.com> wrote:
> On Oct 23, 11:14 am, Ian Collins <ian-n...@hotmail.com> wrote:
>
> > Madhur wrote:
>
> > > 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.
>
> > Why avoid doing the obvious?
>
> > --
> > Ian Collins.
>
> I want those variable to be const. Defining them in a C file , I can
> not declare them as const.
Of course you can.
> Why doesn't C compilation doesn't support Internal linkage as it is
> supported by C++??
We might ask, why doesn't C support block data statements like
Fortran?
At any rate, you can have external const in C. The code you posted
should not work in C++ either. Every time you include that header
file, it defines a new instance of the variables.
C:\tmp>cl /W4 /Ox const.c main.c
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 14.00.50727.762
for 80x86
Copyright (C) Microsoft Corporation. All rights reserved.
const.c
main.c
Generating Code...
Microsoft (R) Incremental Linker Version 8.00.50727.762
Copyright (C) Microsoft Corporation. All rights reserved.
/out:const.exe
const.obj
main.obj
C:\tmp>lin const.c main.c
C:\tmp>"C:\Lint\Lint-nt" +v -i"C:\Lint" std.lnt -os(_LINT.TMP)
const.c main.c
PC-lint for C/C++ (NT) Vers. 8.00u, Copyright Gimpel Software
1985-2006
--- Module: const.c (C)
--- Module: main.c (C)
C:\tmp>type _LINT.TMP | more
--- Module: const.c (C)
--- Module: main.c (C)
---
output placed in _LINT.TMP
C:\tmp>type const.c
#include "const.h"
const int gVal = 100;
const double gPi = 3.1415926535897932384626433832795;
C:\tmp>type const.h
#ifndef CONST_H_INCLUDED
#define CONST_H_INCLUDED
extern const int gVal;
extern const double gPi;
#endif /* CONST_H_INCLUDED */
C:\tmp>type main.c
#include <stdio.h>
#include "const.h"
int main(void)
{
printf("gVal = %d and gPi = %f\n", gVal, gPi);
return 0;
}
C:\tmp>const
gVal = 100 and gPi = 3.141593
-
Re: Definition in Header files
Madhur wrote:
> On Oct 23, 11:14 am, Ian Collins <ian-n...@hotmail.com> wrote:
>> Madhur wrote:
>>
>>> 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.
>> Why avoid doing the obvious?
>>
*Please* don't quote signatures.
>
> I want those variable to be const. Defining them in a C file , I can
> not declare them as const.
Nonsense, how else would one declare an extern constant?
> Why doesn't C compilation doesn't support Internal linkage as it is
> supported by C++??
>
C isn't C++.
--
Ian Collins.
-
Re: Definition in Header files
robertwessel2@yahoo.com wrote:
> On Oct 23, 12:38 am, Madhur <madhurr...@gmail.com> wrote:
>>
>> 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.
>
>
> Well, no really good ways.
>
> You can do like you suggested and put the definitions in a separate
> file. This is probably the best approach, but has the obvious problem
> of considerable duplication.
>
Where?
--
Ian Collins.
-
Re: Definition in Header files
On Oct 23, 2:09 am, Ian Collins <ian-n...@hotmail.com> wrote:
> > You can do like you suggested and put the definitions in a separate
> > file. This is probably the best approach, but has the obvious problem
> > of considerable duplication.
>
> Where?
The definitions in the proposed const.c largely duplicates the
declarations in const.h, and introduces the usual double maintenance
problems. How severe those are is dependent on the situation.
Similar Threads
-
By Application Development in forum c++
Replies: 5
Last Post: 10-24-2007, 03:53 AM
-
By Application Development in forum PHP
Replies: 5
Last Post: 09-10-2007, 06:58 AM
-
By Application Development in forum DOTNET
Replies: 2
Last Post: 06-30-2007, 05:12 PM
-
By Application Development in forum c++
Replies: 7
Last Post: 06-28-2007, 03:45 AM
-
By Application Development in forum Adobe Tools
Replies: 3
Last Post: 01-18-2007, 03:47 AM