How to use multiple source files
Author |
Message |
Quakerstate98
|
Posted: 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 |
|
|
|
|
|
Sponsor Sponsor
|
|
|
DtY
|
Posted: 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:
code: | #include <string>
#ifndef __secondsource_h
#define __secondsource_h
void registerUser(string username);
#endiff |
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:
code: | #include <string>
#include "secondsource.h"
void registerUser(string username) {
/* the actual function */
} |
When you want to use these functions, you include only the header file:
code: | /* some stuff */
#include "secondsource.h"
/* some more stuff */ |
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 |
|
|
|
|
|
bbi5291
|
Posted: 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? |
|
|
|
|
|
DtY
|
Posted: 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? I'm not sure, c++ isn't *guaranteed* to be able to link against templates, but it doesn't violate the standard if a compiler can. As far as I know though, no current compilers can do this.
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. |
|
|
|
|
|
bbi5291
|
Posted: 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? I'm not sure, c++ isn't *guaranteed* to be able to link against templates, but it doesn't violate the standard if a compiler can. As far as I know though, no current compilers can do this.
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. No, it doesn't violate the C++ standard. However, one of the greatest strengths of the linker is that it can link together code produced by different compilers and written in different languages --- at the object level, all code is the same. It can only do this if the object file has a standardized format, which necessarily implies a standardized encoding for exposing interfaces. If it is to be standardized, language-specific features such as templates cannot be incorporated (although perhaps in the future some simplified way of representing generics drawing on common ground from various languages will be incorporated into a new object file format). What you said makes sense: templates probably automatically become static on compilation. |
|
|
|
|
|
ScaryRat
|
Posted: 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:
code: | #include <string>
#ifndef __secondsource_h
#define __secondsource_h
void registerUser(string username);
#endiff |
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:
code: | #include <string>
#include "secondsource.h"
void registerUser(string username) {
/* the actual function */
} |
When you want to use these functions, you include only the header file:
code: | /* some stuff */
#include "secondsource.h"
/* some more stuff */ |
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? |
|
|
|
|
|
|
|