linkage problem

Started by
5 comments, last by AnCcE 23 years, 10 months ago
Okay say i have file.cpp. It goes something like this: #include "file.h" int main() { do_something_here_with_variable_abc(); return 0; } I also have file.h. It looks like this: int number; char character; double decimal; and on and on and on (basically a bunch of variables made). Problem is, when I include file.h into one of my .cpp files, everything works fine, but when i include it into a second .cpp, all heck breaks loose in the form of redefinition and linkage errors. I don''t quite get what''s happening here. Anybody care to enlighten me? Also, would it help if I defined number, character,and decimal in a separate .cpp file with extern linkage? Hi there, I''m AnCcE, and you''d be too if you weren''t somebody else.
Hi there, I''m AnCcE, and you''d be too if you weren''t somebody else.
Advertisement
To make sure that header files are not included several times, add something like this to the top of it:

#ifndef _Header_File_Name_Included_
#define _Header_File_Name_Included_

And add

#endif

to the bottom of the header.

- Muzzafarath

Mad House Software
The Field Marshals
I'm reminded of the day my daughter came in, looked over my shoulder at some Perl 4 code, and said, "What is that, swearing?" - Larry Wall
Or just put

#pragma once

at the top of every header file. This only works in windows thought, but it''s hellaconvenient.
I''ve tried both of those but I''m still getting link error 2005 in VC6 when I include in multiple files. Anything else I should try before I lose my mind over this?

Hi there, I''m AnCcE, and you''d be too if you weren''t somebody else.
Hi there, I''m AnCcE, and you''d be too if you weren''t somebody else.
Got MSDN? (Just like the Got Milk? ads)

search for: __declspec( selectany )
...or just go to "selectany" keyword in the index

... or you can just use the C++ "extern" keyword


I think your problem is that each time you include the .h file in a .cpp file, you are declaring another instance of the variable that gets put in the .obj. So when you link, there are multiple instances. To fix this, instantiate them in only one of the .cpp files. Then use the "extern" keyword in the header file to make them declared elsewhere (but not instantiated).

Warning: global variables can cause bad cohesion.
When you have a pile of global variables and a whole bunch of .cpp files that depend on them (I think academic experts call this "data coupling"), you're going to banging your on the keyboard to debug your program.

If all else fails, try Project Settings, click on the "Link" tab. Select "Customize" from the "Catagory" pulldown. And check the "Force file output" checkbox. Now you can build your project even with the errors. Good Luck debugging it and have fun rebooting you computer when it crashes.

Edited by - Marsupial Rodentia on June 26, 2000 1:29:07 PM
quote:Original post by AnCcE
I also have file.h. It looks like this:

int number;

Its a BAD practice to DECLARE anything in header files. DO put DEFINITIONS there but never DECLARATIONS. headers are supposed to contain ONLY DEFINITIONS.
extern int number; //definition
int number; //declaration
int MyFunc(); //definition
int MyFunc() {}; //declaration

quote:Original post by Marsupial Rodentia
... or you can just use the C++ "extern" keyword

extern is NOT A C++ keyword, its C.





terms declaration and definition are swapped in previous post for sure

This topic is closed to new replies.

Advertisement