ld returned 1 exit status这类问题如何处理

本文介绍了解决C语言中使用异步IO函数时遇到的编译错误的方法。通过添加正确的链接库,解决了未定义的函数引用问题。

问题描述

编译的时候经常会报出的问题

例子

异步IO的例子

#include <aio.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
void main()
{
    struct aiocb stCb;
    int fd = 0;
    int iRet = 0;
    fd = open("file.txt",O_RDONLY);
    if(-1 == fd)
    {
        perror("open");
    }
    memset(&stCb, 0, sizeof(stCb));
    stCb.aio_buf = malloc(512+1);
    if(!stCb.aio_buf)
    {
        perror("malloc");
    }
    stCb.aio_fildes = fd;
    stCb.aio_nbytes = 512;
    stCb.aio_offset = 0;
    if(aio_read(&stCb) < 0)
    {
        perror("aio_read");
    }
    while(EINPROGRESS  == aio_error(&stCb));
    if(aio_return(&stCb) > 0)
    {
        printf("OK\n");
    }
    else
    {
        printf("error\n");
    }

    close(fd);
    printf("Hello,world\n");
}

不知道连接的库,所以直接编译会出现如下错误:
linux-pm2g:/testCPro/AIO # gcc -g aio.c -o aio
aio.c: In function ‘main’:
aio.c:7: warning: return type of ‘main’ is not ‘int’
/tmp/cclS8Pwa.o: In function ‘main’:
/mnt/hgfs/E/test/testCPro/AIO/aio.c:25: undefined reference to ‘aio_read’
/mnt/hgfs/E/test/testCPro/AIO/aio.c:29: undefined reference to ‘aio_error’
/mnt/hgfs/E/test/testCPro/AIO/aio.c:30: undefined reference to ‘aio_return’
collect2: ld returned 1 exit status

解决方法

找到函数对应库的编译连接。通过man查看错误提示里的未关联函数。
这里写图片描述
好,我们找到了-lrt
然后编译一下看看,到底行不行
linux-pm2g:/testCPro/AIO # gcc -g aio.c -o aio -lrt
aio.c: In function ‘main’:
aio.c:7: warning: return type of ‘main’ is not ‘int’
我们看到通过了。

类推

其他函数编译时要是出现这种情况,也可以通过同样的方式来解决。

`ld returned 1 exit status` 是一个编译错误信息,通常出现在使用 GCC(GNU Compiler Collection)编译 C 或 C++ 程序时。这个错误信息表示**链接器(linker)在链接阶段失败了**,返回了状态码 1,这意味着链接过程中出现了某种错误。 --- ### 常见原因及解决办法 以下是导致这个错误的一些常见原因及其对应的解决方法: --- ### 1. **函数或变量重复定义** 如果你在多个地方定义了相同的函数或全局变量,链接器会报错。 #### 示例代码: ```c // main.c #include <stdio.h> int global_var = 10; int main() { printf("Hello\n"); return 0; } ``` ```c // other.c int global_var = 20; // 重复定义了 global_var void dummy() {} ``` #### 编译命令: ```bash gcc main.c other.c -o program ``` #### 错误信息: ``` ld returned 1 exit status ``` #### 解决办法: - 使用 `extern` 声明全局变量而不是重复定义。 - 或者将变量定义放在一个源文件中,在其他文件中使用 `extern int global_var;`。 --- ### 2. **缺少主函数 `main`** 如果你没有定义 `main()` 函数,链接器会找不到程序入口。 #### 示例代码: ```c #include <stdio.h> void my_function() { printf("Hello\n"); } ``` #### 编译命令: ```bash gcc file.c -o program ``` #### 错误信息: ``` ld returned 1 exit status ``` #### 解决办法: 添加 `main()` 函数作为程序入口。 ```c int main() { my_function(); return 0; } ``` --- ### 3. **函数声明了但未实现** 如果你声明了一个函数但没有实现它,而程序中又调用了它,链接器会报错。 #### 示例代码: ```c #include <stdio.h> void missing_function(); // 声明但未定义 int main() { missing_function(); // 调用未定义的函数 return 0; } ``` #### 编译命令: ```bash gcc main.c -o program ``` #### 错误信息: ``` ld returned 1 exit status ``` #### 解决办法: 提供函数的定义: ```c void missing_function() { printf("Function implemented\n"); } ``` --- ### 4. **链接库缺失** 如果你使用了某个库函数但没有链接对应的库,也会导致链接失败。 #### 示例代码: ```c #include <math.h> int main() { double result = sqrt(16.0); return 0; } ``` #### 错误编译命令: ```bash gcc main.c -o program ``` #### 错误信息: ``` undefined reference to 'sqrt' ld returned 1 exit status ``` #### 解决办法: 链接数学库: ```bash gcc main.c -o program -lm ``` --- ### 5. **多个 main 函数** 如果你在多个源文件中定义了 `main()` 函数,链接器会报错。 #### 示例: ```c // file1.c int main() { return 0; } // file2.c int main() { return 0; } ``` #### 编译命令: ```bash gcc file1.c file2.c -o program ``` #### 错误信息: ``` ld returned 1 exit status ``` #### 解决办法: 只保留一个 `main()` 函数。 --- ### 总结 `ld returned 1 exit status` 是链接器错误,通常意味着你的程序在链接阶段出了问题,可能的原因包括: - 函数或变量重复定义 - 缺少 `main()` 函数 - 函数声明但未定义 - 没有链接所需的库 - 多个 `main()` 函数 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值