Multiple .cpp files

Hello all. I am making a program and I have a few headers and then matching .cpp files to define the member functions in the header files. How can I include the other .cpp files in my main.cpp? Or should I just completely declare/define the functions in the header file, and get rid of the other .cpp files?

-tony
You should just include the header files in your main.cpp, then link the different cpp files together.
This process would be made easier if you got an IDE such as Microsoft Visual C++ Express 2010
http://www.microsoft.com/express/Downloads/Download-2010.aspx
or Code::Blocks
http://www.codeblocks.org/
and made a project in it containing all your cpp files.
Thanks Xander, I am actually using Visual Studio 2010. So just having all of the files in the same project automatically includes them? I have the header files defined in my main.cpp; I guess my question then is if I include multiple .cpp files, will the variables in my other .cpp files be recognized in main.cpp?
Last edited on
If they are cpp files they are all compiled and then linked together by the linker into a final executable, yes. However, this should not be considered the same as copying and pasting all the files into one file.

The files cannot necessarily "see" what is in the other files, which is where header files come in. You can put function prototypes and forward declarations, etc. in them and include them in all the files that need those things. Then you define those functions and variables etc. in a particular source file.
thanks for the novel firix. how do I call a function from one .cpp file to my main.cpp file? I know I have the headers which have the declarations, but they do not have the definitions. The header is not linked to its respective .cpp file from the .h file (the .cpp has the .h, not other way around). So although I could call the function, it is a function that does not have a definition, since it is not defined in the header.
As long as it is defined in one of the cpp files, it's fine, provided you have the forward declaration (prototype) of the function in the other cpp file (which you do as you've included the header).
tonyh703 :

thanks for the novel firix. how do I call a function from one .cpp file to my main.cpp file? I know I have the headers which have the declarations, but they do not have the definitions. The header is not linked to its respective .cpp file from the .h file (the .cpp has the .h, not other way around). So although I could call the function, it is a function that does not have a definition, since it is not defined in the header.

What novel ?
you can not read?
Last edited on
Topic archived. No new replies allowed.