【问题标题】:linker reports undefined reference but code compiles OK [duplicate]链接器报告未定义的引用,但代码编译正常 [重复]
【发布时间】:2015-02-04 04:12:58
【问题描述】:

我正在努力学习足够多的 c 来满足我偶尔编写简单程序来回答我遇到的特定问题的需要。我一直在学习教程并使用 Geany 以方便使用。相反,我似乎无法运行最简单的程序。这是我的源代码:

#include <stdio.h>
#include <math.h>

int main(int argc, char **argv)
{
    int x, y;
    double c, sqr_c;

    for (x = 10; x <= 31; x++)
    {
        for (y = 10; y <= 31; y++)
        {
            c = 1000 * x * x + y * y;
            sqr_c = sqrt(c);
            printf ("%f\n", sqr_c);
        }
    }

    return 0;
}

它编译得很好(gcc -c)但是当我尝试构建一个可执行文件时,我得到:

gcc  "concsqr.c" -Wall -o "concsqr"  (in directory: /home/chip)
/tmp/cccSmdZS.o: In function `main':
concsqr.c:(.text+0x4b): undefined reference to `sqrt'
collect2: error: ld returned 1 exit status
Compilation failed.

我读过一些关于确保链接器可以找到定义 sqrt() 的库的内容,但我不知道该怎么做,而且它不会在标准位置吗?为什么链接器不知道它在哪里?它是 c 的标准库。

【问题讨论】:

    标签: c gcc linker geany


    【解决方案1】:

    您必须尝试使用​​-lm 标志编译程序,因为libm.so 是数学库,-l 标志添加lib 前缀和.a.so 后缀。

    gcc concsqr.c -Wall -o concsqr -lm 
    

    【讨论】:

      猜你喜欢
      • 2022-12-07
      • 2012-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-29
      • 2013-01-11
      相关资源
      最近更新 更多