How about let compiler executes the preprocessor step.

Hi

I want to let the compiler executes the preprocessor and get the output.
Can CMake do this without using the add_custcom_command?

thanks

Do you want the compiler to be also called normally? If so, then you could look into using <LANG>_COMPILER_LAUNCHER (https://cmake.org/cmake/help/latest/prop_tgt/lang_compiler_launcher.html) to call the compiler twice.

It needs the third-party tool like ccache.
Are there any other ways?

Using ccache with CMake is exactly done with <LANG>_COMPILER_LAUNCHER. https://cmake.org/cmake/help/latest/prop_tgt/LANG_COMPILER_LAUNCHER.html even contains:

Some example tools are distcc and ccache.

That is one of the typical uses of the variable, but you can write your own script which takes the command line, modifies it to do the preprocessing and execute that, and then do the original compilation step as normal.

Note that the Makefiles generators have targets to make the preprocessing output (usually you just change the .o extension to .i and invoke it manually). For example, if you want the preprocessed output for the CMakeFiles/mytgt.dir/myfile.o file, make CMakeFiles/mytgt.dir/myfile.i should exist.

Are there targets like this in Ninja generator?

That would be this issue if you’re interested in tracking it.