How to modify the value of a preprocessor based on the value returned by a command or script?

Leo Purktz 0 Reputation points
2024-06-14T00:07:35.4566667+00:00

On visual Studio 2022 how to have a preprocessor value defined based on the result returned by a script?

What i tried:

Solution Explorer > Properties > Build Events > Pre-Build Event > Command Line > "python script.py > output.txt"

But im not figuring how to use the output as the value for the preprocessor TEST

#include <iostream>
int main(int argc, char *argv[])
{
#ifdef TEST
    std::cout << "OK";
#else
    std::cout << "FAILED";
 #endif
}
Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
4,781 questions
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,600 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

  2. Minxin Yu 10,466 Reputation points Microsoft Vendor
    2024-06-17T02:20:33.32+00:00

    Hi, @Leo Purktz

    As Viorel suggested, you can define Marco via script and header file.

    #include <iostream>
    #include"myheader.h"
    int main(int argc, char* argv[])
    {
    #ifdef TEST
        std::cout << "OK";
    #else
        std::cout << "FAILED";
    #endif
    }
    

    Pre-build event

    if exist output.txt (
        echo #define TEST 1 > myheader.h
    ) else (
        echo. > myheader.h
    )
    

    Best regards,

    Minxin Yu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    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.

    0 comments No comments