gpt4 book ai didi

c++ - 使用 G++, undefined reference std::Makefile

转载 作者:太空狗 更新时间:2023-10-29 12:12:08 26 4
gpt4 key购买 nike

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

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

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

EDIT: I noticed something unusual here. When I call make client again without calling make clean I get the following:

peter@peter-VirtualBox:~/Desktop/CS360/Messaging_Service/messenger$ make client cc client.o -o client client.o: In function main':
/home/peter/Desktop/CS360/Messaging_Service/messenger/client.cc:10: undefined reference to
std::allocator::allocator()' Not sure if this is significant or not, but it doesn't mention g++ again.

所述错误的片段:

/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。这显示在原始问题的第一个黄色框中。

关于c++ - 使用 G++, undefined reference std::Makefile,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39539752/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com