linking files

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Avishaiozeri
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 85
Joined: Wed Mar 17, 2010 4:32 pm

Re: linking files

Post by Avishaiozeri »

The linking process isn't the same as the precompiling. the precompiler takes the included files and adds the code in them to the page. that is used to include header files in order for your program to identify and "know" your classes and other things you put in separate pages. thats why only the decorations are in the include files. the actual classes and function are written in the other cpp file, that after compilation linked all together. so the #include function adds code, the linker is a different thing, it links the .obj created after the compilation of each cpp file into one exe file. so what happed to you is simple, the precomiler added your cpp files to your main file, and all of the code was compiled to a obj file, but the other cpp files in your project was also compiled, so when the linker tried to link them all together, it found the exact same code in the main file, so there you have an error...
so you don't need to include cpp files, because they will be added to your program by your linker if they are in the same project. what you do need to include is the declaration of your classes, because otherwise your main function won't know it exists.
hope it helps ;)

and by the way, if i'm wrong, correct me. and if you already got the answer, i'm sorry, i just didn't have the time to check all the comments...lol :)

Edit: i'v read your other problem.. I dunno what's the solution. maybe its something with codeblocks beacuse i never had that problem, when using - #ifndef in VS...
mary
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 33
Joined: Tue Apr 27, 2010 2:13 pm

Re: linking files

Post by mary »

I am getting an error "multiple definition of 'map' ", which is an array. The error seems to include everything that is declared in the same .h file, which mostly declares arrays, map would be declared as "bool map[15];".
User avatar
xiphirx
Chaos Rift Junior
Chaos Rift Junior
Posts: 324
Joined: Mon Mar 22, 2010 3:15 pm
Current Project: ******** (Unkown for the time being)
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Contact:

Re: linking files

Post by xiphirx »

mary wrote:I am getting an error "multiple definition of 'map' ", which is an array. The error seems to include everything that is declared in the same .h file, which mostly declares arrays, map would be declared as "bool map[15];".
ok, now look for map again in your code where you include that header, change or get rid of the variable.

That or make sure you didnt declare it twice in your header
StarCraft II Zerg Strategy, open to all levels of players!

Looking for paid work :< Contact me if you are interested in creating a website, need a web design, or anything else you think I'm capable of :)
mary
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 33
Joined: Tue Apr 27, 2010 2:13 pm

Re: linking files

Post by mary »

if I take it away then an error appears in the .cpp that actually uses the array. I checked and its not declared twice in the header.
mary
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 33
Joined: Tue Apr 27, 2010 2:13 pm

Re: linking files

Post by mary »

I made all the variables extern (extern bool map[15]), and in a differnt folder I declared then did [bool map[15]], and the code seems to work, I have no clue why this would change things.
Post Reply