【发布时间】:2015-02-13 01:44:51
【问题描述】:
我尝试用 C 编译一个需要“math.h”的库,这里是 .c 文件的开头:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include "sparse_matrix.h"
...
我用这个命令编译:
gcc -c ./sparse_matrix.c -o sparse_matrix.o -lm -Wall -pedantic -std=c99 -g -O
但即使 #include done 和文件后的标志 -lm (我已经尝试在行尾但没有任何改变)我仍然得到错误:
undefined reference to « sqrt »
collect2: error: ld returned 1 exit status
在谷歌上搜索了一个小时后我没有得到它。 我在 ubuntu 14.10(utopic unicorn)下使用 gcc 4.9。 提前感谢您的帮助!
【问题讨论】:
-
-c选项禁止链接。您不会从该命令行收到该错误。因此,这不是产生错误的命令行。 (不要在makefile中使用 @ 之前的规则——这可能不是你的问题,但它隐藏了命令并可能误导你。)大部分都是 @paxdiablo 所说的。