【问题标题】:while compiling a code using gcc on mac OS linker failed in linking custom made static library在 Mac OS 链接器上使用 gcc 编译代码时链接自定义静态库失败
【发布时间】:2020-02-16 18:58:36
【问题描述】:

这是我的 c 语言项目文件的文件结构:
第一个是完整课程文件夹
fullCourse 文件夹包含三个文件夹:
1. 包括
2. 源
3.库
4. 测试
在包含文件夹中:
它包含一个头文件:temp.h

temp.h 的代码如下:

#ifndef __$__temp_h
#define __$__temp_h 234
int yash();
#endif

在 src 文件夹中:
它包含一个源文件:temp.c

temp.cc 的代码如下:

#ifndef __$__temp_c
#define __$__temp_c 234
int yash()
{
return 22;
}
#endif

然后留在同一个文件夹中,创建 .o 文件如下:

gcc -I ../include -c temp.c

下一步是将这个 temp.o 文件移动到 lib 文件夹,如下所示:

mv temp.o ../lib

比留在 lib 文件夹中创建一个存档(或库)文件如下:

ar rcs libtmds.a temp.o

比在test文件夹里写了一个测试库的源代码(tempTest.c)
tempTest.c 的代码如下:

#include<stdio.h>
#include<temp.h>
int main()
{
int w;
w=yash();
printf("%d\n",w);
return 0;
}

比留在测试文件夹中尝试编译它如下:

gcc -static -I ../include tempTest.c -L ../lib -ltmds -o tempTest.exe

但是代码没有编译,显示如下错误:

ld: library not found for -lcrt0.o
clang: error: linker command failed with exit code 1 (use -v to see invocation)

请帮我解决这个问题。

【问题讨论】:

  • 什么是crt0.o?链接器找不到该文件。

标签: macos gcc linker static-libraries linker-flags


【解决方案1】:

-static 在这里出错。要链接到您的静态库,只需简单地链接到它,不带任何 -static 标志,用于内核编译。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-16
    • 2017-05-07
    • 2016-02-12
    • 1970-01-01
    • 1970-01-01
    • 2013-09-06
    • 1970-01-01
    相关资源
    最近更新 更多