Archived

This topic is now archived and is closed to further replies.

DanG

#include <(class name).h> without being in project?

Recommended Posts

I''m writing a few utility classes using MSVC6. In all of my projects I want to be able to write #include <(the class name).h> and thats all. The prblem is that if I just put the .h and .cpp in MSVC include directory. It doesn''t work. I get unresolved external symbols errors. To make it work I have to bring it into the project, where it is compiled and clutters the workspace. All the standard headers, like iostream and mfc stuff, you can just write #include on the .h and all the classes work just fine, without being compiled or borught into the workspace. How do I do this?

Share this post


Link to post
Share on other sites
You need to compile the class into a library, link to that library (in the project settings) then you can simply #include the .h file and use the class as you wish.

Obviously it pays to group code together into utility libraries instead of just putting one class into one library, but there''s no reason why you can''t do that - it''s just a pain to link to them.

Cheers

Matt

Share this post


Link to post
Share on other sites
Then how does this program work?

#include <iostream.h>
#include <fstream.h>

int main(int argc, char * argv[])
{
cout << "Hello World" << endl;
ofstream Text_File("Hello.txt");
Text_File << "Hello WOrld" << endl;
return 0;
}

That will compile. Notice it uses the classes ostream and ofstream, predefined instance cout and endl. I never brought any library into the project, just included the headers, yet they work just fine. How can I do that?

Share this post


Link to post
Share on other sites
quote:
Original post by DanG
I never brought any library into the project ...

Ah, but you did. There are a number of libraries which are linked by default - including libc and msvcrt, depending on your project settings. You need to follow 3dModelMan''s advice, and create a library. Then you just have to #include the library''s header, and link to the library. No need to bring the individual source files into the project.

Share this post


Link to post
Share on other sites
Alright, I made a libary of the source i wanted put the .h where it goes. It works, but I still have to add the library manually. I found that if I write

#pragma comment(lib, "(class name).lib")

In the .h I don''t have to manually add the library. Is this OK coding style?

Share this post


Link to post
Share on other sites
If you go to Project, then Settings, then the C/C++ tab, and choose Preprocessor in the Category dialog box, you''ll get a field where you can enter include directories. Is that what you''re looking for?

Share this post


Link to post
Share on other sites