Computer Science Canada How to use multiple source files |
Author: | Quakerstate98 [ Thu Sep 02, 2010 7:34 pm ] |
Post subject: | How to use multiple source files |
Im working on a database program and some of my functions are getting pretty big and its getting confusing having everything in one .cpp file. So if i wanted to take one of my functions called registerUser(string username) and put it in a second file in the solution how do I call it or w/e. Ive tried includes for example #include "secondSource.cpp". Im using Visual Studios 2008 |
Author: | DtY [ Thu Sep 02, 2010 8:03 pm ] | ||||||
Post subject: | RE:How to use multiple source files | ||||||
Including the file like that *should* work, but it's not the *right* way. You want to create two files, and Visual Studios will have some way to create these both at the same time, and add them (I'm not sure how, if you can't figure it out, maybe someone with Visual Studios experience can help). The first file is called a header file, it will have the same name with the .h extension. All this has is the function outlines, not what they actually do, for example:
The three lines; ifndef, define and endif are called include guards, these just protect the file against being included more than one (if it's included twice, it will ignore the function definitions subsequent times). The other file is the actual source file, it would look like this:
When you want to use these functions, you include only the header file:
Just so the compiler knows about the functions when it compiles, it doesn't compile the functions in right away. The compiler will compile each file separately, and then runs the linker. The linker links all your compiled files together to make the final executable (the .exe on windows). Assuming you added the file properly to the project, it will compile both and then link them together. The reason this is better is because the compiler is slow, if you modify one file, it will only compile that file, and then link it against what has already been compiled, if you included the function definitions directly, it would have to compile both files every time either one was changed. (note that if you edit the header file (adding a new function or such), it will need to recompile any file that included it). Functions and classes should go in the source (.cpp) file, and only the definitions should go in the header (.h). EXCEPT FOR templates, which have to be put completely in the header; this is just a quark of C++, it cannot link against a template in another file. Here's some more info on it: http://www.ensta.fr/~diam/c++/online/notes-cpp/basics/multiple-files.html |
Author: | bbi5291 [ Thu Sep 02, 2010 11:28 pm ] |
Post subject: | Re: RE:How to use multiple source files |
Quote: Functions and classes should go in the source (.cpp) file, and only the definitions should go in the header (.h). EXCEPT FOR templates, which have to be put completely in the header; this is just a quark of C++, it cannot link against a template in another file. This is because object file formats are standardized, and cannot support language-specific features such as templates. What would happen if a non-C++ program tried to link against that object, anyway? |
Author: | DtY [ Fri Sep 03, 2010 7:55 am ] |
Post subject: | Re: RE:How to use multiple source files |
bbi5291 @ Thu Sep 02, 2010 11:28 pm wrote: Quote: Functions and classes should go in the source (.cpp) file, and only the definitions should go in the header (.h). EXCEPT FOR templates, which have to be put completely in the header; this is just a quark of C++, it cannot link against a template in another file. This is because object file formats are standardized, and cannot support language-specific features such as templates. What would happen if a non-C++ program tried to link against that object, anyway?If you did though (on a compiler that doesn't support it), the template functions and classes would probably automatically become static, so you can't access them from other objects. |
Author: | bbi5291 [ Fri Sep 03, 2010 6:14 pm ] |
Post subject: | Re: RE:How to use multiple source files |
DtY @ Fri Sep 03, 2010 7:55 am wrote: bbi5291 @ Thu Sep 02, 2010 11:28 pm wrote: Quote: Functions and classes should go in the source (.cpp) file, and only the definitions should go in the header (.h). EXCEPT FOR templates, which have to be put completely in the header; this is just a quark of C++, it cannot link against a template in another file. This is because object file formats are standardized, and cannot support language-specific features such as templates. What would happen if a non-C++ program tried to link against that object, anyway?If you did though (on a compiler that doesn't support it), the template functions and classes would probably automatically become static, so you can't access them from other objects. |
Author: | ScaryRat [ Sun Sep 05, 2010 10:47 am ] | ||||||
Post subject: | Re: RE:How to use multiple source files | ||||||
DtY @ Thu Sep 02, 2010 8:03 pm wrote: Including the file like that *should* work, but it's not the *right* way.
You want to create two files, and Visual Studios will have some way to create these both at the same time, and add them (I'm not sure how, if you can't figure it out, maybe someone with Visual Studios experience can help). The first file is called a header file, it will have the same name with the .h extension. All this has is the function outlines, not what they actually do, for example:
The three lines; ifndef, define and endif are called include guards, these just protect the file against being included more than one (if it's included twice, it will ignore the function definitions subsequent times). The other file is the actual source file, it would look like this:
When you want to use these functions, you include only the header file:
Just so the compiler knows about the functions when it compiles, it doesn't compile the functions in right away. The compiler will compile each file separately, and then runs the linker. The linker links all your compiled files together to make the final executable (the .exe on windows). Assuming you added the file properly to the project, it will compile both and then link them together. The reason this is better is because the compiler is slow, if you modify one file, it will only compile that file, and then link it against what has already been compiled, if you included the function definitions directly, it would have to compile both files every time either one was changed. (note that if you edit the header file (adding a new function or such), it will need to recompile any file that included it). Functions and classes should go in the source (.cpp) file, and only the definitions should go in the header (.h). EXCEPT FOR templates, which have to be put completely in the header; this is just a quark of C++, it cannot link against a template in another file. Here's some more info on it: http://www.ensta.fr/~diam/c++/online/notes-cpp/basics/multiple-files.html What if there is a default declaration in the function such as: "registeruser(string username = "a") { }", then when I tried to use the prototype: "registeruser(string username = "a");" in the header file and compile, it says "redefinition of default parameter".so when i call the function, i had to enter all its parameters, is there any way to get around this? |