【问题标题】:Trying to generate assembly code from C试图从 C 生成汇编代码
【发布时间】:2018-03-30 19:10:16
【问题描述】:

我从一个 C 项目创建了一个汇编代码,但是当我运行它时,我得到了这个编译错误:

Compilation Error
/usr/bin/x86_64-linux-gnu-ld: warning: cannot find entry symbol _start; 
defaulting to 00000000004000b0
/tmp/tmpM7HOI5/squares.o: In function `main':
main.c:(.text+0x2e): undefined reference to `malloc'
main.c:(.text+0x43): undefined reference to `perror'
main.c:(.text+0x4d): undefined reference to `exit'
main.c:(.text+0x54): undefined reference to `stdin'
main.c:(.text+0x68): undefined reference to `fgets'
main.c:(.text+0xb5): undefined reference to `atoi'
main.c:(.text+0x12d): undefined reference to `free'
main.c:(.text+0x14d): undefined reference to `malloc'
main.c:(.text+0x158): undefined reference to `stdin'
main.c:(.text+0x16c): undefined reference to `fgets'
main.c:(.text+0x17f): undefined reference to `strtok'
main.c:(.text+0x18f): undefined reference to `atoi'
main.c:(.text+0x1b5): undefined reference to `strtok'
main.c:(.text+0x1c5): undefined reference to `atoi'
main.c:(.text+0x1ef): undefined reference to `strtok'
main.c:(.text+0x1ff): undefined reference to `atoi'
main.c:(.text+0x229): undefined reference to `strtok'
main.c:(.text+0x239): undefined reference to `atoi'
main.c:(.text+0x25e): undefined reference to `free'
main.c:(.text+0x2f6): undefined reference to `printf'

似乎所有库调用都未定义。

编译我使用:

gcc -std=c99 -Wall -O2 -lm -S main.c

【问题讨论】:

  • 这些错误是因为您没有与标准 C 库链接。
  • 另请注意,您不会收到编译错误。或装配错误。你得到 linker 错误。
  • 编译步骤是否显示了打印错误的命令?还是有没有其他命令没有显示?
  • 似乎显示的命令不是产生错误的命令。
  • 只需按照通常的方式编译并添加-save-temps(而不是-S)

标签: c assembly


【解决方案1】:

您的 GCC 命令只生成汇编代码,但您的错误列表是针对链接器调用的。

我怀疑-lm 选项会导致编译器驱动程序调用链接器 - 如果您只为单个翻译单元生成程序集,则无需指定库。

链接器抱怨缺少 C 标准库和 crt0.0 C 运行时启动。

删除不必要的-lm 选项 - 您不打算链接,因此库无关紧要。

【讨论】:

  • 我试过了,但是当我尝试运行生成的汇编代码时,仍然得到同样的编译错误。
  • @user2081417 请向我们展示您用于“运行生成的汇编代码”的命令行。
  • 代码上传到远程服务器运行:Assembly (x64) by (binutils 2.25)。所以我没有运行任何命令。预期的格式也是 AT&T,但 gcc 是按照标准来做的
  • @user2081417 :您不能“运行”汇编代码。你必须组装它,然后链接它。如果您这样做,那么您没有在问题中提供完整的信息。
  • @user2081417 :这正是您需要添加到问题中的信息。由于信息不完整,我们只能猜测。您发布的命令不是发出所示错误的原因。编译器翻译单个编译单元 - 您没有链接。远程服务器正在尝试链接代码,但尚未提供标准库或启动代码。就那么简单。如果您正在使用诸如此类的不寻常的开发过程,则需要提及。
【解决方案2】:

您的 -lm 链接到数学库。同样,您需要-lc 才能链接到 C 库。

【讨论】:

  • 我试过了,但是当我尝试运行生成的汇编代码时,仍然得到同样的编译错误。
猜你喜欢
  • 2012-03-04
  • 1970-01-01
  • 2021-07-18
  • 1970-01-01
  • 1970-01-01
  • 2012-11-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多