Results 1 to 9 of 9

Thread: More than one source file

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jul 1999
    Posts
    1,800

    More than one source file

    How would I link two source files together in a project? Because I would prefer to keep my class in a seperate file.

  2. #2
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    Just add both source files to the project and the compiler should sort out the grouping them together thing (It will if you're using VC++ anyway). If you want to use a variable defined in one source file in another source file you'll need to 'extern' it, if ya know what I mean.
    Harry.

    "From one thing, know ten thousand things."

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jul 1999
    Posts
    1,800
    Like how would I use a class that's on the second source code?

  4. #4
    Zaei
    Guest
    If you have your main source file, we'll call it "main.cpp" and say you have your class file, called "class.h". at the top of your "main.cpp", do "#include "class.h"". Basically what this does is, at compile time, takes the entire contents of "class.h", and sticks it where you put that "#include" statement. If you called the file with your class in it "class.cpp", you CANNOT have that file also in your project. If you do, itll give you errors. Just remove it from the project, and "#include "class.cpp"".

    Z.

  5. #5
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    Well it's usually a good idea to put class definitions in header files and include the header file in whatever needs to use it. Just put the usual #ifndef, #define, #endif compiler directives in to make sure that it doesn't include a header more than once. I hope you know what I'm talking about... if not just say so.

    Generally you should put definitions in header files, and code and declarations on source files. So you only put the class definition in the header file, with just declarations of the member variables and functions. Then in a source file to go with the header file, you put the definitions of each of the member functions of the class.
    Harry.

    "From one thing, know ten thousand things."

  6. #6
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    Ugh, I hate #including .c and .cpp files, it's just ugly... seems like some kind of sacrilege... I think you should only #include header files.
    Harry.

    "From one thing, know ten thousand things."

  7. #7
    Zaei
    Guest
    I know what you're talking about Harry. Its disgusting.

    Z.

  8. #8
    Member
    Join Date
    May 2001
    Location
    US Of A
    Posts
    41
    that is why you have you class def in the header file and the class coe in a cpp file alot easyer, and in the class.cpp file you include the class.h file that easy and then you call the class.h file and it will do the rest.

  9. #9
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Yep

    Although I usually put inlined functions in the the .h file as well (there's a split over whether to do this or put them in the .cpp file, but I do it this way to prevent me forgetting to put the extra inline into the source file and buggering the compiler over).
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

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