Search code examples
c++visual-studiomsbuildpreprocessorcl

Adding preprocessor to CL in command line and build *.sln by MSBuild


I need to compile and build c++ project in command-line using MSBiuld without using vs2019 IDE, some preprocessors must be added by command-line!

for example, how to define PO preprocessor and run MSBuild to compile below code?

#include <iostream>

using namespace std;

int main()
{
#ifdef PO
    cout << "PO is defined";
#else
    cout << "PO not defined";
#endif
    
    return 0;
}

Solution

  • Based on CL environment variables any prepossessor can be defined by /D for example

    set CL=/DPO
    MSBuild project.sln
    

    To make sure that the last definition of prepossessors will applied without restarting cmd or commandline tool, use /t:Clean;Rebuild

    set CL=/DPO
    MSBuild Project1.sln /t:Clean;Rebuild