Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6159217
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T21:05:39+00:00 2026-05-23T21:05:39+00:00

I’m using cmake with C++ project. I want to use precompiled headers in GCC.

  • 0

I’m using cmake with C++ project. I want to use precompiled headers in GCC.

I want to run cmake once and then, when running make, I want this actions to happen:

  1. Run my custom tool (python script) on whole source tree. It generates precompiled.h headers in some subdirectories. It’s purpose is to scan *.h and *.cpp for #include’s and move them to common precompiled.h files.
  2. generate GCC’s precompiled headers (g++ -c precompiled.h -o precompiled.h.gch) ONLY when precompiled.h file has changed after step 1.
  3. build outdated targets (after step 2. because I want to use .gch file)

I’ve managed to add dependencies, so whenever I build any target, my python script’s target is executed (this is ugly, because now every target has to depend on single global_init target). Also I can modify my python script, so it doesn’t modify precompiled.h when it is not necessary.

But I don’t know what to do next. Is there any way to tell cmake that it should run my custom script and AFTER THAT determine if precompiled.h.gch must be created? Now it is always build (this file contains many #include’s, so it takes some time). Basically:

  1. Execute python script -> precompiled.h MAY BE updated
  2. Examine precompiled.h timestamp and if it’s newer -> build precompiled.h.gch
  3. Standard compilation steps

I’ve looked in many places, but cmake’s (and make’s) way of calculating dependencies seems too weak to accomplish this task.

In this question:
lazy c++
compilation always happens after lazycpp is executed. This would not be optimal in my case.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T21:05:40+00:00Added an answer on May 23, 2026 at 9:05 pm

    The following CMake commands may serve as a starting point for your project:

    add_custom_target(scanner COMMAND "${CMAKE_CURRENT_SOURCE_DIR}/scanner.py"
        COMMENT "Scanning include files ..."
        WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}")
    

    This command covers step 1. It invokes your python script which may update the existing precompiled.h files in CMAKE_CURRENT_SOURCE_DIR.

    add_custom_command(OUTPUT precompiled.h.gch
        COMMAND ${CMAKE_CXX_COMPILER} ${CMAKE_CXX_FLAGS} 
        -c "${CMAKE_CURRENT_SOURCE_DIR}/precompiled.h" -o precompiled.h.gch
        DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/precompiled.h"
        IMPLICIT_DEPENDS CXX "${CMAKE_CURRENT_SOURCE_DIR}/precompiled.h")
    
    add_custom_target(generate_precompiled DEPENDS
        "${CMAKE_CURRENT_BINARY_DIR}/precompiled.h.gch")
    
    add_dependencies(generate_precompiled scanner)
    

    These commands cover step 2. The precompiled header is generated with a custom command which depends on precompiled.h and implicitly on other headers that precompiled.h includes.
    precompiled.h.gch is generated in the CMAKE_CURRENT_BINARY_DIR directory.
    Because the precompiled header generation requires precompiled.h to be updated by the python script, we add a target level dependency on the target scanner.

    include_directories(BEFORE ${CMAKE_CURRENT_BINARY_DIR})
    
    add_executable(main main.cpp)
    
    set_target_properties(main PROPERTIES COMPILE_FLAGS "-include precompiled.h -H")
    
    add_dependencies(main generate_precompiled)
    

    These commands add standard compilation steps which generate an executable main. The precompiled header is included as a default header with gcc’s -include switch. The CMAKE_CURRENT_BINARY_DIR which contains precompiled.h.gch is added as an include directory. gcc will pick up precompiled.h.gch from there when precompiled.h is included (see Using Precompiled Headers).

    Finally the target dependency on generate_precompiled ensures that the precompiled header is updated if necessary.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm making a simple page using Google Maps API 3. My first. One marker
We're building an app, our first using Rails 3, and we're having to build
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have some data like this: 1 2 3 4 5 9 2 6
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
i want to parse a xhtml file and display in UITableView. what is the
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I have a bunch of posts stored in text files formatted in yaml/textile (from
I am trying to loop through a bunch of documents I have to put

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.