编译错误/ usr / bin / ld:无法打开输出文件bin / server:没有这样的文件或目录

时间:2014-12-14 16:58:52

标签: c++ makefile

我正在尝试使用ubuntu中的makefile编译我的程序C.但我不知道它的问题是什么。并且有一个我无法解决的错误。

gcc -Wall -I. -pthread -ggdb -g -O0  -o bin/server server/message_queue.o server/client_thread.o server/server.o server/file.o server/datatypes.o common/datatypes.o common/error.o common/socket.o
/usr/bin/ld: cannot open output file bin/server: No such file or directory
collect2: error: ld returned 1 exit status
make: *** [bin/server] Error 1

这是一个makefile:

CC=gcc
CFLAGS=-Wall -I. -pthread -ggdb -g -O0
SERVER_OBJ=\
 server/message_queue.o \
 server/client_thread.o \
 server/server.o \
 server/file.o \
 server/datatypes.o

COMMON_OBJ=\
 common/datatypes.o \
 common/error.o \
 common/socket.o

CLIENT_OBJ=\
 client/send_thread.o \
 client/recv_thread.o \
 client/terminal_thread.o \
 client/client.o \
 client/datatypes.o

BIN=bin

all: server client

server: $(BIN)/server
client: $(BIN)/client

$(BIN)/server: $(SERVER_OBJ) $(COMMON_OBJ)
 $(CC) $(CFLAGS) $(SERVER_CFLAGS) -o $@ $^

$(BIN)/client: $(CLIENT_OBJ) $(COMMON_OBJ)
 $(CC) $(CFLAGS) $(CLIENT_CFLAGS) -o $@ $^

%.o: %.c
 $(CC) $(CFLAGS) -c -o $@ $^

clean:
 rm -f server

可能是我忘记为linux安装了一些东西?

1 个答案:

答案 0 :(得分:3)

您的问题很简单:在链接可执行文件之前,您不会检查目录bin是否存在。另外,你的makefile有点乱。那个人应该做你想做的事:

BIN         :=  bin
CLIENT      :=  $(BIN)/client
SERVER      :=  $(BIN)/server

COMMON_SRC  :=  $(wildcard common/*.c)
COMMON_OBJ  :=  $(COMMON_SRC:.c=.o)

CLIENT_SRC  :=  $(wildcard client/*.c)
CLIENT_OBJ  :=  $(CLIENT_SRC:.c=.o)

SERVER_SRC  :=  $(wildcard server/*.c)
SERVER_OBJ  :=  $(SERVER_SRC:.c=.o)

CPPFLAGS    :=  -I. -pthread
CFLAGS      :=  -Wall -ggdb -g -O0
LDFLAGS     :=  -pthread

.PHONY: all client server clean fclean

all: client server

client: $(CLIENT)
server: $(SERVER)

$(CLIENT): $(COMMON_OBJ) $(CLIENT_OBJ) | $(BIN)
    $(CC) $(LDFLAGS) $^ $(LDLIBS) -o $@

$(SERVER): $(COMMON_OBJ) $(SERVER_OBJ) | $(BIN)
    $(CC) $(LDFLAGS) $^ $(LDLIBS) -o $@

$(BIN):
    mkdir $@

clean:
    $(RM) $(COMMON_OBJ) $(CLIENT_OBJ) $(SERVER_OBJ)

fclean: clean
    $(RM) $(CLIENT) $(SERVER)