【发布时间】:2014-11-06 02:43:23
【问题描述】:
所以我对构建自己的 makefile 还是很陌生。我遇到了一个奇怪的问题,我需要运行两次 make 才能生成我的可执行文件。我有多个文件,它们位于多个子文件夹中。每个包含文件的文件夹都有自己的 makefile。在那个 make 文件中,我生成我的目标文件,然后将目标文件推送到主目录。生成所有目标文件后,我将继续尝试创建可执行文件。
这是我运行其他 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
【问题讨论】:
-
$(wildcard *.o):第一次运行 makefile 时没有.o文件,所以这将是空的。也许您的意思是$(OBJECTS),但是您的 Makefile 有各种奇怪的地方需要修复。 -
您希望规则的 makefile 目标与规则创建的文件名相匹配。例如,您的 SocketServ 规则不会生成该文件。它使 ../../SocketServer.o 成为规则目标。
-
这一行:$(CC) $(CFLAGS) -I$(INCLUDE_PATH) -c SocketServer.cpp 需要另外一个参数,可能是:-o SocketServer.o
-
关于 makefile(s) 中的目标,没有使用目标名称创建的实际文件,因此需要有一行:.PHONY $(TARGETS) 和 'clean' 需要虚假目标之一。
-
makefile 正在使用 'mv' 将目标文件移动到主目录。这将从当前目录中删除文件。然后它必须重新生成。也许使用 ''cp' 来复制文件。