gcc equivalent of #pragma comment

白昼怎懂夜的黑 提交于 2019-12-02 03:08:08

I'm not sure what it means to "add a comment to an executable". Who or what is going to consume, display, or even notice such comments? Nevertheless, if you just want to ensure some string is embedded somewhere in your program, then simply declare it as an ordinary (C) string at file scope.

static const char my_comment[] = "This comment should appear in the compiled executable";

If you don't want to clutter the .data section (which is where the static const char[] would go) with comments or conversely, want the comments to be found easily from the .comment section, you can add the comments there with a little bit of inline assembly:

__asm__(".section .comment\n\t"
        ".string \"Hello World\"\n\t"
        ".section .text");

Gcc also has #ident directive which copies the text into an appropriate section if available. In the case of ELF, it would be the .comment section. This solution, even though the directive isn't standard, is probably more portable than the former.

#ident "Hello World"

#ident may be useful. But there are two caveats :

  1. It may not work on all targets.
  2. This is neither a C language standard nor a GNU coding standard

The ‘#ident’ directive takes one argument, a string constant. On some systems, that string constant is copied into a special segment of the object file. On other systems, the directive is ignored. The ‘#sccs’ directive is a synonym for ‘#ident’.

These directives are not part of the C standard, but they are not official GNU extensions either.

https://gcc.gnu.org/onlinedocs/cpp/Other-Directives.html

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!