Home > Blockchain >  Why do I keep getting a linker error even after compiling my header file?
Why do I keep getting a linker error even after compiling my header file?

Time:08-16

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!

CodePudding user response:

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
  • Related