相关疑难解决方法(0)

对sqrt(或其他数学函数)的未定义引用

我有这个简单的代码:

max = (int) sqrt (number);
Run Code Online (Sandbox Code Playgroud)

在标题中我有:

#include <math.h>
Run Code Online (Sandbox Code Playgroud)

但应用程序仍然表示未定义的引用sqrt.你觉得这里有什么问题吗?看起来一切都应该没问题.

c linker-errors undefined-reference

57
推荐指数
2
解决办法
9万
查看次数

gcc不会正确包含math.h

这是一个概述我的问题的最小例子

test.c的:

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

main ()
{
   fmod ( 3, 2 );
}
Run Code Online (Sandbox Code Playgroud)

这是我要编译的命令 test.c

gcc -lm test.c -o test
Run Code Online (Sandbox Code Playgroud)

这是我发出上述命令时得到的输出

/tmp/ccQmRk99.o: In function `main':
test.c:(.text+0x3e): undefined reference to `fmod'
collect2: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)

如果相反,我会使用相同的输出cc.我使用的是以下版本的gcc

gcc-4.6.real (Ubuntu/Linaro 4.6.1-9ubuntu3) 4.6.1
Run Code Online (Sandbox Code Playgroud)

我的程序无法编译的任何想法?

c linker gcc static-libraries

19
推荐指数
1
解决办法
6万
查看次数

未定义的引用`round' - 为什么?我正在使用math.h

我的源代码有点问题.gcc对我说: 未定义引用`round'但我不知道为什么因为我正在使用stdio.h,stdlib.h,math.h ... :-(你可以帮我解决这个问题吗?

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <dirent.h>
#include <math.h>
#include <string.h>
#include <dirent.h>
#include <sys/stat.h>
#define VERYBIG 200

