Multiple cpp files in MSVC 6.0
I am working on a large project and wish to put code blocks into multiple cpp files for a project. Everything i have tried doesnt seem to work and all the books i have read all seem to take it for granted one knows how to do this. I have tried using other source code with multiple cpp files from various tutorials but nothing in do seems to work. If anyone can give me some basic instructions on the steps neccassary I would appreciate it.
Well, if you''re going to have a function in one .cpp file called by another .cpp file, you need some way of letting the second .cpp file know about functions in the first. So what you might want to do is, make a common header that is to be included by each .cpp file, containing the function definitions for all of your functions. And I think you need to make them all EXTERN.
Martee
Magnum Games
Martee
Magnum Games
ReactOS - an Open-source operating system compatible with Windows NT apps and drivers
Here's an example:
Basically, you always have to declare a function if it's called before you define it. The declaration is simply the function return value, name, and parameter list, as well as extra stuff like const, with a semicolon after it. Nothing more. Parameter names are optional, but you must specify the types. As long as a function is declared before you call it, the file will compile. Then, after both files (Functions.cpp and Main.cpp) have compiled, when they are linked, the linker will see that a function "Func1" has indeed been defined, and will call it correctly.
Hope this was clear.
Edited by - IdEstVita on July 29, 2000 12:27:11 AM
// Functions.cppvoid Func1(int a, int b){ // ... code goes here ...}// Functions.hvoid Func1(int,int); // param names are optional// Main.cpp#include "Functions.h"int main(){ Func1(1,2); return 0;}
Basically, you always have to declare a function if it's called before you define it. The declaration is simply the function return value, name, and parameter list, as well as extra stuff like const, with a semicolon after it. Nothing more. Parameter names are optional, but you must specify the types. As long as a function is declared before you call it, the file will compile. Then, after both files (Functions.cpp and Main.cpp) have compiled, when they are linked, the linker will see that a function "Func1" has indeed been defined, and will call it correctly.
Hope this was clear.
Edited by - IdEstVita on July 29, 2000 12:27:11 AM
Someone correct me if I am wrong,
But on the header file (e.g. functions.h), shouldn''t you remember to include
#ifndef FUNCTION_H
#define FUNCTION_H
#endif
since you will be including this multiple times, it sounds like?
Mihkael
But on the header file (e.g. functions.h), shouldn''t you remember to include
#ifndef FUNCTION_H
#define FUNCTION_H
#endif
since you will be including this multiple times, it sounds like?
Mihkael
And excuse me if I am wrong as well, but if you use void Func1(int,int); would it still work? Isn''t it extern void Func1(int, int);? Due to the fact that you are possibly calling it from another .cpp file.
-Chris Bennett ("Insanity" of Dwarfsoft)
Check our site:
http://www.crosswinds.net/~dwarfsoft/
Check out our NPC AI Mailing List :
http://www.egroups.com/group/NPCAI/
made due to popular demand here at GDNet :)
-Chris Bennett ("Insanity" of Dwarfsoft)
Check our site:
http://www.crosswinds.net/~dwarfsoft/
Check out our NPC AI Mailing List :
http://www.egroups.com/group/NPCAI/
made due to popular demand here at GDNet :)
dwarfsoft: yes I don''t use extern for anything other than variables....
Great Milenko
Great Milenko
Words Of Wisdom:
"Never Stick A Pretzel In Your Butt It Might Break Off In There."
http://www.crosswinds.net/~milenko
http://www.crosswinds.net/~pirotech
The Great Milenko"Don't stick a pretzel up your ass, it might get stuck in there.""Computer Programming is findding the right wrench to hammer in the correct screw."
Yes, Mikhael, I forgot about that, you should put an
#ifndef FUNCTIONS_H
#define FUNCTIONS_H
..
#endif
around the header code.
And dwarf, the extern was required in C, but in C++ you don''t need it.
#ifndef FUNCTIONS_H
#define FUNCTIONS_H
..
#endif
around the header code.
And dwarf, the extern was required in C, but in C++ you don''t need it.
Hmm....if I see this thread I remember a problem I have very often and my question might be very stupid(excuse if so):
If I have matrix.h(containing defs for my matrix-class) and a vector.h(containing defs for my vector-class) and I want to code a function which multiplies a vector by a matrix(coded in vector.h) and a function which multiplies a matrix by a vector
I don´t know how to do it ´cause if I link the files this way:
matrix.h:
#include"vector.h"
stuff which uses vectore-defs
vector.h:
#include"matrix.h"
stuff which uses matrix-defs
I get a compliler error "nested Function...compiler limit reached"(Sorry don´t remember the error exactly)
So is the only way to solve this to make a third header which has the typdef´s for matrices+vertices in it?At least it works this way but I think this isn´t very nice....
Any ideas?
Greets,XBTC!
If I have matrix.h(containing defs for my matrix-class) and a vector.h(containing defs for my vector-class) and I want to code a function which multiplies a vector by a matrix(coded in vector.h) and a function which multiplies a matrix by a vector
I don´t know how to do it ´cause if I link the files this way:
matrix.h:
#include"vector.h"
stuff which uses vectore-defs
vector.h:
#include"matrix.h"
stuff which uses matrix-defs
I get a compliler error "nested Function...compiler limit reached"(Sorry don´t remember the error exactly)
So is the only way to solve this to make a third header which has the typdef´s for matrices+vertices in it?At least it works this way but I think this isn´t very nice....
Any ideas?
Greets,XBTC!
To do it, you need to have a third one called MatXVect or something and you include the two and do the two multiplications. At least this is the only thing I can think of with such a small amount of sleep over such a large space of time
Oh yeah, extern was for variables, Sorry.. I need sleep
-Chris Bennett ("Insanity" of Dwarfsoft)
Check our site:
http://www.crosswinds.net/~dwarfsoft/
Check out our NPC AI Mailing List :
http://www.egroups.com/group/NPCAI/
made due to popular demand here at GDNet :)
Oh yeah, extern was for variables, Sorry.. I need sleep
-Chris Bennett ("Insanity" of Dwarfsoft)
Check our site:
http://www.crosswinds.net/~dwarfsoft/
Check out our NPC AI Mailing List :
http://www.egroups.com/group/NPCAI/
made due to popular demand here at GDNet :)
Thanx for replying!
But Is the solution that dwarfsoft tells(the same that I already use) the only way?`Cause this has forced me to do really ugly things in the past(mixing two completly different things in ONE header) and there has to be a way to avoid this....
Thanx in advance!
Greets,XBTC!
But Is the solution that dwarfsoft tells(the same that I already use) the only way?`Cause this has forced me to do really ugly things in the past(mixing two completly different things in ONE header) and there has to be a way to avoid this....
Thanx in advance!
Greets,XBTC!
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement