-
February 9th, 2011, 03:20 AM
#1
Confused with linking header file in C++
I have following four files:
1. A hash - header1.h
2. A main header file - header2.h
3. A functions file - functions.cpp and
4. A main file - main.cpp.
header1.h and header2.h are included in functions.cpp
Only header2.h is included in main.cpp
Actually, it works fine.
But now, I wanna call a "const int" data declared in header1.h from main.cpp.
I tried including header1.h in main.cpp, it shows the error. It says redefined.
How can I access a variable from header1?
-
February 9th, 2011, 03:26 AM
#2
Re: Confused with linking header file in C++
declare const int in header1.h as extern. ie:
Code:
extern const int someData;
-
February 9th, 2011, 03:35 AM
#3
Re: Confused with linking header file in C++
Originally Posted by hypheni
declare const int in header1.h as extern. ie:
Code:
extern const int someData;
Hi hypheni,
Thanks for your quick reply.
Do I need to declare "external const int" instead of const int in header1.h?
Do "external const int" and "const int" same for header1.h?
-
February 9th, 2011, 03:38 AM
#4
Re: Confused with linking header file in C++
Originally Posted by ritika_sharma82
But now, I wanna call a "const int" data declared in header1.h from main.cpp.
I tried including header1.h in main.cpp, it shows the error. It says redefined.
Strange. I should say that the "const int" isn't initialized.
A const declaration should look like this,
const int someData = 43;
and you should be able to include it in many compilation units (like main.cpp and function.cpp) you then link.
It's only if someData isn't const you should need to declare it extern. In that case you make sure that this is in one compilation unit only,
int someData;
and that all compilation units who want to use someData incude this,
extern int someData;
Last edited by nuzzle; February 9th, 2011 at 03:43 AM.
-
February 9th, 2011, 04:03 AM
#5
Re: Confused with linking header file in C++
Hi,
Since each .cpp is compiled separately, you could include
Code:
const int someData = 768;
one time in each, without problem. In fact, you could use a different constant number in each unit and it should still work, but probably wouldn't be what you wanted to do.
A typical way to do it which ensures you use the same constant each time would be to use
Code:
const int someData = 768;
in one .cpp file, and in your header file (as hypheni says)
Code:
extern const int someData;
If you are seeing an error indicating that it's redefined, it seems strange. Can you post the error message, and the actual declarations & references to the const data?
Last edited by alanjhd08; February 9th, 2011 at 04:08 AM.
Reason: Fixed formatting mangled by WYSIWYG editor
-
February 9th, 2011, 04:22 AM
#6
Re: Confused with linking header file in C++
Originally Posted by alanjhd08
Hi,
................
............
A typical way to do it which ensures you use the same constant each time would be to use
Code:
const int someData = 768;
in one .cpp file, and in your header file (as hypheni says)
Code:
extern const int someData;
If you are seeing an error indicating that it's redefined, it seems strange. Can you post the error message, and the actual declarations & references to the const data?
Hi alanjhd08,
Thank you for your help.
I did the same as you and hypheni said.
I declared "const int someData = 6" in header1.h
I declared "extern const int someData in main.cpp
The error is as follows:
Code:
Linking...
main.obj : error LNK2001: unresolved external symbol "int const someData" (?M@@3HB)
Debug/main.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.
expectation.exe - 2 error(s), 0 warning(s)
NB: I am using visual c++ 6.0 to compile.
-
February 9th, 2011, 04:30 AM
#7
Re: Confused with linking header file in C++
Hi,
I just checked with VC++ 6.0, doesn't seem to be any difference.
I think you described the files the wrong way round, you should use "extern" in the header file, and just declare it once in one of the .cpp files. That wouldn't explain the link error you described though, unless you're not including header1.h.
Can you check that you are declaring it as extern in the header?
-
February 9th, 2011, 05:57 AM
#8
Re: Confused with linking header file in C++
Did you remember to use include guards?
const ints don't need to be externed, you can just put it in the header and be done with it.
a simple int though (non const) should be externed though, or you'll have multiple instances (of un-equal value).
Last edited by monarch_dodra; February 9th, 2011 at 06:29 AM.
Is your question related to IO?
Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
-
February 9th, 2011, 06:41 AM
#9
Re: Confused with linking header file in C++
Hi,
I read this is a difference between C & C++, consts at file-scope do not have external linkage in C++, but they did in C.
So in C++, if you want to define const int in your header, you could have:
header1.h
Code:
const int someData = 43;
header2.h
Code:
const int someData = 42;
As long as you don't include both headers, all will work well, and you can use different values for the constant depending on which header you include.
Of course, normally you would just want to use the same value throughout, so would define it once, in one header file.
If you declare it as "extern", then you can only define it once, and will have to use the same value throughout your program. However, choosing where to define it becomes tricky. If you do it in a header, you can only include it once, in one compilation unit, so you are better defining it in a .cpp file. However, for a constant that is used in many places, it may not be clear which .cpp file it should be defined in.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|