Search code examples
c++pragmastring-literals

Convert char * to string literal


I want to use a char * with #pragma message to tell where my problems are in the Visual Studio debugger.

void OutputShaderErrors(const char *filename)
{
  std::string outputMessage = "Errors written to: ";
  outputMessage += filename;

  #pragma message(outputMessage.c_str())
}

The above block of code does work, but the following warning is given:

Warning 1   warning C4083: expected 'string'; found identifier 'outputMessage'

Solution

  • As the # in #pragma suggest, this is a feature of the preprocessor. You cannot call it at runtime. The code does not actually work, and the warning you see comes from your invalid use of #pragma.

    Since you are working with Visual Studio, you may find the OutputDebugMessage function useful.