简体   繁体   中英

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 . 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. 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 ):

lex.c  lex.h  lex.o  main.c

Any help would be appreciated, thanks!

Thanks to Tom Karzes and Itagaki Fumihiko for help. The fatal flaw in my compilation was not linking main.o and 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM