Advertisement

Please tell me what i'm doing wrong!

Started by September 10, 2001 02:06 AM
5 comments, last by _Titan_ 23 years, 5 months ago
I am having trouble putting different code in seperate .cpp files and compiling the program. I have a Textures.cpp and Text.cpp a globals.h and the main.cpp I include the globals.h in the main.cpp but when it compiles, text.cpp and textures.cpp cant find the global variables. So, if i include the globals.h in each .cpp file, it gives me an error, saying the global variables are already defined in main.cpp. how do i get my code in seperate .cpp files in one project and make it work? It just wont work for me! What am i doing wrong?
You should declare the variables as

extern data_type name

in the other .cpp files. Including the header file in all files is more or less the same thing as pasting the code in the header file at the top of the code files and the result is that the compiler tries to declare the variables in globals.h for each code file. Since you obviously can't declare to variables with the same identifier you'll get an error. With extern you tell the compiler to use an existing variable instead.


Snale
+--My humble and superior homepage

Edited by - Snale on September 10, 2001 3:16:35 AM
Advertisement
Hi,

I''ll suggest you and alternative. You can use preprocessor''s directives to tell the compiler you only want your .h included
once.

At the beginning to each header you can put something like:

#ifndef _YOUR_HEADER_NAME_H_
#define _YOUR_HEADER_NAME_H_
...

...
#endif

With this trick your .h file will be only included once even if you #include it in more than one source. You can import all the
variables you need in the various source files with the extern
directive, as suggested by Snale.

Hope it helps,


Fabio "SnowDruid" Franchello
--- snowdruid@libero.it ---
Fabio "SnowDruid" Franchello--- snowdruid@libero.it ---
Snow druid, while that is good practice it doesnt ALWAYS work when it comes to variables. I recommend that he uses an extern system... here is an example in case you are still confused.

header.h
-extern int foo; // here we declare it
stuff.cpp
-extern int foo; // here we acknowledge it so we can use it in
// this file
main.cpp
-int foo; // here we define it

make sense? Hope so...
Ok, to test the extern theory, i made a project with main.cpp other.cpp and vars.h

other.cpp has a function that just displays some text. I made it display NUMBER, its a variable stored in vars.h I declared

extern int number;

i did just like you said, but i keep getting this error now
error LNK2001: unresolved external symbol "int number" (?number@@3HA)

y?
It''s really simple:


"extern int variable" is placed in a .h file that is included in ALL of your .cpp files.


"int variable" is placed in your main.cpp file.


explanation:

when you #include it''s just like having cut and paste that into your .cpp. If you fail to declare a variable at all, it''ll complain about it. If you declare the same variable multiple times, it''ll complain about that as well when you link them. Using "extern" means that you''re declaring the variable, but telling the compiler that it''s from another .cpp file. Obviously you can''t have all your .cpp files declare the variable as "extern" or it''ll come back undefined (like you''re getting now) because all the .cpp''s are saying it''s defined elsewhere. So ONE file needs to actually declare without the extern part. You CAN define as "extern int" and then "int" later in the same .cpp without a problem -- that is, include the same .h file everything else does (with the extern''s) and then go ahead and redeclare as "int".
Advertisement
Hey thnx man, that explains it more clearly. I was a little foggy about where things went...duh! Ok, i feel stupid now.

Well, that said an learned, lets see if i can get it working this time eh!

This topic is closed to new replies.

Advertisement