[英]Why do I keep getting a linker error even after compiling my header file?
I keep getting this error when compiling!编译时总是出现这个错误!
Undefined symbols for architecture x86_64:
"_lex", referenced from:
_main in main-7a45f7.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I am compiling a file main.c
.我正在编译一个文件main.c
。 Here are the first few lines of the program:这是程序的前几行:
#include <stdio.h>
#include "lex.h"
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
I thought compiling the header file "lex.h"
might have been the issue, but I was apparently wrong.我认为编译 header 文件"lex.h"
可能是问题所在,但我显然错了。 I ran the command我运行了命令
gcc -c lex.c lex.o
and received the following warning (but no errors):并收到以下警告(但没有错误):
clang: warning: lex.o: 'linker' input unused [-Wunused-command-line-argument]
I think I have included all the information needed, but these are the files in my current directory (as per ls
):我想我已经包含了所有需要的信息,但这些是我当前目录中的文件(根据ls
):
lex.c lex.h lex.o main.c
Any help would be appreciated, thanks!任何帮助将不胜感激,谢谢!
Thanks to Tom Karzes and Itagaki Fumihiko for help.感谢Tom Karzes和板垣文彦的帮助。 The fatal flaw in my compilation was not linking main.o
and lex.o
.我的编译中的致命缺陷是没有链接main.o
和lex.o
。 To compile the files properly, this is what you should actually do:要正确编译文件,您实际上应该执行以下操作:
gcc -c lex.c
gcc -c main.c
gcc main.o lex.o
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.