I can't seem to understand how multi-file projects work. Are all the files internally combined into one, or do they all include each other...? Does every file need a main()?
I can't seem to understand how multi-file projects work. Are all the files internally combined into one, or do they all include each other...? Does every file need a main()?
Multi-file projects I think "link" to each other. No, each doesn't have to have a main. If you wanted one to call functions from it would be like this:
// file1.cpp
int add(int a, int b)
{
return a + b;
}
// mainfile.cpp
#include <iostream.h>
#include "file1.cpp"
int main()
{
cout << add(5, 5);
return 0;
}
You can also do this:
// File2.cpp
int add(int a, int b)
{
return a + b;
}
// File1.cpp
#include <iostream.h>
#include "File2.cpp"
int out()
{
cout << add(5, 5);
}
// mainfile.cpp
#include <iostream.h>
#include "File1.cpp"
int main()
{
out();
return 0;
}
This isn't a good explanation, but I hope it helps some.
Website(s): http://www16.brinkster.com/trifaze/
E-mail: [email protected]
---------------------------------
C++ Environment: MSVC++ 6.0; Dev-C++ 4.0/4.1
DirectX Version: 9.0b
DX SDK: DirectX 8.1 SDK
Each .cpp file is compiled into it's own .obj file. The linker then combines these files into your .exe file.
Headers are used/included in your code to let the compiler know that you are accessing functionality that is outside the current file scope... in which case it knows that it will be linked in at a later date (provided that you have actually written that code, or have a library elsewhere).
No... you only need one main() function... you program needs ONE entry point so it knows where to start, this is the main() function... so having more than one in your project would give you an error.
Hope this helps.
U.
Quidquid latine dictum sit, altum sonatur.
Whatever is said in Latin sounds profound.
So lets say I wanted to make a program with Networking code in one file and the main code in the other. Would I set it up like this?
-------------------------------------------------
//network.cpp
#include <whatever.h>
void connect(){
//whatever
}
// gui.cpp
#include <whateverelse.h>
void main(){
connect();
}
-------------------------------------------------
And once its found the entry point, where do I go from there? Let's say the user has chosen to connect to the network, so how do I get it so the networking file "takes over"? Sorry, I'm a PHP coder, not used to compiled languages
As long as the prototypes are global, use conditional compliation, all will work.
Conditional Compilation is #including a header in all files, but by using #ifndef statements ensures it is complied only once into the project.
ie
prototypes.h
#ifndef MY_APPS_PROTOS
#define MY_APPS_PROTOS
//all the prototypes go here
#endif
On build the compiler will link all into one exe.
"Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
Friedrich Nietzsche
"I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
George Best
"If you are going through hell....keep going."
Winston Churchill