小编BFl*_*ash的帖子

如何阻止GCC从obj文件中的字符串文字中删除尾随换行符?

在Linux下工作,我刚刚遇到了以下问题.(当然,有人会给我答案,但到目前为止,我没有找到任何简单明了的答案:)

/*compile with gcc -o out.x hello.c*/

#include<stdio.h>

int main()
{
    printf("Hello World2\r\n");
    printf("Hello World3\r\n ");

 return 0;
}
Run Code Online (Sandbox Code Playgroud)

在Linux下运行以下代码给出两个字符串但结尾字符是不同的:第一个输出以0x0d结尾,而第二个以0x0d,0x0a结尾.

这是由编译器(GCC)完成的,如obj文件中所示:

Contents of section .rodata:
 400610 01000200 48656c6c 6f20576f 726c6432  ....Hello World2
 400620 0d004865 6c6c6f20 576f726c 64330d0a  ..Hello World3..
 400630 2000                                  .              
Run Code Online (Sandbox Code Playgroud)

所以,问题是:

  • 为什么?
  • 我怎样才能避免这种"优化"(!?)

谢谢

linux printf gcc newline carriage-return

1
推荐指数
1
解决办法
73
查看次数

标签 统计

carriage-return ×1

gcc ×1

linux ×1

newline ×1

printf ×1