Avoid double compile

This is a discussion on Avoid double compile within the C++ Programming forums, part of the General Programming Boards category; I'm using stxxl library, and recently I noticed that compile time is significantly increased. Therefore, I tried to make use ...

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    43

    Avoid double compile

    I'm using stxxl library, and recently I noticed that compile time is significantly increased. Therefore, I tried to make use of precompiled header files.
    This is the content of "precompiled.h"
    Code:
    #include <iostream>
     
    #include <stxxl/vector>
    #include <stxxl/priority_queue>
    #include <stxxl/sort>
    #include <stxxl/scan>
    #include <stxxl/stream>
    
    #include <string> 
    #include <sstream>
    #include <cstdlib>
    #include <ctime>
    #include <cmath>  
    #include <vector>   
    #include <limits.h>
    #include <queue>
    #include <algorithm>
    #include <numeric>
    #include <typeinfo>
    #include <fstream>              
    #include <cairo.h>
    #include <cairo-pdf.h>
    #include <myFile1.cpp>
    #include <myFile2.cpp>
    and the content of a specific corresponding makefile is
    Code:
    STXXL_ROOT      ?= /home/mirza/stxxl-1.2.1
    STXXL_CONFIG    ?= stxxl.mk
    include $(STXXL_ROOT)/$(STXXL_CONFIG)
    
    # use the variables from stxxl.mk
    
    CXX              = $(STXXL_CXX)
    CPPFLAGS        += $(STXXL_CPPFLAGS)
    
    # add your own optimization, warning, debug, ... flags
    # (these are *not* set in stxxl.mk)
    CPPFLAGS        += $(shell pkg-config --cflags cairo)
    STXXL_LDLIBS    += $(shell pkg-config --libs cairo)
    
    CPPFLAGS        += -O3 -Wall -g -c -DFOO=BAR
    
    # build your application
    # (my_example.o is generated from my_example.cpp automatically)
    
    precompiled.o: precompiled.h
    	$(CXX) $(CPPFLAGS) $(CXXFLAGS) precompiled.h -o $@ $(STXXL_LDLIBS)
    The first line of the .cpp file is "include "precompiled.h", but after invoking makefile specific for that .cpp file, I get reports and warnings that I got when invoking makefile specific for header file. So, it seems that it compiles again. I've read that the precompiled header files produce .gch files that are automatically sought to be incorporated. In my header specific makefile I could not "tell the compiler" I want .gch as output to be later used by main .cpp file. It only accepts .o.
    Any thoughts on how to do this properly?
    Thanks
    To, maybe, make things easier, I include the content of the makefile specific for the fileA.cpp, which is main file, and contains "include "precompiled.h"" line
    Code:
    STXXL_ROOT      ?= /home/mirza/stxxl-1.2.1
    STXXL_CONFIG    ?= stxxl.mk
    include $(STXXL_ROOT)/$(STXXL_CONFIG)
    
    # use the variables from stxxl.mk
    
    CXX              = $(STXXL_CXX)
    CPPFLAGS        += $(STXXL_CPPFLAGS)
    
    # add your own optimization, warning, debug, ... flags
    # (these are *not* set in stxxl.mk)
    
    CPPFLAGS        += $(shell pkg-config --cflags cairo)
    STXXL_LDLIBS    += $(shell pkg-config --libs cairo)
    
    
    CPPFLAGS        += -O3 -Wall -g -DFOO=BAR
    
    # build your application
    # (my_example.o is generated from my_example.cpp automatically)
    fileA.bin: fileA.o
        $(CXX) $(CPPFLAGS) $(CXXFLAGS) fileA.o -o $@ $(STXXL_LDLIBS)
    Maybe changing this file would make things easier? Thanks
    Last edited by onako; 02-24-2011 at 08:45 AM.

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    First, in the first makefile change:
    Code:
    precompiled.o: precompiled.h
    to:
    Code:
    precompiled.h.gch: precompiled.h
    and in the 2nd makefile change:
    Code:
    CPPFLAGS        += -O3 -Wall -g -DFOO=BAR
    to:
    Code:
    CPPFLAGS        += -O3 -Wall -g -DFOO=BAR -include precompiled.h
    Next, why are you #including .cpp files?
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    43
    cpp files are included in other cpp files (or h files), inherited project.
    I've made the changes you proposed, but, again, after invoking the main makefile I get reports and warnings I got during compilation of the header file. So, it seems that the .gch file is neglected.
    What is the way to save the job once done?
    Note that for project this large, it would be inappropriate to go through and separate .h files.

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    I went through this same problem just a few days ago.
    I assume you read this page: Precompiled Headers - Using the GNU Compiler Collection (GCC)
    Are you sure the .gch file and the rest of your .o files are being compiled with exactly the same compiler flags...?

    Try adding a -H flag to both of them and then you can see exactly which header files are being included for each file. If the .gch file is being used by g++ it will print a ! at the beginning of the line; if it's not used, it'll print an X instead.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,768
    Quote Originally Posted by onako View Post
    cpp files are included in other cpp files (or h files), inherited project.
    Don't do that. Compile .cpp files; don't include them.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,248
    Quote Originally Posted by Elysia View Post
    Don't do that. Compile .cpp files; don't include them.
    Did you miss the part about "inherited project?"

    There are a couple of legitimate reasons that might be done, though most people won't ever run into that.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,768
    I didn't miss it, but perhaps I missed the point about why it was as it is.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Travel Expenses-Weird Result
    By taj777 in forum C++ Programming
    Replies: 4
    Last Post: 04-06-2010, 02:23 PM
  2. Need some help...
    By darkconvoy in forum C Programming
    Replies: 32
    Last Post: 04-29-2008, 03:33 PM
  3. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  4. expected primary expression
    By mju4t in forum C Programming
    Replies: 2
    Last Post: 03-27-2007, 06:59 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21