We help IT Professionals succeed at work.

Compiling .h and .cpp

mitchell042997
on
Medium Priority
1,153 Views
Last Modified: 2006-11-17
I own Borland's Turbo C++ 3.0 for Windows 3.x.  How do I
compile my .cpp file so that my specification (.h) can link
the obj when I declare #include "myfile.h"?

If that isn't clear enough a question, I want to be able to say:

#include "myfile.h"

and have it include the file:

//--- MYFILE.H

class whatever {
  public:
    whatever();  
    void test();
};

//--------------------------------

then the file MYFILE.CPP:

whatever::whatever()
{  cout << "Whatever";  }

void whatever::test()
{  cout << "Testing whatever...";  }

???  thanks for the input!
Comment
Watch Question

Commented:
This question can be interpreted a few different ways, so I will try to include all.

1) If you mean "How do you use the .h files in a multi-module compilation:  Put your include statement in each .cpp file that uses the class (including the myfile.cpp).

2) If you mean that you want to use just an include without having requiring the linking of the myfile.obj:  Change your class to a template and the linker will merge all instances of the same class - or - If you make all functions inline, you don't need the obj.

3) If you mean you want to automaticaaly include the cpp whaenever the .h is included:  Don't use this; it will cause duplicate symbbol errors when linking.  Re-think your design.

4) I can't tell what else you may be asking.  Clarify if you didn't get your answer.

Author

Commented:
I am speaking of instance #3.  Why not do this?  In the books on C++ programming I read it strongly suggests this.  "One of the most fundamental principles of good software engineering is to separate interface from implementation. ... Place the class declaration in a header file to be included by any client that wants to use the class.  This forms the class's public interface.  Place the definition of the class member functions in a source file.  This forms the implementation of the class."  (Deitel & Deitel, How to Program in C++, pgs. 357,358)

The makers of C++ use it all the time.  #include<iostream>, for example.  I know this can be done.  The same book I mentioned above says this:  "The header files are included (via #include) in each file in which the class is used, and the source-code file is compiled and linked with the file containing the main program.  See your compiler's documentation to determine how to compile and link programs consisting of multiple source files." (pg. 359) I have checked the documentation, but cannot find the information needed, hence my asking here.

I hope this clears up the confusion a bit.  And you can use #ifndef's to make sure you don't cause duplicate symbol errors.

Commented:
You don't want to include the .cpp with each compilation unit.  It's not neccesary.  The compiler creates a .lib or .obj file with the .cpp file and the linker links this into your .exe.  The iostream example is linked through a .lib file; they never "include" a .cpp file, just the .h.  The only reason you need to include the .h file when compiling other modules is so they will know how to access variables and classes in the .cpp file.  They obviously don't need to know how each function is implemented, so they don't need to include the .cpp.


Author

Commented:
Right, right, right.  I know that.  I just don't know how to compile the .cpp into an .obj or .lib and have it automatically link to the .exe I am compiling!  That is my question, sorry if I was vague in asking.  How do you do that?  That is what I am trying to do with very little luck.

Commented:
Oh, sorry, I misinterpreted your question.  

If you are using an ide, add the module to a project.

If you are using a command line compiler, you can manually compile each module and then link them all with the linker (link module1.obj module2.obj etc...) Or, you can use a "make" program by building a .mak script and running "make program.mak"

There is no other way the linker can tell what modules to link up.

Author

Commented:
How does Turbo C++ do it, then, with all of it's .h files, like iostream.h, stdio.h, windows.h, etc.?

Commented:
They don't show you the run time .lib modules on the project view.  Actually this is an option.  Go to options/environment/project view and check "show run-time nodes" and you will see all the libbs in the project.  I have 5.02, but it is close if not the same.

Commented:
If my answer is insufficient, you should give me an F so your question will go back to the queue to be answered by others.
Engineer
Commented:
The Turbo C++ for Windows compiler does automatically include the various libraries. This is done because of the options you have set. For example, if you say that you are building a Windows program, then the IDE automatically adss in the proper libraries for a Windows program. It is NOT a feature of the compiler nor the linker. Ultimately, the linker MUST know what libraries to link in or else you will get an undefined symbol.

But by only including a header file, you cannot automatically force the linker to pull in a library or .obj file, at least not with Turbo C++ for Windows. Microsoft Visual C++ has a #pragma option that can do that.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Commented:
That's what I was trying to say.

Author

Commented:
OK.  If it is not possible, then I guess I will have to work around it.  One would think, though, that if Turbo C++ 3.0 for DOS can do it, Turbo C++ 3.1 for Windows could do it too.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.