Skip to main content
ferro
Lead
November 28, 2024
Question

How can I make CubeIDE 1.16 generate and use precompiled headers?

  • November 28, 2024
  • 1 reply
  • 712 views

Hi,

Aiming to speed up build times, I would like to explore precompiled headers.

How can I make CubeIDE 1.16 generate and use precompiled headers?

 

Thank you
Ferro

1 reply

Pavel A.
Super User
November 28, 2024

GCC compiler shipped with CubeIDE should support precompiled headers. Please see GCC documentation for command line syntax.

Below is a small example for gcc created by AI (imagine that!)

----- this is common .h file to precompile: -----
#ifndef COMMON_H 
#define COMMON_H 
#include <stdio.h>
void greet(const char* name);
#endif

------ First C file using the common .h -----
// common.c 
#include "common.h" 
void greet(const char* name) { 
 printf("Hello, %s!\n", name);
}

------ Another C file using the common .h -----
// main.c 
#include "common.h" 
int main() {
 greet("GCC User");
 return 0;
}

-------- Commands to build --------
# Create the precompiled header for common.h, it will be common.h.gch
gcc -x c-header common.h

# For C++ use command: 
# g++ -x c++-header common.h

# Build the program
gcc main.c common.c -o prog

 

 

ferro
ferroAuthor
Lead
November 28, 2024

Thanks @Pavel A. - I posted my question after running example similar to yours.

I would like solution for CubeIDE.

Pavel A.
Super User
November 28, 2024

In CubeIDE you can put the command to create precompiled headers in the pre-build step script. As the example shows, no other compiler options are needed. The gch files should be used automatically if they exist.