GCC:如何修复或抑制警告:未知转义序列:'\#'

时间:2022-06-05 00:02:39

The OpenGL ES shader code is defined as a C string. To make writing the shader code more intuitively, I define a macro named SHADER_STRING(code) like below. But I encounter a GCC warning unknown escape sequence: '\#' and don't know how to suppress it or fix it.

OpenGL ES着色器代码定义为C字符串。为了更直观地编写着色器代码,我定义了一个名为SHADER_STRING(代码)的宏,如下所示。但我遇到一个GCC警告未知的转义序列:'\#'并且不知道如何抑制它或修复它。

A example code is below

示例代码如下

#include <stdio.h>

int main(void)
{
#define _SHADER_STRING(code) #code
#define SHADER_STRING(code) _SHADER_STRING(code)
    // OpenGL ES Shader codes
    char vShaderStr[] = SHADER_STRING(
        \#version 300 es \n

        void main()
        {
            // ... some OpenGL ES codes.
        });
    printf("%s\n", vShaderStr);
    return 0;
}

Because the #version is a preprocessor supported by OpenGL ES 3.0, then it's invalid to replace the \# with \\#, otherwise the codes will become

因为#version是OpenGL ES 3.0支持的预处理器,所以用\#替换\#是无效的,否则代码将变为

\#version 300 es
void main() { }

The OpenGL shader compiler will complained knowing nothing about the \#version .

OpenGL着色器编译器会抱怨对\ #version一无所知。

2 个解决方案

#1


4  

According to this answer, the following code should work:

根据这个答案,以下代码应该工作:

#define GLSL(version, shader)  "#version " #version "\n" #shader

const GLchar* vShaderStr_30 = GLSL(300,
    void main() {
        …
    }
);

#2


1  

#define _SHADER_STRING(str) #str
#define SHADER_STRING(str) _SHADER_STRING(str)
// OpenGL ES Shader codes
char vShaderStr_30[] = SHADER_STRING(#version 300 es \n

    void main()
    {
         // ...
    });

But without a good Minimal, Complete, and Verifiable example it's hard to have a better answer.

但如果没有良好的Minimal,Complete和Verifiable示例,很难有更好的答案。

#1


4  

According to this answer, the following code should work:

根据这个答案,以下代码应该工作:

#define GLSL(version, shader)  "#version " #version "\n" #shader

const GLchar* vShaderStr_30 = GLSL(300,
    void main() {
        …
    }
);

#2


1  

#define _SHADER_STRING(str) #str
#define SHADER_STRING(str) _SHADER_STRING(str)
// OpenGL ES Shader codes
char vShaderStr_30[] = SHADER_STRING(#version 300 es \n

    void main()
    {
         // ...
    });

But without a good Minimal, Complete, and Verifiable example it's hard to have a better answer.

但如果没有良好的Minimal,Complete和Verifiable示例,很难有更好的答案。