这是我的错误
make
cc msgd.o -o msgd
msgd.o: In function `main':
/home/cam/Desktop/lab1/msgd.cc:37: undefined reference to `Server::Server(int, bool)'
/home/cam/Desktop/lab1/msgd.cc:39: undefined reference to `Server::~Server()'
msgd.o: In function `__static_initialization_and_destruction_0':
/usr/include/c++/4.7/iostream:75: undefined reference to `std::ios_base::Init::Init()'
/usr/include/c++/4.7/iostream:75: undefined reference to `std::ios_base::Init::~Init()'
msgd.o:(.eh_frame+0x13): undefined reference to `__gxx_personality_v0'
collect2: error: ld returned 1 exit status
make: *** [msgd] Error 1
对 Server:: 的前两个未定义引用是伪造的,代码有效,并且 Server.h 正确包含在 msgd.cc 中。
然而,当我只是做“制作服务器”时它运行良好,当我做“制作客户端”时它运行良好,但我希望能够只做“制作”并让它同时构建。
这是我的生成文件。根据https://stackoverflow.com/a/10907227/2080104我需要包括 -lstdcc++ 但我似乎无法弄清楚如何在生成文件中这样做。
# Makefile for socket examples
CXX= g++ $(CCFLAGS)
msgd= msgd.o Server.o Data.o User.o Message.o Str.o
msg= msg.o Client.o Str.o
OBJS = $(msgd) $(msg)
CCFLAGS= -std=c++11 -g
all: msgd Server Data User Message Str msg Client
server:$(msgd)
$(CXX) -o msgd $(msgd)
client:$(msg)
$(CXX) -o msg $(msg)
clean:
rm -f $(OBJS) $(OBJS:.o=.d)
realclean:
rm -f $(OBJS) $(OBJS:.o=.d) msgd msg
# These lines ensure that dependencies are handled automatically.
%.d: %.cc
$(SHELL) -ec '$(CC) -M $(CPPFLAGS) $< \
| sed '\''s/\($*\)\.o[ :]*/\1.o $@ : /g'\'' > $@; \
[ -s $@ ] || rm -f $@'
include $(OBJS:.o=.d)