Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

linkage problem

This topic is 6539 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
Or just put

#pragma once

at the top of every header file. This only works in windows thought, but it''s hellaconvenient.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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.





Share this post


Link to post
Share on other sites
Guest Anonymous Poster
terms declaration and definition are swapped in previous post for sure

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!