Solved

Compiling .h and .cpp

Posted on 1997-07-18
11
1,105 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 2

Author Comment

by:mitchell042997
Comment Utility
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
Comment Utility
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 2

Author Comment

by:mitchell042997
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
That's what I was trying to say.
0
 
LVL 2

Author Comment

by:mitchell042997
Comment Utility
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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 be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now