我一直在努力让服务器/客户端运行一个简单的单线程信使服务。 它能够在Visual Studio上编译,但是当我尝试在Linux上通过makefile进行编译时,使用以下命令: make client
,我得到一个非常大的错误列表。 我将提供开始部分和错误列表:
这里说它至less试图使用g ++:
g ++ -g -c -o client.o client.cc cc client.o -o client client.o:in
main': /home/peter/Desktop/CS360/Messaging_Service/messenger/client.cc:10: undefined reference to
Messaging_Service/messenger/client.ccmain': /home/peter/Desktop/CS360/Messaging_Service/messenger/client.cc:10: undefined reference to
std :: allocator :: allocator()'编辑:我注意到这里不寻常的东西。 当我打电话
make client
而不打电话make clean
我得到以下内容:
- SIOCGIWFREQ ioctl返回错误22 EINVAL – 无效的参数,为什么?
- subprocess打印错误ppid()
- Linux Cnetworking通信程序在debugging器中工作,但不在外部
- 应用程序正在无故死亡。 怀疑高BSS。 如何debugging呢?
- 我添加了一个MAX7320 i2c输出芯片。 我怎样才能让内核加载它的驱动程序?
peter @ peter-VirtualBox:〜/ Desktop / CS360 / Messaging_Service / messenger $ make client cc client.o -o client client.o:in
main': /home/peter/Desktop/CS360/Messaging_Service/messenger/client.cc:10: undefined reference to
/home/peter/Desktop/CS360/Messaging_Service/messenger/main': /home/peter/Desktop/CS360/Messaging_Service/messenger/client.cc:10: undefined reference to
std :: allocator :: allocator()'不知道这是否重要,但它不再提及g ++。
说错误的代码片段:
/home/peter/Desktop/CS360/Messaging_Service/messenger/client.cc:7: undefined reference to `std::allocator<char>::allocator()' /home/peter/Desktop/CS360/Messaging_Service/messenger/client.cc:7: undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> const&)' /home/peter/Desktop/CS360/Messaging_Service/messenger/client.cc:7: undefined reference to `std::allocator<char>::~allocator()' /home/peter/Desktop/CS360/Messaging_Service/messenger/client.cc:15: undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::operator=(char const*)' /home/peter/Desktop/CS360/Messaging_Service/messenger/client.cc:18: undefined reference to `std::cout' /home/peter/Desktop/CS360/Messaging_Service/messenger/client.cc:18: undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)' /home/peter/Desktop/CS360/Messaging_Service/messenger/client.cc:18: undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)' /home/peter/Desktop/CS360/Messaging_Service/messenger/client.cc:18: undefined reference to `std::ostream::operator<<(std::ostream& (*)(std::ostream&))' /home/peter/Desktop/CS360/Messaging_Service/messenger/client.cc:23: undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)' /home/peter/Desktop/CS360/Messaging_Service/messenger/client.cc:23: undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()' /home/peter/Desktop/CS360/Messaging_Service/messenger/client.cc:7: undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()' /home/peter/Desktop/CS360/Messaging_Service/messenger/client.cc:25: undefined reference to `std::allocator<char>::~allocator()' /home/peter/Desktop/CS360/Messaging_Service/messenger/client.cc:25: undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()' /home/peter/Desktop/CS360/Messaging_Service/messenger/client.cc:7: undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()' client.o: In function `Client::Client(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, int)': /home/peter/Desktop/CS360/Messaging_Service/messenger/client.cc:27: undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string()' /home/peter/Desktop/CS360/Messaging_Service/messenger/client.cc:27: undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string()' /home/peter/Desktop/CS360/Messaging_Service/messenger/client.cc:29: undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::operator=(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)' /home/peter/Desktop/CS360/Messaging_Service/messenger/client.cc:32: undefined reference to `operator new[](unsigned long)'
这导致我相信我的makefile有一个错误。 我使用g ++,我相信它也被设置为很好,所以我不太确定从哪里去。 我的Makefile是:
CXX= g++ $(CCFLAGS) MSG-SERVER= server.o MSG-CLIENT= client.o OBJS = $(MSG-SERVER) $(MSG-CLIENT) LIBS = CCFLAGS = -g all: msg-server msg-client msg-server:$(MSG-SERVER) $(CXX) -o server $(MSG-SERVER) $(LIBS) msg-client:$(MSG-CLIENT) $(CXX) -o client $(MSG-CLIENT) $(LIBS) clean: rm -f $(OBJS) $(OBJS:.o=.d) realclean: rm -f $(OBJS) $(OBJS:.o=.d) server client # 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)
如果需要的话,我也会链接我的代码: https : //bitbucket.org/picklepetters/messenger/src/44e4fa12541948215f874b56e4ba0dedea1766f5/client.cc?at=master&fileviewer=file-view-default
当我用make -d
跟踪make时,它会给我大量的数据。 我会张贴一些片段,因为我真的不知道我在看什么:
Trying implicit prerequisite '/usr/include/c++/5/SCCS/s.stdexcept.F'. Trying pattern rule with stem 'stdexcept'. Trying implicit prerequisite '/usr/include/c++/5/stdexcept.r'. Looking for a rule with intermediate file '/usr/include/c++/5/stdexcept.r'. Avoiding implicit rule recursion. Avoiding implicit rule recursion. Avoiding implicit rule recursion. Trying pattern rule with stem 'stdexcept'. Rejecting impossible implicit prerequisite '/usr/include/c++/5/stdexcept.l'. Trying pattern rule with stem 'stdexcept.r'. Trying implicit prerequisite '/usr/include/c++/5/stdexcept.r,v'. Trying pattern rule with stem 'stdexcept.r'. Trying implicit prerequisite '/usr/include/c++/5/RCS/stdexcept.r,v'. Trying pattern rule with stem 'stdexcept.r'. Trying implicit prerequisite '/usr/include/c++/5/RCS/stdexcept.r'. Trying pattern rule with stem 'stdexcept.r'. Trying implicit prerequisite '/usr/include/c++/5/s.stdexcept.r'. Trying pattern rule with stem 'stdexcept.r'. Trying implicit prerequisite '/usr/include/c++/5/SCCS/s.stdexcept.r'. Trying pattern rule with stem 'stdexcept'. Rejecting impossible implicit prerequisite '/usr/include/c++/5/stdexcept.F'. Trying pattern rule with stem 'stdexcept'. Trying implicit prerequisite '/usr/include/c++/5/stdexcept.m'. Looking for a rule with intermediate file '/usr/include/c++/5/stdexcept.m'. Avoiding implicit rule recursion. Avoiding implicit rule recursion. Trying pattern rule with stem 'stdexcept'. Trying implicit prerequisite '/usr/include/c++/5/stdexcept.ym'. Trying pattern rule with stem 'stdexcept.m'. Trying implicit prerequisite '/usr/include/c++/5/stdexcept.m,v'. Trying pattern rule with stem 'stdexcept.m'. Trying implicit prerequisite '/usr/include/c++/5/RCS/stdexcept.m,v'. Trying pattern rule with stem 'stdexcept.m'. Trying implicit prerequisite '/usr/include/c++/5/RCS/stdexcept.m'. Trying pattern rule with stem 'stdexcept.m'.
这就是说,直到完成目标文件的先决条件时,可能要等到150?
Finished prerequisites of target file 'server.d'. Prerequisite 'server.cc' is older than target 'server.d'. Prerequisite 'server.cc' is older than target 'server.d'. Prerequisite '/usr/include/stdc-predef.h' is older than target 'server.d'. Prerequisite 'server.h' is older than target 'server.d'. Prerequisite '/usr/include/errno.h' is older than target 'server.d'. Prerequisite '/usr/include/features.h' is older than target 'server.d'. Prerequisite '/usr/include/x86_64-linux-gnu/sys/cdefs.h' is older than target 'server.d'. Prerequisite '/usr/include/x86_64-linux-gnu/bits/wordsize.h' is older than target 'server.d'. Prerequisite '/usr/include/x86_64-linux-gnu/gnu/stubs.h' is older than target 'server.d'. Prerequisite '/usr/include/x86_64-linux-gnu/gnu/stubs-64.h' is older than target 'server.d'. Prerequisite '/usr/include/x86_64-linux-gnu/bits/errno.h' is older than target 'server.d'.
对于最后的100行,它指出:
Must remake target 'server.o'. g++ -g -c -o server.o server.cc Putting child 0x13601a0 (server.o) PID 7768 on the chain. Live child 0x13601a0 (server.o) PID 7768 server.cc: In function 'int main(int, char**)': server.cc:19:17: error: 'cout' was not declared in this scope cout << "server [-p port]" << endl; ^ server.cc:19:47: error: 'endl' was not declared in this scope cout << "server [-p port]" << endl; ^ server.cc: In constructor 'Server::Server(int)': server.cc:34:30: error: '>>' should be '> >' within a nested template argument list map<string, vector<string>> mappedMessages; ^ server.cc: At global scope: server.cc:121:36: error: 'String' has not been declared Server::handle_message(int client, String message) { ^ server.cc:121:1: error: prototype for 'void Server::handle_message(int, int)' does not match any in class 'Server' Server::handle_message(int client, String message) { ^ In file included from server.cc:1:0: server.h:33:12: error: candidate is: std::__cxx11::string Server::handle_message(int, std::__cxx11::string) string handle_message(int, string); ^ server.cc:127:35: error: 'String' has not been declared Server::parse_message(int client, String message) { ^ server.cc:127:1: error: prototype for 'std::__cxx11::string Server::parse_message(int, int)' does not match any in class 'Server' Server::parse_message(int client, String message) { ^ In file included from server.cc:1:0: server.h:34:12: error: candidate is: std::__cxx11::string Server::parse_message(int, std::__cxx11::string) string parse_message(int, string); ^ server.cc:190:63: error: ISO C++ forbids declaration of 'store_message' with no type [-fpermissive] Server::store_message(string name, string subject, string data) { ^ server.cc:190:1: error: prototype for 'int Server::store_message(std::__cxx11::string, std::__cxx11::string, std::__cxx11::string)' does not match any in class 'Server' Server::store_message(string name, string subject, string data) { ^ In file included from server.cc:1:0: server.h:36:10: error: candidate is: void Server::store_message(std::__cxx11::string, std::__cxx11::string, std::__cxx11::string) void store_message(string, string, string); ^ server.cc: In member function 'std::__cxx11::string Server::get_subjects(std::__cxx11::string)': server.cc:210:5: error: 'mappedMessages' was not declared in this scope if(mappedMessages.find(name) != mappedMessages.empty()) { ^ server.cc:214:15: error: missing template arguments before 'it' for(iterator it = mappedMessages.at(name).begin(); it != mappedMessages.at(name).end(); ++it) { ^ server.cc:214:53: error: 'it' was not declared in this scope for(iterator it = mappedMessages.at(name).begin(); it != mappedMessages.at(name).end(); ++it) { ^ server.cc:214:59: error: 'mappedMessages' was not declared in this scope for(iterator it = mappedMessages.at(name).begin(); it != mappedMessages.at(name).end(); ++it) { ^ server.cc:219:29: error: no match for 'operator+' (operand types are 'std::__cxx11::basic_string<char>' and 'int') response = response + " " + counter + " " + mappedMessages.at(name).it.front() + "\n";
这里的问题是使用
make client
代替
make msg-client
由于没有客户端的目标,所以默认使用cc。 这在原始问题的第一个黄色框中显示。
那些undefined reference to 'std::allocator<char>::allocator()'
错误的undefined reference to 'std::allocator<char>::allocator()'
通常意味着libstdc++
没有链接,通常是因为人们用gcc
而不是g++
链接。
依赖关系生成规则使用$(CC)
而不是$(CXX)
,这是导致这些编译器错误的原因。 事实上,这个规则现在已经十年没有必要了。 你应该做的是以下几点:
%.d
规则及其配方。 当编译.cc
到.o
(你的makefile使用内置的规则%.o
: %.cc
-MD -MP
)时, -MD -MP
编译器产生依赖关系( -MD -MP
):
%.o : %.cc $(CXX) -c -o $@ -MD -MP ${CPPFLAGS} ${CXXFLAGS} $<
include $(OBJS:.o=.d)
更改为-include $(OBJS:.o=.d)
。 没有什么是必需的生成和使用依赖关系。 有关更多详细信息,请参阅https://stackoverflow.com/a/32379965/412080 。
在你的命令行添加选项g ++ [-std = c ++ 11]