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

Laila 40 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
}
Developer technologies C++
Developer technologies Visual Studio Other
{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 13,501 Reputation points Microsoft External Staff
    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

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.