【问题标题】:How can the C function "Exp" be properly used in NASM for Linux?如何在 NASM for Linux 中正确使用 C 函数“Exp”?
【发布时间】:2014-02-17 17:28:47
【问题描述】:

我正在尝试在 NASM for Linux 中实现 C 函数“exp”。该函数接受一个双精度值 x,并返回一个双精度值 r = e^x,其中 e 是欧拉数。这是我的实现:

extern exp

SECTION .bss

    doubleActual: resq 1
    doubleX: resq 1

SECTION .text

    main:
    ;some other code here

    ;calculate actual result
    push doubleActual ; place to store result
    push doubleX ;give the function what x is.
    call exp
    add esp, 8

在编译尝试时,我得到以下信息:

hw7_3.o: In function `termIsLess':
hw7_3.asm:(.text+0xf9): undefined reference to `exp'

这是指我实际调用 exp 的时间,这很奇怪,因为“extern exp”似乎工作得很好。我做错了什么?

【问题讨论】:

  • 你是否链接到它定义的任何库?
  • 使用 NASM for linux,这通常是没有必要的。例如,我可以“extern printf”,然后立即可以在我的代码中使用“call printf”。我认为这是 C 函数所独有的。
  • 我猜你需要一个 -lm 来链接 cmath 库。
  • 请原谅我的无知,但我该怎么做呢?我以前不需要与 NASM 链接。我当前的编译例程是:“nasm -f elf32 name.asm”后跟“gcc -m32 name.o -o name”,其中name是我的程序名称。
  • 你不需要为其他函数做任何事情,因为你让 GCC 为你做链接,它总是链接到libc。但它并没有隐式链接到libm

标签: c linux nasm exp


【解决方案1】:

通过http://www.linuxtopia.org/online_books/an_introduction_to_gcc/gccintro_17.html ....

我需要用 gcc 做以下事情:

gcc -m32 name.o -lm -o name

“-lm”标签是链接C数学库的快捷方式,独立于标准库。

【讨论】:

    猜你喜欢
    • 2012-01-21
    • 1970-01-01
    • 1970-01-01
    • 2019-01-09
    • 2019-07-03
    • 1970-01-01
    • 2019-11-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多