鱼C论坛

 找回密码
 立即注册
查看: 6841|回复: 11

[已解决]至今不懂ld returned 1 exits status是什么错误

[复制链接]
发表于 2017-4-8 10:53:40 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
应该向那个方向去想来排错呢?
最佳答案
2017-4-8 11:07:01
在Linux下创建线程时,编译时会出现下面的错误,
[root@linuxserver 807]# gcc -o 22 22.c
/tmp/cc21HcoW.o(.text+0x4c): In function `main':
: undefined reference to `pthread_create'
collect2: ld returned 1 exit status
程序为:

  1. #include <unistd.h>
  2. #include <pthread.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. void testthread(void)
  6. {
  7.         printf("I am working.\n");
  8.         printf("I am stopping.\n");
  9.         pthread_exit(0);
  10. }

  11. int main(int argc,char *argv[])
  12. {
  13.         int i=0;
  14.         pthread_t pid;
  15.         char *szP=NULL;
  16.         while(1)
  17.         {
  18.                 i++;
  19.                 pthread_create(&pid,NULL,(void *)testthread,(void *)&i);
  20.                 printf("ok%d,pid=%d\n",i,pid);
  21.                 sleep(5);
  22.         }
  23. }
复制代码

此时,只需改变编译方式
将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模块
wrong.jpg
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2017-4-8 10:56:08 | 显示全部楼层
我的问题是“应向哪个方向思考来排错”,求教
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-4-8 11:07:01 | 显示全部楼层    本楼为最佳答案   
在Linux下创建线程时,编译时会出现下面的错误,
[root@linuxserver 807]# gcc -o 22 22.c
/tmp/cc21HcoW.o(.text+0x4c): In function `main':
: undefined reference to `pthread_create'
collect2: ld returned 1 exit status
程序为:

  1. #include <unistd.h>
  2. #include <pthread.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. void testthread(void)
  6. {
  7.         printf("I am working.\n");
  8.         printf("I am stopping.\n");
  9.         pthread_exit(0);
  10. }

  11. int main(int argc,char *argv[])
  12. {
  13.         int i=0;
  14.         pthread_t pid;
  15.         char *szP=NULL;
  16.         while(1)
  17.         {
  18.                 i++;
  19.                 pthread_create(&pid,NULL,(void *)testthread,(void *)&i);
  20.                 printf("ok%d,pid=%d\n",i,pid);
  21.                 sleep(5);
  22.         }
  23. }
复制代码

此时,只需改变编译方式
将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模块
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-4-8 11:07:39 | 显示全部楼层
ld是dev c++里面的一个程序(估计是连接器),这句意思是ld执行返回状态1.至于为什么光靠你给出的信息是看不出来的,你得看这一句的上面几行的信息来判断你程序的错误。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

那为什么会返回为1呢,是哪种类型的错误呢?
为什么其他普通的错误都可以直接出来呢?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

看我上面的例子
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-4-8 12:17:34 | 显示全部楼层

看不懂喔,可以用一点好理解的话吗。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-4-8 13:27:35 | 显示全部楼层
出现“collect2: ld returned 1 exit status”的错误是因为申明了函数却没实现,一般是链接阶段出现,例如下面例子

  1. #include <iostream>
  2. using namespace std;
  3. class base0
  4. {
  5.     public:
  6.         void fun0a();   //申明了,但是未定义
  7.     protected:
  8.         int var1a;
  9. };

  10. int main()
  11. {
  12.     base0 B0;
  13.     B0.fun0a();
  14.     return 0;
  15. }
复制代码


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
编译失败。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 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[120],p[10000];

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[num] = 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[i];
                x[i] = d/k;
                d = d%k;
        }
        if(d == 0){
                while((x[length-1]==0) && (length>1))
                length--;
        }
        return d;
}

main()
{
        int count,i,j,tot,dd;
        char s[120];
        int a[120];
        scanf("%s",s);
        length =strlen(s);
        for( i =0; i <length; i++){
                a[i] =s[length-i-1] - '0';
        }
        tot =numofprime();
        i =0;
        while( i<tot && length>0){
                count =0;
                for( j=length-1; j>=0; j--){
                        temp[j] =a[j];
                }
                dd =divide(temp,p[i]);
                while( dd==0 ){
                        for( j =length-1; j>=0; j--){
                                a[j] =temp[j];
                        }
                        count++;
                        dd =divide(temp,p[i]);
                }
                if( count>0 )pritnf("%d %d\n",p,count);
                i++;
        }
        return 0;
}
[/i][/i][/i][/i][/i]
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

我找不到哪里没有定义啊
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-4-8 14:52:29 | 显示全部楼层
绾胁正太郎 发表于 2017-4-8 14:23
我找不到哪里没有定义啊

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-4-8 16:09:34 | 显示全部楼层


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

int n,m[6];
char ms[6];
int length;

int mol()
{
        int i;
        int d =0;
        for( i=0; i<length; i++)
        {
                d =m[i] + d*10;
                m[i] =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[i] =ms[i] - '0';
        }
        result =mol();
        printf("%d",result);
        return 0;
}
这个高精度求模也是这个情况,是电脑的问题吗?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-17 10:52

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表