使用G ++,Undefined Reference std :: Makefile

时间:2016-09-16 20:38:22

标签: c++ linux makefile g++

我一直致力于让服务器/客户端运行简单的单线程信使服务。它能够在Visual Studio上编译,但是当我尝试使用命令:make client通过makefile在Linux上编译它时,我得到了一个非常大的错误列表。我将提供开头部分,然后是错误列表:

这里说它至少试图使用g ++:

  

g ++ -g -c -o client.o client.cc cc client.o -o client client.o:   在函数main': /home/peter/Desktop/CS360/Messaging_Service/messenger/client.cc:10: undefined reference to std :: allocator :: allocator()'

中      编辑:我发现这里有些异常。当我再次致电make client而不致电make clean时,我会收到以下信息:

     

peter @ peter-VirtualBox:〜/ Desktop / CS360 / Messaging_Service / messenger $ make client       cc client.o -o client       client.o:在函数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跟踪制作时,它会为我提供大量数据。我会发布一些所提供内容的片段,因为我真的不知道我在看什么:

 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";

3 个答案:

答案 0 :(得分:1)

这些undefined reference to 'std::allocator<char>::allocator()'错误通常意味着libstdc++未被关​​联,最常见的原因是人们与gcc而不是g++相关联。

依赖关系生成规则使用$(CC)而不是$(CXX),这是导致这些编译器错误的原因。事实上,这条规则已经十年不用了。你应该做的是以下几点:

  1. 完全删除%.d规则及其配方。
  2. 在编译-MD -MP.cc(您的makefile使用the built-in rule %.o : %.cc)时,指示编译器生成依赖项(.o):

    %.o : %.cc
        $(CXX) -c -o $@ -MD -MP ${CPPFLAGS} ${CXXFLAGS} $<
    
  3. include $(OBJS:.o=.d)更改为-include $(OBJS:.o=.d)
  4. 生成和使用依赖项不需要任何其他内容。有关详细信息,请参阅https://stackoverflow.com/a/32379965/412080

答案 1 :(得分:1)

这里的问题是使用

make client

而不是

make msg-client

由于客户端没有目标,因此默认使用cc。这显示在原始问题的第一个黄色框中。

答案 2 :(得分:0)

在你的命令行中为g ++添加选项[-std = c ++ 11]