|
-
May 12th, 2001, 06:28 PM
#1
Thread Starter
Frenzied Member
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.
-
May 12th, 2001, 06:37 PM
#2
Frenzied Member
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."
-
May 12th, 2001, 07:02 PM
#3
Thread Starter
Frenzied Member
Like how would I use a class that's on the second source code?
-
May 12th, 2001, 07:08 PM
#4
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.
-
May 12th, 2001, 07:12 PM
#5
Frenzied Member
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."
-
May 12th, 2001, 07:20 PM
#6
Frenzied Member
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."
-
May 12th, 2001, 11:10 PM
#7
I know what you're talking about Harry. Its disgusting.
Z.
-
May 13th, 2001, 07:09 AM
#8
Member
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.
-
May 13th, 2001, 08:43 AM
#9
Monday Morning Lunatic
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|