Solved

Compiling .h and .cpp

Posted on 1997-07-18
11
1,109 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!
0
Comment
Question by:mitchell042997
  • 6
  • 4
11 Comments
 
LVL 4

Expert Comment

by:md041797
ID: 1165370
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.
0
 
LVL 2

Author Comment

by:mitchell042997
ID: 1165371
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.
0
 
LVL 4

Expert Comment

by:md041797
ID: 1165372
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.


0
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 
LVL 2

Author Comment

by:mitchell042997
ID: 1165373
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.
0
 
LVL 4

Expert Comment

by:md041797
ID: 1165374
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.
0
 
LVL 2

Author Comment

by:mitchell042997
ID: 1165375
How does Turbo C++ do it, then, with all of it's .h files, like iostream.h, stdio.h, windows.h, etc.?
0
 
LVL 4

Expert Comment

by:md041797
ID: 1165376
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.
0
 
LVL 4

Expert Comment

by:md041797
ID: 1165377
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.
0
 
LVL 15

Accepted Solution

by:
Tommy Hui earned 50 total points
ID: 1165378
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.

0
 
LVL 4

Expert Comment

by:md041797
ID: 1165379
That's what I was trying to say.
0
 
LVL 2

Author Comment

by:mitchell042997
ID: 1165380
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.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question