我是C的新手,我有这个代码:
#include <stdio.h>
#include <math.h>
int main(void)
{
double x = 0.5;
double result = sqrt(x);
printf("The square root of %lf is %lf\n", x, result);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但是当我编译它时:
gcc test.c -o test
Run Code Online (Sandbox Code Playgroud)
我收到这样的错误:
/tmp/cc58XvyX.o: In function `main':
test.c:(.text+0x2f): undefined reference to `sqrt'
collect2: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)
为什么会这样?是sqrt()不是在math.h头文件中?我cosh和其他三角函数有同样的错误.为什么?
对于以下C源代码:
#include <math.h>
int main(void)
{
double x;
x = log(0.0);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我编译时gcc -lm,我得到:
/tmp/ccxxANVH.o: In function `main':
a.c:(.text+0xd): undefined reference to `log'
collect2: error: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)
但是,如果我替换log(0.0)为log(10.0),那么它可以成功编译.
我不太明白这一点,因为无论它们是否具有数学意义,它们都应该编译 - 没有语法错误.有人能解释一下吗?
以防万一,我的gcc -v输出:
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 4.8.2-19ubuntu1' --with-bugurl=file:///usr/share/doc/gcc-4.8/README.Bugs --enable-languages=c,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.8 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.8 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --disable-libmudflap --enable-plugin --with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-4.8-amd64/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-4.8-amd64 …Run Code Online (Sandbox Code Playgroud) 我创建了一个小程序,如下所示:
#include <math.h>
#include <stdio.h>
#include <unistd.h>
int main(int argc, char *argv[]) {
int i;
double tmp;
double xx;
for(i = 1; i <= 30; i++) {
xx = (double) i + 0.01;
tmp = sqrt(xx);
printf("the square root of %0.4f is %0.4f\n", xx,tmp);
sleep(1);
xx = 0;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我尝试使用以下命令编译它时,我收到编译器错误.
gcc -Wall calc.c -o calc
Run Code Online (Sandbox Code Playgroud)
收益:
/tmp/ccavWTUB.o: In function `main':
calc.c:(.text+0x4f): undefined reference to `sqrt'
collect2: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)
如果我用sqrt(10.2)之类的常量替换对sqrt(xx)的调用中的变量,它编译得很好.或者,如果我明确链接如下:
gcc -Wall -lm calc.c -o …Run Code Online (Sandbox Code Playgroud) #include <math.h>
#include <stdio.h>
int main(void)
{
double x = 4.0, result;
result = sqrt(x);
printf("The square root of %lf is %lfn", x, result);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
此代码不起作用,因为它采用变量的平方根.如果你改变了sqrt(x),to sqrt(20.0),代码工作正常,为什么?请解释.
另外,我如何获得变量的平方根(这是我真正需要的)?
OUTPUT:
matthewmpp@annrogers:~/Programming/C.progs/Personal$ vim sqroot1.c
matthewmpp@annrogers:~/Programming/C.progs/Personal$ cc -c sqroot1.c
matthewmpp@annrogers:~/Programming/C.progs/Personal$ cc -o sqroot1 sqroot1.c
matthewmpp@annrogers:~/Programming/C.progs/Personal$ ./sqroot1
4.472136
matthewmpp@annrogers:~/Programming/C.progs/Personal$ vim sqroot2.c
matthewmpp@annrogers:~/Programming/C.progs/Personal$ cc -c sqroot2.c
matthewmpp@annrogers:~/Programming/C.progs/Personal$ cc -o sqroot2 sqroot2.c
/tmp/ccw2dVdc.o: In function `main':
sqroot2.c:(.text+0x29): undefined reference to `sqrt'
collect2: ld returned 1 exit status
matthewmpp@annrogers:~/Programming/C.progs/Personal$
Run Code Online (Sandbox Code Playgroud)
注意:sqroot1是20.0的sqroot.sqroot2是变量的sqroot.
matthewmpp@annrogers:~/Programming/C.progs/Personal$ cc …Run Code Online (Sandbox Code Playgroud) 我在 GNU/Linux Debian 8.5 下编码
我有一个简单的程序。
如果我用gcc prog.c它编译它就可以了!
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
int main(int argc, char const *argv[]) {
float _f = 3.1415f;
floor(_f);
ceil(_f);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但是,如果我添加pow(),它会说它找不到pow,我需要添加gcc prog.c -lm以使其正确。
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
int main(int argc, char const *argv[]) {
float _f = 3.1415f;
floor(_f);
ceil(_f);
pow(_f, 2);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如果我是对的,pow(), ceil(),floor()都是来自<math.h>?
那么,为什么不floor() …
以下代码:
#include <stdio.h>
#include <math.h>
int main(void)
{
long long int a;
scanf("%lld", &a);
printf("%lf", sqrt(a));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
给出输出:
source_file.c: In function ‘main’:
source_file.c:9:5: warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld", &a);
^
/tmp/ccWNm2Vs.o: In function `main':
source.c:(.text.startup+0x65): undefined reference to `sqrt'
collect2: error: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)
但是,如果我执行long long int a = 25;并删除scanf语句,或者只是执行sqrt(25),它们都可以工作(正确给出 output 5.000000)。
我检查了这个问题,但它是针对 C++ 并使用函数重载的,而 afaict C 没有函数重载(这就是为什么我们有sqrtf, …