簡體   English   中英

為什么我需要運行我的makefile兩次來編譯我的代碼

[英]Why do I need to run my makefile twice to compile my code

因此,我對構建自己的makefile非常陌生。 我遇到一個奇怪的問題,我需要運行兩次make才能生成可執行文件。 我有多個文件,它們在多個子文件夾中。 每個包含文件的文件夾都有自己的makefile。 在該生成文件中,我生成我的目標文件,然后將目標文件推送到主目錄。 生成所有目標文件后,我將繼續嘗試創建可執行文件。

這是我運行其他makefile的主要makefile。

# 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

這是我的子makefile之一。

  # 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*

這是我得到的錯誤。

    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

首先,必須將makefile中的子目錄描述為make,這將使您不知道您擁有子目錄。

SUBDIR = NameSubDirecory, NameSubDirecory

對於其他問題,您必須閱讀遞歸制作

查看錯誤輸出的這一行:

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

正在編譯哪些文件(源文件或目標文件)以生成輸出可執行文件run 看到沒有源文件,我懷疑這是對main的未定義引用的源。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM