链接器只能在.a中找到.o的一部分。

发布于 2025-01-17 15:11:11 字数 1477 浏览 3 评论 0原文

我正在尝试使用mingw-w64在Windows上创建一个静态Lib。
catalog
静态lib src:

// test.h
#ifndef __TEST_H
#define __TEST_H

int add(int a, int b);
int div(int a, int b);
int sub(int a, int b);

#endif

// add.c
#include"test.h"

int add(int a, int b)
{
    return a + b;
}

// sub.c
#include"test.h"

int sub(int a, int b)
{
   return a - b;
}

// div.c
#include"test.h"

int div(int a, int b)
{
    return a / b;
}

我使用这些命令来创建

PS D:\ThisPC\Desktop\dlltest> gcc -c *.c
PS D:\ThisPC\Desktop\dlltest> ar rcs libtest.a *.o

使用此lib的应用程序的静态lib:src:

#include<stdio.h>
#include"test.h"

int main()
{
    int m, n;
    printf("Input two bumbers:\n");
    scanf("%d %d", &m, &n);

    printf("%d / %d = %d\n", m, n, div(m, n));
    printf("%d + %d = %d\n", m, n, add(m, n));
    printf("%d - %d = %d\n", m, n, sub(m, n));

    return 0;
}

一切都很好。但是当我使用lib时,我会得到链接错误。

PS D:\ThisPC\Desktop\dlltest\app> gcc -I./include -L./lib -ltest -o app main.c  
C:\Users\Admin\AppData\Local\Temp\cc04bSGs.o:main.c:(.text+0x6a): undefined reference to `add'
C:\Users\Admin\AppData\Local\Temp\cc04bSGs.o:main.c:(.text+0x93): undefined reference to `sub'
collect2.exe: error: ld returned 1 exit status

如果我删除printf(add())和printf(sub(sub()),则可以工作。
链接器只能在.a文件中找到DIV标签。

I am trying to use mingw-w64 to create a static lib on windows.
Catalog
Static Lib src:

// test.h
#ifndef __TEST_H
#define __TEST_H

int add(int a, int b);
int div(int a, int b);
int sub(int a, int b);

#endif

// add.c
#include"test.h"

int add(int a, int b)
{
    return a + b;
}

// sub.c
#include"test.h"

int sub(int a, int b)
{
   return a - b;
}

// div.c
#include"test.h"

int div(int a, int b)
{
    return a / b;
}

I use these command to create static lib:

PS D:\ThisPC\Desktop\dlltest> gcc -c *.c
PS D:\ThisPC\Desktop\dlltest> ar rcs libtest.a *.o

Src of app which use this lib:

#include<stdio.h>
#include"test.h"

int main()
{
    int m, n;
    printf("Input two bumbers:\n");
    scanf("%d %d", &m, &n);

    printf("%d / %d = %d\n", m, n, div(m, n));
    printf("%d + %d = %d\n", m, n, add(m, n));
    printf("%d - %d = %d\n", m, n, sub(m, n));

    return 0;
}

Everything is OK.But when I use the lib, I get link error.

PS D:\ThisPC\Desktop\dlltest\app> gcc -I./include -L./lib -ltest -o app main.c  
C:\Users\Admin\AppData\Local\Temp\cc04bSGs.o:main.c:(.text+0x6a): undefined reference to `add'
C:\Users\Admin\AppData\Local\Temp\cc04bSGs.o:main.c:(.text+0x93): undefined reference to `sub'
collect2.exe: error: ld returned 1 exit status

If I delete printf(add()) and printf(sub()) ,it can work.
Linker can only find div label in .a file.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文