Solved

Compiling .h and .cpp

Posted on 1997-07-18
11
1,108 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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