473,395 Members | 1,641 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,395 software developers and data experts.

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.

Oct 23 '07 #1
4 5962
On Oct 23, 6:39 am, Madhur <madhurr...@gmail.comwrote:
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 think your problem is that, based on the extension,
these are C rather than C++ files.
In C++ a1 and a2 have internal linkage so you should have
no problem, but the rules in C, IIRC, are different.
So, rename the files to cpp or otherwise compile them as
C++. If they must remain C, put "static" in front of
the variable definitions.

Oct 23 '07 #2

"tragomaskhalos" <da*************@logicacmg.comwrote in message
news:11**********************@q3g2000prf.googlegro ups.com...
On Oct 23, 6:39 am, Madhur <madhurr...@gmail.comwrote:
>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 think your problem is that, based on the extension,
these are C rather than C++ files.
In C++ a1 and a2 have internal linkage so you should have
no problem, but the rules in C, IIRC, are different.
So, rename the files to cpp or otherwise compile them as
C++. If they must remain C, put "static" in front of
the variable definitions.
I'm pretty sure that's not the problem. The link error is probably because
there are no include guards in the header file, and the header file is
included by two files in the same compilation unit.

But you are correct that those should be .cpp files if he's using C++. The
main reason for that is because certain IDEs will use the C compiler instead
of the C++ compiler for files with a .c extension.

-Howard

Oct 23 '07 #3
Howard wrote:
"tragomaskhalos" <da*************@logicacmg.comwrote in message
news:11**********************@q3g2000prf.googlegro ups.com...
>On Oct 23, 6:39 am, Madhur <madhurr...@gmail.comwrote:
>>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 think your problem is that, based on the extension,
these are C rather than C++ files.
In C++ a1 and a2 have internal linkage so you should have
no problem, but the rules in C, IIRC, are different.
So, rename the files to cpp or otherwise compile them as
C++. If they must remain C, put "static" in front of
the variable definitions.

I'm pretty sure that's not the problem. The link error is probably
because there are no include guards in the header file, and the
header file is included by two files in the same compilation unit.
What *same compilation unit*? Separate .c files should mean separate
compilation, no?
But you are correct that those should be .cpp files [..]
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 23 '07 #4
On Oct 23, 6:05 pm, "Howard" <m...@here.comwrote:
"tragomaskhalos" <dave.du.verg...@logicacmg.comwrote in message
[...]
I think your problem is that, based on the extension,
these are C rather than C++ files.
In C++ a1 and a2 have internal linkage so you should have
no problem, but the rules in C, IIRC, are different.
So, rename the files to cpp or otherwise compile them as
C++. If they must remain C, put "static" in front of
the variable definitions.
I'm pretty sure that's not the problem.
I'm almost 100% sure that it is the problem. Most of the C++
compilers I know will compile a file with the extension .c as C.
And in C, const doesn't mean internal linkage.
The link error is probably because
there are no include guards in the header file, and the header file is
included by two files in the same compilation unit.
That would cause a compile error, not a link error.
But you are correct that those should be .cpp files if he's
using C++. The main reason for that is because certain IDEs
will use the C compiler instead of the C++ compiler for files
with a .c extension.
Not the IDE's. Most "compilers" are actually driver programs,
which invoke different compilers, the linker, perhaps the
librarian, etc., depending on the options given and the filename
extensions. While g++ and Sun CC will compile .c as C++, VC++
(command cl) doesn't, and historically, I believe that many
others didn't either. (The fact that g++ and Sun CC don't
actually surprised me. I would, in fact, consider this a bug:
if I want to compile and link an application with both C and
C++, I want the C compiled as C, but I still need to invoke C++
for the link to work correctly.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Oct 24 '07 #5

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

Similar topics

2
by: Jochen Zeischka | last post by:
Hi everybody! I have a question concerning code organisation. Suppose I have the following header file: #ifndef SOME_NAME #define SOME_NAME namespace N { void F()
2
by: Hartmut Sbosny | last post by:
Hello NG, I have a question. I have a header file with a function template and a fully specialized version of it, for instance //======== File "templ.hpp" ======== #include <iostream> // the...
7
by: A_StClaire_ | last post by:
hi, I'm working on a project spanning five .cpp files. each file was used to define a class. the first has my Main and an #include for each of the other files. problem is my third file...
19
by: J. J. Farrell | last post by:
After many years of dealing with definition and linkage issues in ways that I know to be safe, I've decided it's time to try to understand this area properly. Consider a header file with the file...
10
by: PCHOME | last post by:
Hi! Would someone please help me thess C error(in gcc on Linux)? The compiler continues to give me: readLP.o: In function `Input_Problem': readLP.o(.text+0x0): multiple definition of...
14
by: Carramba | last post by:
hi! I have program with several funktion witch are in separete files, I have one include file were I have definet some variables and initiated 'const double fVar=0.874532;' this files is includet...
2
by: Paolo | last post by:
I imported a VC++6.0 project into VC++7.1. The conversion operation makes a mess with Preprocessor Definitions, adding a "$(NoInherit)" for each file. For example: I had a DLL project in VC++6.0...
3
by: WaterWalk | last post by:
I read from c99 std TC2 community draft, and found the following statement in 6.7.2.3: "Tw o declarations of structure, union, or enumerated types which are in different scopes or use different...
4
by: Mike | last post by:
Hi ! I have some strange problem and I would like to know if it is a bug or not : In my projects, in 2 different .cpp files, I use the same name to define a local structure: file1.cpp : ...
31
by: Madhur | last post by:
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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,...

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.