使用预处理器将代码转换为字符串

发布于 2024-10-04 17:03:03 字数 785 浏览 5 评论 0原文

免责声明:我不是C程序员。

最近看到一个朋友的项目。由于我不明白的原因,他在运行时编译的字符串中编写代码。这会导致类似这样的结果:

char x[] = "int y = 5; printf(\"%i\", y)";
run_this_code(x);

使用起来很糟糕,因为 Visual Studio 不会介入并进行语法突出显示等。

使用一些预处理器滥用,可以欺骗 Visual Studio 认为您正在编写真正的代码,然后让在编译器获取源代码之前,预处理器将其转换为字符串。这是可行的:

#define STRINGIFY(x) #x

int main(void){
        char[] y = STRINGIFY(
                int x = 5;
                printf("%i", x);
        );
        printf("%s", y);
}

问题是它打印出来:

int x = 5; printf("%i\n", x);

那么问题是运行时编译器在第 1 行说错误。有没有办法让它包含换行符?

更新 这不是我的问题。这是别人的代码,我只是对使用预处理器让他的生活更轻松的想法感兴趣。我不知道他为什么要这样做。

更新删除了所有提及 CUDA 的内容,因为这个问题是关于预处理器的,而不是 CUDA。

Disclaimer: I am not a C programmer.

I have recently seen a friend's project. Due to reasons I don't understand, he writes code in a string which is compiled at runtime. This results in something like:

char x[] = "int y = 5; printf(\"%i\", y)";
run_this_code(x);

Which is horrible to use because Visual Studio doesn't step in and do syntax highlighting etc.

Using some preprocessor abuse, it is possible to do trick Visual Studio into thinking you're writing real code and then having the preprocessor turn it into a string before the compiler gets hold of your source. This works:

#define STRINGIFY(x) #x

int main(void){
        char[] y = STRINGIFY(
                int x = 5;
                printf("%i", x);
        );
        printf("%s", y);
}

The problem with this is it prints out:

int x = 5; printf("%i\n", x);

The problem then is the runtime compiler says Error on line 1. Is there a way of making it include the newlines?

Update This is not my problem. It is someone else's code, I just became interested in the idea of using the preprocessor to make his life easier. I have no idea why he's doing it like this.

Update removed all mention of CUDA because this question is about the preprocessor, not CUDA.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

伪装你 2024-10-11 17:03:03

我首先建议只转义换行符就足够了。在有时间验证(并看到问题所有者的评论)后,我意识到这并不能解决问题。

我做了一些测试,并且明确地放入换行符似乎有效:

char[] y = STRINGIFY(
                int x = 5;\n
                printf("%i", x);\n
        );

不过我只在 Linux 上测试过这一点,而不是在语法感知的 IDE 中测试过。那些“看起来很简单”的换行符可能会被聪明的语法荧光笔标记为语法错误。

I first suggested that just escaping the newlines should be enough. Having had time to verify (and seeing the comment by the question's owner), I realize that doesn't cut it.

I did some testing, and explicitly putting in newline symbols seems to work:

char[] y = STRINGIFY(
                int x = 5;\n
                printf("%i", x);\n
        );

I have only tested this on Linux though, not in a syntax-aware IDE. It's possible that those "bare-looking" newlines will instead get flagged as syntax errors, by a clever syntax highlighter.

凉栀 2024-10-11 17:03:03

在 CUDA 中编码时,必须将所有代码以字符串形式发送到显卡进行编译。

是什么让你这么说?查看 CUDA SDK 示例,您可以将 CUDA 代码放入 .cu 文件中,然后使用 nvcc 进行编译。您可以拥有语法突出显示、智能感知和 Visual Studio 的所有优点!请参阅 CUDA 编程指南和

When coding in CUDA, you have to send all the code to the graphics card in a string to be compiled.

What makes you say this? Take a look at the CUDA SDK examples, you can put the CUDA code into .cu files which are then compiled using nvcc. You can have syntax highlighting, intellisense and all the Visual Studio goodness! See the CUDA Programming Guide and this post for more information

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文