链接器只能在.a中找到.o的一部分。
我正在尝试使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论