简体   繁体   English

为什么即使在编译了我的 header 文件后,我仍然会收到 linker 错误?

[英]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.olex.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.

相关问题 为什么在编译我的 Unix 复制程序时会收到错误“文件格式无法识别;视为链接描述文件”? - Why do I get the error "file format not recognized; treating as linker script" when compiling my Unix copy program? 编译后链接器错误 - linker error after compiling 为什么我不断收到此错误? - Why do I keep getting this error? 在MAC OSX上编译简单的mysql c应用程序时,为什么为什么总是得到未定义的符号? - Why do I keep getting undefined symbols when compiling a simple mysql c application on MAC OSX? 无法弄清楚为什么我在使用C打开文件时总是出错 - Can't figure out why do I keep getting an error opening a file in C 为什么我的 C 程序总是出现分段错误 - Why do I keep getting segmentation fault in my C program 为什么我在字符串的末尾总是得到多余的字符? - Why do I keep getting extra characters at the end of my string? 我一直从我的方程中得到零,不知道为什么 - I keep getting zero from my equations and do not know why 尝试从.c文件中的标头使用结构时,为什么会出现类型错误? - Why am I getting a type error when trying to use a struct from a header in my .c file? RecyclerView:未连接适配器-为什么我不断收到此错误? - RecyclerView: No adapter attached - why do I keep getting this error?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM