PCH Sample and Makefile

I'm stutdying about PCH files.  I have this sample makefile.

Sample Makefile for PCH

# Makefile : Illustrates the effective use of precompiled
#            headers in a project
# Usage:     NMAKE option
# option:    DEBUG=[0|1]
#            (DEBUG not defined is equivalent to DEBUG=0)
#
OBJS = myapp.obj applib.obj
# List all stable header files in the STABLEHDRS macro.
STABLEHDRS = stable.h another.h
# List the final header file to be precompiled here:
BOUNDRY = stable.h
# List header files under development here:
UNSTABLEHDRS = unstable.h
# List all compiler options common to both debug and final
# versions of your code here:
CLFLAGS = /c /W3
# List all linker options common to both debug and final
# versions of your code here:
LINKFLAGS = /NOD /ONERROR:NOEXE
!IF "$(DEBUG)" == "1"
CLFLAGS   = /D_DEBUG $(CLFLAGS) /Od /Zi /f
LINKFLAGS = $(LINKFLAGS) /COD
LIBS      = slibce
!ELSE
CLFLAGS   = $(CLFLAGS) /Oselg /Gs
LINKFLAGS = $(LINKFLAGS)
LIBS      = slibce
!ENDIF
myapp.exe: $(OBJS)
    link $(LINKFLAGS) @<<
$(OBJS), myapp, NUL, $(LIBS), NUL;
<<
# Compile myapp
myapp.obj  : myapp.cpp $(UNSTABLEHDRS)  stable.pch
    $(CPP) $(CLFLAGS) /Yu$(BOUNDRY)    myapp.cpp
# Compile applib
applib.obj : applib.cpp $(UNSTABLEHDRS) stable.pch
    $(CPP) $(CLFLAGS) /Yu$(BOUNDRY)    applib.cpp
# Compile headers
stable.pch : $(STABLEHDRS)
    $(CPP) $(CLFLAGS) /Yc$(BOUNDRY)    applib.cpp myapp.cpp

Open in new window



And I have this

Example Code for PCH
// ANOTHER.H : Contains the interface to code that is not
//             likely to change.
//
#ifndef --ANOTHER_H
#define --ANOTHER_H
#include<iostream>
void savemoretime( void );
#endif // --ANOTHER_H

// STABLE.H : Contains the interface to code that is not likely
//            to change. List code that is likely to change 
//            in the makefile's STABLEHDRS macro.
//
#ifndef --STABLE_H
#define --STABLE_H
#include<iostream>
void savetime( void );
#endif // --STABLE_H

// UNSTABLE.H : Contains the interface to code that is
//              likely to change. As the code in a header
//              file becomes stable, remove the header file
//              from the makefile's UNSTABLEHDR macro and list
//              it in the STABLEHDRS macro.
//
#ifndef --UNSTABLE_H
#define --UNSTABLE_H
#include<iostream.h>
void notstable( void );
#endif // --UNSTABLE_H

// APPLIB.CPP : This file contains the code that implements
//              the interface code declared in the header
//              files STABLE.H, ANOTHER.H, and UNSTABLE.H.
//
#include"another.h"
#include"stable.h"
#include"unstable.h"
// The following code represents code that is deemed stable and
// not likely to change. The associated interface code is
// precompiled. In this example, the header files STABLE.H and
// ANOTHER.H are precompiled.
void savetime( void )
    { cout << "Why recompile stable code?\n"; }
void savemoretime( void )
    { cout << "Why, indeed?\n\n"; }
// The following code represents code that is still under
// development. The associated header file is not precompiled.
void notstable( void )
    { cout << "Unstable code requires"
            << " frequent recompilation.\n"; }

// MYAPP.CPP : Sample application
//             All precompiled code other than the file listed
//             in the makefile's BOUNDRY macro (stable.h in
//             this example) must be included before the file
//             listed in the BOUNDRY macro. Unstable code must
//             be included after the precompiled code.
//
#include"another.h"
#include"stable.h"
#include"unstable.h"
int main( void )
{
    savetime();
    savemoretime();
    notstable();
}

Open in new window


It should be pretty simple but I don't know how to organize the files, it is "clear" to me that I need to have a makefile, then my question is:
Should I create the following files: myapp.cpp, applib.cpp, another.h, stable.h and stable.h? Or what?

Help is obviously need it.
PanadroidAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jkrCommented:
Yes, you should create those files, just split the above into the respective sections:

stable.h:
//            in the makefile's STABLEHDRS macro.
//
#ifndef --STABLE_H
#define --STABLE_H
#include<iostream>
void savetime( void );
#endif // --STABLE_H

Open in new window


unstable.h:
// UNSTABLE.H : Contains the interface to code that is
//              likely to change. As the code in a header
//              file becomes stable, remove the header file
//              from the makefile's UNSTABLEHDR macro and list
//              it in the STABLEHDRS macro.
//
#ifndef --UNSTABLE_H
#define --UNSTABLE_H
#include<iostream.h>
void notstable( void );
#endif // --UNSTABLE_H

Open in new window


applib.cpp:
// APPLIB.CPP : This file contains the code that implements
//              the interface code declared in the header
//              files STABLE.H, ANOTHER.H, and UNSTABLE.H.
//
#include"another.h"
#include"stable.h"
#include"unstable.h"
// The following code represents code that is deemed stable and
// not likely to change. The associated interface code is
// precompiled. In this example, the header files STABLE.H and
// ANOTHER.H are precompiled.
void savetime( void )
    { cout << "Why recompile stable code?\n"; }
void savemoretime( void )
    { cout << "Why, indeed?\n\n"; }
// The following code represents code that is still under
// development. The associated header file is not precompiled.
void notstable( void )
    { cout << "Unstable code requires"
            << " frequent recompilation.\n"; }

Open in new window


myapp.cpp:
// MYAPP.CPP : Sample application
//             All precompiled code other than the file listed
//             in the makefile's BOUNDRY macro (stable.h in
//             this example) must be included before the file
//             listed in the BOUNDRY macro. Unstable code must
//             be included after the precompiled code.
//
#include"another.h"
#include"stable.h"
#include"unstable.h"
int main( void )
{
    savetime();
    savemoretime();
    notstable();
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PanadroidAuthor Commented:
What about another.h? Another file?
0
PanadroidAuthor Commented:
I did the another.h file too, then I got this error

nother.h(4): fatal error C1016: #if[n]def expected an identifier

Should I do the following change  in the another.h file?

#ifndef ANOTHER_H  <------instead------>  #ifndef --ANOTHER_H
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

jkrCommented:
Ooops, missed that file, sorry. Yes, removing all the '--' won't do any harm. If necessary, you can do that in the other files as well.
0
PanadroidAuthor Commented:
I can't make my file:

applib.cpp(6): warning C4622: overwriting debug information formed during creation of the precompile
eader in object file: 'd:\din\temp\testpch\applib.obj'

applib.cpp(6): fatal error C1854: cannot overwrite information formed during creation of the precompile
d header in object file: 'd:\din\temp\testpch\applib.obj'

Any ideas?
0
jkrCommented:
Try deleting all intermediade files in the target folder (there doesn't seem to be a 'clean' rule in your Makefile), e.g.

del *.obj
del *.pch

Open in new window

0
PanadroidAuthor Commented:
I did it, same result.
0
jkrCommented:
*scratching head*

Ah, try changing the last line of your makefile to

    $(CPP) $(CLFLAGS) /Yc$(BOUNDRY)    applib.cpp myapp.cpp

Open in new window


(/Yu instead of /Yc)
0
PanadroidAuthor Commented:
Isn't it exactly the same line?
0
jkrCommented:
Ups, yes. should have been

    $(CPP) $(CLFLAGS) /Yu$(BOUNDRY)    applib.cpp myapp.cpp

Open in new window

0
PanadroidAuthor Commented:
Now, I'm scratching my head, it doesn't work.
0
jkrCommented:
Sorry for the delay, but does deleting the entire target folder help at all? Or is that acompanied with error messages ("file in use") as well?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.