绾胁正太郎 发表于 2017-4-8 10:53:40

至今不懂ld returned 1 exits status是什么错误

应该向那个方向去想来排错呢?

绾胁正太郎 发表于 2017-4-8 10:56:08

我的问题是“应向哪个方向思考来排错”,求教

新手·ing 发表于 2017-4-8 11:07:01

在Linux下创建线程时,编译时会出现下面的错误,
# gcc -o 22 22.c
/tmp/cc21HcoW.o(.text+0x4c): In function `main':
: undefined reference to `pthread_create'
collect2: ld returned 1 exit status
程序为:

#include <unistd.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
void testthread(void)
{
      printf("I am working.\n");
      printf("I am stopping.\n");
      pthread_exit(0);
}

int main(int argc,char *argv[])
{
      int i=0;
      pthread_t pid;
      char *szP=NULL;
      while(1)
      {
                i++;
                pthread_create(&pid,NULL,(void *)testthread,(void *)&i);
                printf("ok%d,pid=%d\n",i,pid);
                sleep(5);
      }
}
此时,只需改变编译方式
将gcc -o 22 22.c 改变为 gcc -O2 -Wall -o 22 22.c -lpthread

最关键的是-lpthread

根据错误
/tmp/cc21HcoW.o(.text+0x4c): In function `main':
: undefined reference to `pthread_create'
collect2: ld returned 1 exit status
可以看出是在ld的时候系统无法找到pthread_create函数。也就是说编译器在link得时候找不到其中的一个使用库的函数。
如果差pthread_create的话可以发现其在pthread.so中,所以需要增加 -lpthread编译参数,告诉linker在link的时候使用pthread模块

新手·ing 发表于 2017-4-8 11:07:39

ld是dev c++里面的一个程序(估计是连接器),这句意思是ld执行返回状态1.至于为什么光靠你给出的信息是看不出来的,你得看这一句的上面几行的信息来判断你程序的错误。

绾胁正太郎 发表于 2017-4-8 11:40:48

新手·ing 发表于 2017-4-8 11:07
ld是dev c++里面的一个程序(估计是连接器),这句意思是ld执行返回状态1.至于为什么光靠你给出的信息是看 ...

那为什么会返回为1呢,是哪种类型的错误呢?
为什么其他普通的错误都可以直接出来呢?

新手·ing 发表于 2017-4-8 11:42:40

绾胁正太郎 发表于 2017-4-8 11:40
那为什么会返回为1呢,是哪种类型的错误呢?
为什么其他普通的错误都可以直接出来呢?

看我上面的例子

绾胁正太郎 发表于 2017-4-8 12:17:34

新手·ing 发表于 2017-4-8 11:42
看我上面的例子

看不懂喔,可以用一点好理解的话吗。{:10_254:}

新手·ing 发表于 2017-4-8 13:27:35

出现“collect2: ld returned 1 exit status”的错误是因为申明了函数却没实现,一般是链接阶段出现,例如下面例子

#include <iostream>
using namespace std;
class base0
{
    public:
      void fun0a();   //申明了,但是未定义
    protected:
      int var1a;
};

int main()
{
    base0 B0;
    B0.fun0a();
    return 0;
}

g++ -Wall -o "const_initial" "const_initial.cpp" (在目录 E:\1_MYPROJECT\cTest\keyword 中)
C:\Users\ADMINI~1\AppData\Local\Temp\ccSSyAfQ.o:const_initial.cpp:(.text+0x15): undefined reference to `base0::fun0a()'
collect2.exe: error: ld returned 1 exit status
编译失败。

绾胁正太郎 发表于 2017-4-8 14:21:58

新手·ing 发表于 2017-4-8 13:27
出现“collect2: ld returned 1 exit status”的错误是因为申明了函数却没实现,一般是链接阶段出现,例如 ...

#include<stdio.h>
#include<math.h>
#include<string.h>
int length,temp,p;

int numofprime()
{
      int i,j,num = 0;
      for( i=2; i<=65535; i++){
                j=2;
                while( i%j!=0 && j<=sqrt(i) ){
                j++;
            }
                if( j >sqrt(i) ){
                        p = i;
                        num++;
                        
                }
      }
      return num;
}

int divide( int x[], int k)
{
      int i,d = 0;
      for( i=length-1; i>=0; i--)
      {
                d = d*10 + x;
                x = d/k;
                d = d%k;
      }
      if(d == 0){
                while((x==0) && (length>1))
                length--;
      }
      return d;
}

main()
{
      int count,i,j,tot,dd;
      char s;
      int a;
      scanf("%s",s);
      length =strlen(s);
      for( i =0; i <length; i++){
                a =s - '0';
      }
      tot =numofprime();
      i =0;
      while( i<tot && length>0){
                count =0;
                for( j=length-1; j>=0; j--){
                        temp =a;
                }
                dd =divide(temp,p);
                while( dd==0 ){
                        for( j =length-1; j>=0; j--){
                              a =temp;
                        }
                        count++;
                        dd =divide(temp,p);
                }
                if( count>0 )pritnf("%d %d\n",p,count);
                i++;
      }
      return 0;
}

绾胁正太郎 发表于 2017-4-8 14:23:02

新手·ing 发表于 2017-4-8 13:27
出现“collect2: ld returned 1 exit status”的错误是因为申明了函数却没实现,一般是链接阶段出现,例如 ...

我找不到哪里没有定义啊{:10_266:}

新手·ing 发表于 2017-4-8 14:52:29

绾胁正太郎 发表于 2017-4-8 14:23
我找不到哪里没有定义啊

{:10_250:}

绾胁正太郎 发表于 2017-4-8 16:09:34

新手·ing 发表于 2017-4-8 14:52



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

int n,m;
char ms;
int length;

int mol()
{
        int i;
        int d =0;
        for( i=0; i<length; i++)
        {
                d =m + d*10;
                m =d/n;
                d =d%n;
        }
        return d;
}


main()
{
        int i,result;
        scnaf("%d",&n);
        scnaf("%s",ms);
        length =strlen(ms);//存m的位数
        for( i =0; i<length; i++){
                m =ms - '0';
        }
        result =mol();
        printf("%d",result);
        return 0;
}
这个高精度求模也是这个情况,是电脑的问题吗?
页: [1]
查看完整版本: 至今不懂ld returned 1 exits status是什么错误