最近,我尝试创建一个库,并为此创建了两个文件:file-parser.c
以及array.c
相应的.h
文件。因此,在文件解析器中,我正在使用数组中的一个函数,该函数调用长度,并且在我编译(不链接它)时parser-file.c
,命令
gcc -c -o file-parser.o file-parser.c
gcc -c -o array.o array.c
ar r libutils.a array.o file-parser.o
不要告诉我一个错误。
问题是当我编译main.c
文件时
gcc -o prova main.c -L. -lutils
gcc返回:
array/libutils.a(file-parser.o): in function "AnalizeRow":
file-parser.c:(.text+0x109): undefined reference to "length"
array/libutils.a(file-parser.o): in the function "GetWord":
file-parser.c:(.text+0x16a): undefined reference to "length"
collect2: error: ld returned 1 exit status
我不是英语,所以我认为ld收到了错误消息。谁能解释我的错误在哪里?谢谢。
在开发过程中的任何地方都可能发生类似的错误!!!
您的问题并未全面介绍当前的开发过程。
看到您的问题后,我看到您有两个带有源代码的文件-array.c和file-parser.c。此外,您还提到了静态库libutils.a,但没有指出-您如何创建它以及现在包含什么。
我尝试重新创建您的当前位置:
文件file-parser.c
#include <stdlib.h>
int
AnalizeRow(char row[])
{
size_t len = length(row);
return 0;
}
char *
GetWord(char row[])
{
size_t len = length(row);
return NULL;
}
文件file-parser.h
int AnalizeRow(char row[]);
char* GetWord(char row[]);
文件array.c
#include <string.h>
size_t
length(char row[])
{
return strlen(row);
}
文件array.h
size_t length(char row[]);
创建空的静态库libutils.a
$ ar cr libutils.a
$ ar t libutils.a (it empty)
您的验证码
$ gcc -c -o file-parser.o file-parser.c
$ gcc -c -o array.o array.c
$ ar r libutils.a array.o file-parser.o
内容libutils.a
$ ar t libutils.a
array.o
file-parser.o
构建并运行
$ gcc -o prova main.c -L. -lutils && ./prova
我一点都没有错误。。
初步结论:
$ gcc --version
gcc (Debian 4.9.2-10) 4.9.2
$ lsb_release -a
No LSB modules are available.
Distributor ID: Debian
Description: Debian GNU/Linux 8.7 (jessie)
Release: 8.7
Codename: jessie
$ uname -a
$ apt-cache policy binutils
binutils:
Installed: 2.25-5+deb8u1
Candidate: 2.25-5+deb8u1
Version table:
*** 2.25-5+deb8u1 0
500 http://ftp.ru.debian.org/debian/ jessie-proposed-updates/main amd64 Packages
100 /var/lib/dpkg/status
2.25-5 0
500 http://ftp.ru.debian.org/debian/ jessie/main amd64 Packages
500 http://httpredir.debian.org/debian/ jessie/main amd64 Packages
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句