Solved

Compiling .h and .cpp

Posted on 1997-07-18
11
1,112 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

739 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