-1

So I have a subdirectory with several files and need to link with it. Inside the .c files I have an include that looks somewhat like:

#include "subdirectory/header.h"

This header file includes functions such as lex() that I am using and my output on compiling is:

 cc -IlexicalAnalyzer -Wall   -c -o parser.o parser.c
 cc -IlexicalAnalyzer -Wall   -c -o recognizer.o recognizer.c
 g++      -IlexicalAnalyzer -Wall parser.o recognizer.o  -o recognizer
 parser.o: In function `advance':
 parser.c:(.text+0x36): undefined reference to `lex'
 recognizer.o: In function `recognizer':
 recognizer.c:(.text+0xd): undefined reference to `newLexer'
 collect2: error: ld returned 1 exit status
 make: *** [recognizer] Error 1

What am I doing wrong?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Jeremy H.
  • 13
  • 3

1 Answers1

2

You have included the header files from the subdirectory, that's good because that declares them for the compiler, but there should also be some source files there that you need to compile and then link. Those source files should define the lex and newLexer functions that are referred to by the header files.

MadScientist
  • 92,819
  • 9
  • 109
  • 136
  • Yes I do have the source files in this subdirectory as well. They are defined. – Jeremy H. Sep 18 '13 at 14:45
  • 1
    But you have to _compile_ the sources and _link_ the resulting objects into your program, or you get errors such as the above. They won't get added by magic. – MadScientist Sep 18 '13 at 16:12
  • The name of the subdirectory is lexicalAnalyzer I thought that the use of `-IlexicalAnalyzer` does add them to the linking list. Obviously I'm wrong on that, but I can't seem to find any other explanation of linking a subdirectory. – Jeremy H. Sep 21 '13 at 22:34
  • You don't "link a subdirectory". You link object files. If there are more than one object file you have to list them all individually, even if they're in subdirectories. It sounds like you need a basic tutorial; try searching for compiling and linking tutorials on Google. You can start with this: http://stackoverflow.com/questions/6264249/how-does-the-compilation-linking-process-work. The `-I` flag tells the preprocessor where to look for `#include` files, it has nothing to do with linking object files. – MadScientist Sep 22 '13 at 03:35