简体   繁体   中英

Why do I need to run my makefile twice to compile my code

So I am fairly new to building my own makefiles. I am having a weird issue where I need to run make twice in order for my executable to be generated. I have multiple files and they are in multiple sub folders. Each folder that contains files has there own makefile. In that make file I generate my object files and then push the object files to the main directory. Once all of my object files are generated I continue to try and create my executable.

This is my main makefile that runs the other makefiles.

# Declaration of variables
CC = g++
CFLAGS=-std=c++0x -Wall -lboost_system -lpthread
INCLUDE_PATH0=SocketIO/SocketServer/
INCLUDE_PATH1=SocketIO/SocketServer/SocketSession/


EXEC = run
SOURCES = $(wildcard *.cpp)
OBJECTS = $(SOURCES:.cpp=.o)

TARGETS = SocketSess SocketServ maincpp

$(EXEC): $(TARGETS) $(wildcard *.o)
  $(CC) $(wildcard *.o) $(CFLAGS) -o $(EXEC)

maincpp: $(SOURCES)
  $(CC) $(CFLAGS) -I$(INCLUDE_PATH0) -I$(INCLUDE_PATH1) -c $(SOURCES)
  ls
SocketSess:
  cd ./SocketIO/SocketServer/SocketSession ; make SocketSess

SocketServ:
  cd ./SocketIO/SocketServer ; make SocketServ

# To remove generated files
clean:
  rm -f $(EXEC) $(wildcard *.o)
  cd ./SocketIO/SocketServer/SocketSession ; make clean
  cd ./SocketIO/SocketServer ; make clean

This is one of my sub makefiles.

  # Declaration of variables
  CC = g++
  CFLAGS=-std=c++0x -Wall -lboost_system -lpthread
  INCLUDE_PATH=SocketSession/

  #Just build object file do not link yet
  SocketServ: SocketServer.cpp SocketServer.h
    $(CC) $(CFLAGS) -I$(INCLUDE_PATH) -c SocketServer.cpp
    mv *.o ../../

  clean:
    rm -f *.o*

This is the error I get.

    Main.cpp  Main.o  Makefile  SocketIO  SocketServer.o  SocketSession.o  test
    g++  -std=c++0x -Wall -lboost_system -lpthread -o run
    /usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 0 has invalid symbol index 11
    /usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 1 has invalid symbol index 12
    /usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 2 has invalid symbol index 2
    /usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 3 has invalid symbol index 2
    /usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 4 has invalid symbol index 11
    /usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 5 has invalid symbol index 13
    /usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 6 has invalid symbol index 13
    /usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 7 has invalid symbol index 13
    /usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 8 has invalid symbol index 12
    /usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 9 has invalid symbol index 13
    /usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 10 has invalid symbol index 13
    /usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 11 has invalid symbol index 13
    /usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 12 has invalid symbol index 13
    /usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 13 has invalid symbol index 13
    /usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 14 has invalid symbol index 13
    /usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 15 has invalid symbol index 13
    /usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 16 has invalid symbol index 13
    /usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 17 has invalid symbol index 13
    /usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 18 has invalid symbol index 13
    /usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 19 has invalid symbol index 21
    /usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_line): relocation 0 has invalid symbol index 2
    /usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/crt1.o: In function `_start':
    (.text+0x20): undefined reference to `main'
    collect2: error: ld returned 1 exit status
    make: *** [run] Error 1

First thing that you have to describe subdirectory in makefile as make would not know you have sub directories.

SUBDIR = NameSubDirecory, NameSubDirecory

For other problems you have to read Recursion Make

Looking at this line of your error output:

g++  -std=c++0x -Wall -lboost_system -lpthread -o run

What files (either source files or object files) are being compiled to produce the output executable run ? Seeing as how there are no source files, I suspect that this is the source of the undefined reference to main.

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