小编ale*_*lin的帖子

仅当参数不是常量时,math.h中的sqrt才会导致链接器错误"未定义对sqrt的引用"

我创建了一个小程序,如下所示:

  #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)

c compiler-errors compilation

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

pthread_create后跟pthread_detach仍会导致Valgrind中可能丢失的错误

我有一个问题,Valgrind告诉我,我可能会丢失一些内存:

==23205== 544 bytes in 2 blocks are possibly lost in loss record 156 of 265
==23205==    at 0x6022879: calloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==23205==    by 0x540E209: allocate_dtv (in /lib/ld-2.12.1.so)
==23205==    by 0x540E91D: _dl_allocate_tls (in /lib/ld-2.12.1.so)
==23205==    by 0x623068D: pthread_create@@GLIBC_2.2.5 (in /lib/libpthread-2.12.1.so)
==23205==    by 0x758D66: MTPCreateThreadPool (MTP.c:290)
==23205==    by 0x405787: main (MServer.c:317)
Run Code Online (Sandbox Code Playgroud)

创建这些线程的代码(MTPCreateThreadPool)基本上获取一个等待pthread_t槽的块的索引,并创建一个带有该线程的线程.TI成为指向具有线程索引和pthread_t的结构的指针.(简化的/消毒):

for (tindex = 0; tindex < NumThreads; tindex++)
  {
  int rc;
  TI = &TP->ThreadInfo[tindex];
  TI->ThreadID = tindex;

  rc = pthread_create(&TI->ThreadHandle,NULL,MTPHandleRequestsLoop,TI);
  /* check for non-success that I've omitted */
  pthread_detach(&TI->ThreadHandle); …
Run Code Online (Sandbox Code Playgroud)

c memory multithreading memory-leaks pthreads

6
推荐指数
2
解决办法
2417
查看次数