未定义对"cos"的引用 [英] undefined reference to `cos'

查看:572
本文介绍了未定义对"cos"的引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有以下代码:

Hi,

I have the code below:

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

int main(int argc, char **argv){

float index;
	for(index = 0; index <= 360; index++){
		index = (3.14/180) * index; //convert to radiant
		float a = cos(index);
		printf("a = %f\n", a);
	}
}



当我尝试编译时,出现以下错误:

/tmp/ccEPoXwg.o:在函数"main"中:
test.c :(.text + 0x6b):对`cos'的未定义引用
collect2:ld返回了1个退出状态

奇怪的是,当我尝试此操作时(没有for循环):



And when I tried to compile, I got the error below:

/tmp/ccEPoXwg.o: In function `main'':
test.c:(.text+0x6b): undefined reference to `cos''
collect2: ld returned 1 exit status

What is weird is that when I try this (without the for loop) :

float a = cos(3.14);
  printf("a = %f\n", a);



我没有错误

任何人都可以,请帮帮我.

预先谢谢您.



I get no errors

Can anyone, please help me.

Thank you in advance

推荐答案

问题是您的数学库中只有一个cos(double)函数,但是您试图在该库中调用一个cos(float)函数.环形.如果没有循环,则将double类型编号传递给cos()(因为您说的是cos(3.14)而不是cos(3.14f)).但是在循环中,您传递的是浮点数(float index).调用cos()时,将索引声明为双精度或强制转换为双精度!
大多数数学库包含cos(double)cosf(float)函数.如果要坚持使用浮动数据,可以尝试使用cosf()
The problem is that you have only a cos(double) function in your math library but you are trying to call a cos(float) function in the loop. Without the loop you pass a double type number to cos() (because you say cos(3.14) and not cos(3.14f)). But in the loop you pass in a float (float index). Declare index as a double or cast index to a double when you call cos()!

Most math libraries contain a cos(double) and a cosf(float) function. You can try using cosf() if you want to stick with your float data!


必须与数学库链接,即:
You must link with the math library, namely:
gcc myprogram.c -lm


:)


检查您的构建语句或制作脚本,以确保您在链接阶段中包含正确的数学库.

pasztorpisti的答案正确;考虑到样本未使用匈牙利表示法,做得很好. :laugh:
Check your build statement, or make script, to ensure that you are including the correct math library in your link phase.

pasztorpisti has the correct answer; well done considering the sample does not use Hungarian notation. :laugh:


这篇关于未定义对"cos"的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