Hey,
Can someone tell me how i can include source files. Like with header files you can do
#include "headerfile.h"
I want to put functions in the second source file, but i have no idea how to call them.
Printable View
Hey,
Can someone tell me how i can include source files. Like with header files you can do
#include "headerfile.h"
I want to put functions in the second source file, but i have no idea how to call them.
you can't.
make a header file with the function prototypes and include the header file in teh source file. then you can include the header file wherever you want to call the functions.
You can....?? I see it all the time in other projects. They have lots of source files included in their project. How do they get the functions from one source to another. Im sure its posible somehow.
not as far as i know... i could be wrong (but i am 99.999999999999999999999999999999999% sure i'm not!).. i have never done that..
header:
source:Code://header.h
void test();
void test2();
keep in mind:Code:include "header.h"
//implement function
file A.h
file B.h
file C.h
file A.cpp
file B.cpp
file C.cpp
you have:
A:
include B.h
B:
include C.h
and you have in another file
include A.h
if you have only functions, you will be able to access functions in A, B and C.
You would declare the function in a header file. Like this -Quote:
Originally Posted by Trainwreck
You would define these functions in a cpp file that includes this header:Quote:
Inside SomeHeader.h -
Code:void TestFunc (void);
Now, to use TestFunc in AnotherCPP.cpp, you would include SomeHeader.hQuote:
Inside SomeHeader.cpp -
Code:#include "SomeHeader.h"
void TestFunc (void)
{
std::cout << "Executing TestFunc" << std::endl;
}
Like this -
Quote:
Inside AnotherCPP.cpp -
Code:#include "SomeHeader.h"
And for all this to work fine, you must make sure that these files in question (i.e. SomeHeader.h, SomeHeader.cpp and AnotherCPP.cpp) are included into the project.Quote:
Code:void CallTestFunc ()
{
TestFunc ();
}
You would include a file into the project by right-clicking on the Project, and then selecting -
- "Add New Item" to add a new item OR
- "Add Existing Item" to add an existing .cpp or .h file.
Ok... I think what the other poster are saying is correct about the way we 'should' use #include. But: I really think #include is just a preprocessor directive and all it really does is include the 'text' of the file into the source file so you can have source code included via the include directive. In fact a long time ago I had some snippets of code that I did include that way. Maybe not the 'correct' way to do things but it worked.
I'm not sugesting this is a good idea... but if your reading someone else code and see this... it can be done.