Two stage compiling fails while single stage succeeds

Viewed 25

I am trying to compile a simple Antlr4 project. When I do it in single g++ command, it compiles successfully. However, when I compile and link separately, it fails.

Single stage compilation (succeeds):

g++ -std=c++11 -Wall -Wextra *.cpp -L. -l antlr4-runtime -I/usr/local/include/antlr4-runtime -. -o exec

Two stage (fails: cannot link properly to antlr library and gives a lot of undefined reference errors):

main: $(OBJS)
          g++ -L. -l antlr4-runtime $^ -o exec
%.o: %.cpp
          g++ -std=c++11 -Wall -Wextra -I/usr/local/include/antlr4-runtime -I. -c $< 

I wonder why linking fails in the latter case, while it has similar linking options to single stage compiling.

0 Answers
Related