CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Aug 2005
    Posts
    134

    including source files

    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.

  2. #2
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    628

    Re: including source files

    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.

  3. #3
    Join Date
    Aug 2005
    Posts
    134

    Re: including source files

    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.

  4. #4
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    628

    Re: including source files

    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:

    Code:
    //header.h
    void test();
    void test2();
    source:

    Code:
    include "header.h"
    //implement function
    keep in mind:

    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.
    Last edited by drewdaman; August 10th, 2005 at 02:53 PM.

  5. #5
    Join Date
    Oct 2002
    Location
    Germany
    Posts
    6,205

    Re: including source files

    Quote Originally Posted by Trainwreck
    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 would declare the function in a header file. Like this -

    Inside SomeHeader.h -
    Code:
    void TestFunc (void);
    You would define these functions in a cpp file that includes this header:
    Inside SomeHeader.cpp -
    Code:
    #include "SomeHeader.h"
      void TestFunc (void)
       {
       std::cout << "Executing TestFunc" << std::endl;
        }
    Now, to use TestFunc in AnotherCPP.cpp, you would include SomeHeader.h

    Like this -
    Inside AnotherCPP.cpp -
    Code:
     #include "SomeHeader.h"
    Code:
       void CallTestFunc ()
       {
       	TestFunc ();
     }
    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.

    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.
    Last edited by Siddhartha; August 10th, 2005 at 04:04 PM. Reason: clarification...

  6. #6
    Join Date
    Feb 2001
    Location
    TN
    Posts
    290

    Re: including source files

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured