How to Preprocess to File in a CMake project

slymz 6 Reputation points
2020-12-01T18:29:02.413+00:00

In a CMake project (not a Visual Studio project generated by CMake), is there a way to run "preprocess-to-file" target?

For example,

  1. with MSBuild based Visual Studio projects, this is accomplished by preprocess-to-file (/E) option on the project settings per given cpp file.
  2. with Unix Makefile based projects, there is a foo.cpp.i make target available for each cpp source file.

There does not seem to be a way to do this with default VS CMake folder, that uses ninja under the hood. Or is there?

C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,559 questions
Not Monitored
Not Monitored
Tag not monitored by Microsoft.
36,473 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Dylan Zhu-MSFT 6,406 Reputation points
    2020-12-02T02:51:57.56+00:00

    Hi slymz-9073,

    I found a similar issue: https://stackoverflow.com/questions/11383314/how-to-configure-cmake-target-or-command-to-preprocess-c-file, hope it could help you.

    And since cmake is beyond our support, we suggest you could redirect to stack overflow with cmake tag, and the community members will provide dedicated support for you.

    Best Regards,
    Dylan


    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our **documentation to enable e-mail notifications if you want to receive the related email notification for this thread.**


  2. slymz 6 Reputation points
    2020-12-07T14:18:31.94+00:00

    Hi Dylan, thanks for taking the time to answer. On the CMake-side of things, from what I understand Ninja projects do not support .i targets (https://gitlab.kitware.com/cmake/cmake/-/issues/13838). Also it is quite cumbersome to be adding preprocessor targets manually on every CMakeLists in projects. So unfortunately, tinkering with CMake does not seem to be an optimal solution.

    What would be extremely useful from Visual Studio would be provide easy ways of doing this in tasks.vs.json. In fact, I suspect there might already be a convenient, possibly undocumented, alternative to the following:

    Here is my very tedious entry to tasks.vs.json:

     {
          "taskLabel": "Preprocess to File",
          "appliesTo": "/*.cpp",
          "type": "launch",
          "command": "clang-cl.exe",
          "args": [
            "/nologo",
            "-TP",
            "-I..\\\\..\\\\..\\\\",
            "-IC:\\\\usr\\\\include\\\\boost-1_71",
             ....  [ 10s of other flags ]
            "-std:c++latest",
            "-E",
            "${file}",
            ">${file}.i"
          ]
        },
    

    This is super cumbersome, as I have to run build once with -v flag, capture the output, copy-paste all compilation flags, and add -E etc.

    Is there anything that can be done to ease this situation a bit?

    Thanks

    slymz

    0 comments No comments