int dir_size(const char* dirname)
{
    int size = 0;
    char path[VERYBIG];
    struct stat tmp;
    DIR* cat = opendir(dirname);
    struct dirent* entry;

    while ((entry = readdir(cat)))
    {
        if (!((strcmp(entry->d_name, ".") == 0) || (strcmp(entry->d_name, "..") == 0)))
        {
            path[0] = 0;
            strcat(path, dirname);
            strcat(path, "/");
            strcat(path, entry->d_name);
            if (lstat(path, &tmp) == -1)
                perror("lstat");

            if (S_ISDIR(tmp.st_mode))
            {
                size …
Run Code Online (Sandbox Code Playgroud)

c linux ubuntu gcc

6
推荐指数
1
解决办法
7640
查看次数

编译时出错"未定义引用`sin'"(使用-lm)

我已下载并编译:http://leenissen.dk/fann/wp/

  • cmake版本2.8.11.2
  • gcc(Ubuntu/Linaro 4.8.1-10ubuntu8)4.8.1

用于编译的命令:

cmake -D CMAKE_INSTALL_PREFIX:PATH=/usr .
Run Code Online (Sandbox Code Playgroud)

安装:

sudo make && sudo make install
Run Code Online (Sandbox Code Playgroud)

然后我转到fann项目中的examples /目录并尝试通过运行编译示例:

make all
Run Code Online (Sandbox Code Playgroud)

我收到一个错误:

gcc -O3 xor_train.c -o xor_train -lfann -lm
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../lib/libfann.so: undefined reference to `sin'
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../lib/libfann.so: undefined reference to `exp'
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../lib/libfann.so: undefined reference to `cos'
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../lib/libfann.so: undefined reference to `log'
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../lib/libfann.so: undefined reference to `pow'
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../lib/libfann.so: undefined reference to `sqrt'
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../lib/libfann.so: undefined reference to `floor'
collect2: error: ld returned 1 exit status
make: *** [xor_train] Error 1
Run Code Online (Sandbox Code Playgroud)

更新:

  • 我遵循了图书馆的指示 …

c gcc cmake linker-errors fann

4
推荐指数
1
解决办法
5551
查看次数

在 C 中使用 math.h sqrt 函数

阅读 math.h 的文档,似乎我所要做的就是包含 math.h,并使用包含的数学函数,例如 sqrt。问题是当我尝试在程序中使用 sqrt 时出现以下错误。我尝试了 math.sqrt,但这也不起作用。知道我做错了什么吗?

undefined reference to `sqrt'
Run Code Online (Sandbox Code Playgroud)

...

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

int main (int argc, char *argv[])
{
  int a, b;

  a = 1;
  b = 5;

  if (a < sqrt (b))
    printf("1 < sqrt(5)");

  return 0;
}
Run Code Online (Sandbox Code Playgroud)

c unix math sqrt

3
推荐指数
1
解决办法
5503
查看次数

在C编程中寻找平方根

我试图在C编程中找到平方根.但我得到的错误是对sqrt的未定义引用.我的代码是:

#include<stdio.h>
#include<math.h>
void main(void){
int x;
int y;
printf("Enter two number numbers");
scanf("%d", &x);
scanf("%d", &y);
int result;
result = ( x * x ) + ( y * y );
double finalresult = sqrt(result);
printf("%f\n", finalresult);
}
Run Code Online (Sandbox Code Playgroud)

c square-root

3
推荐指数
1
解决办法
2万
查看次数

传递变量而不是常量时,未定义引用`sin'

我有一个在Windows中完美运行的代码但是当我尝试在linux中编译它时出现错误.我发现问题在于sin()功能.

如果我直接传递给它一个常数,它工作正常:

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

int main(void) {
    float y = sin(1.57);

    printf("sin(1.57) = %f", y);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出1:

sin(1.57) = 1.000000
Run Code Online (Sandbox Code Playgroud)

但当我传递给它一个变量我得到一个错误:

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

int main(void) {

    float x = 1.5;
    float y = sin(x);

    printf("sin(%f) = %f", x, y);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出2:

/tmp/ccfFXUZS.o: In function `main':
source-bcfaa9ff162b:(.text+0x1a): undefined reference to `sin'
collect2: error: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)

c linux gcc

3
推荐指数
1
解决办法
4424
查看次数

为什么这个小函数(在opengl中画一个圆圈)不会在c中编译?

我正在用linux中的opengl进行一些实验.我有以下功能,可以根据这些参数绘制一个圆圈.我已经包括在内了

 #include <stdlib.h>
 #include <math.h>
 #include <GL/gl.h>
 #include <GL/glut.h>
Run Code Online (Sandbox Code Playgroud)

但是当我编译时:

gcc fiver.c -o fiver -lglut
Run Code Online (Sandbox Code Playgroud)

我明白了:

   /usr/bin/ld: /tmp/ccGdx4hW.o: undefined reference to symbol 'sin@@GLIBC_2.2.5'
   /usr/bin/ld: note: 'sin@@GLIBC_2.2.5' is defined in DSO /lib64/libm.so.6 so try  
   adding it to the linker command line
  /lib64/libm.so.6: could not read symbols: Invalid operation
   collect2: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)

功能如下:

void drawCircle (int xc, int yc, int rad) {
//
// draw a circle centered at (xc,yc) with radius rad
//
  glBegin(GL_LINE_LOOP);
//
  int angle;
  for(angle …
Run Code Online (Sandbox Code Playgroud)

c opengl math

2
推荐指数
1
解决办法
6842
查看次数

math.h 中未定义的引用问题

我已将 math.h 包含在我的 c 文件中,但它保留了提示undefined reference to truncundefined reference to ceil. 我什至没有在我的文件中使用 trunc 。谁能告诉我出了什么问题吗?

c math

2
推荐指数
1
解决办法
9149
查看次数

使用math.h进行sin函数和获取错误不知道为什么

../NoteConverter.c: In function ‘main’:
../NoteConverter.c:154:9: warning: variable ‘position’ set but not used [-Wunused-but-set-variable]
Finished building: ../NoteConverter.c

Building target: NoteConverter
Invoking: GCC C Linker
gcc  -o "NoteConverter"  ./NoteConverter.o   
../NoteConverter.c:21: error: undefined reference to 'sin'
collect2: error: ld returned 1 exit status
make: *** [NoteConverter] Error 1
Run Code Online (Sandbox Code Playgroud)

以下是代码

/**
 * Frequency octave finder and play note
 */

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

 #define DURATION 5
 #define SAMPLERATE 48000
 #define AMPLITUDE 1

 //A*sin(2*pi*n*f/R)

double yCord(int n, double freq)
{
    double y= sin(2*M_PI*freq*n/48000); …
Run Code Online (Sandbox Code Playgroud)

c gcc

0
推荐指数
1
解决办法
204
查看次数