2014-11-06 166 views
2

所以我對構建我自己的makefiles相當陌生。我有一個奇怪的問題,我需要運行make兩次才能生成可執行文件。我有多個文件,它們位於多個子文件夾中。每個包含文件的文件夾都有自己的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 

這是我的一個子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* 

這是我得到的錯誤。

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 
+2

'$(通配符* .o)':第一次運行makefile時沒有'.o'文件,所以這將是空的。也許你的意思是'$(OBJECTS)',但是你的Makefile有各種各樣的怪異需要修復。 – user657267 2014-11-06 02:49:38

+0

您希望規則的makefile目標與規則創建的文件名相匹配。例如,您的SocketServ規則不會生成該文件。它使得../../SocketServer.o成爲規則目標。 – 2014-11-06 02:52:00

+0

這一行:$(CC)$(CFLAGS)-I $(INCLUDE_PATH)-c SocketServer.cpp需要另一個參數,或許:-o SocketServer.o – user3629249 2014-11-06 03:00:21

回答

0

首先,您必須在makefile中將子目錄描述爲make,但不知道您有子目錄。

SUBDIR = NameSubDirecory, NameSubDirecory 

對於一定要仔細閱讀Recursion

+0

我加了這個,但它似乎沒有太大的改變。我可以在沒有這個的情況下找到子目錄。 – theuglymonkey 2014-11-06 03:32:32

0

看着這行你的錯誤輸出的其他問題:

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

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

+0

是的,因爲某些原因,它不拉動目標文件。第二次抽取目標文件。我只是困惑,爲什麼它不能看到他們,如果他們是第一個產生的反正。 – theuglymonkey 2014-11-06 16:26:01

相關問題