这个makefile 有什么问题吗?
这是我第一次写makefile。这不起作用(例如,如果我修改 ClientSocket.cc,它只会显示“uptodate”。如果我运行 make myprog,还会引发大量依赖项错误)。你能告诉我这有什么问题吗? 谢谢
myprog: myprog.o Client.o ClientSocket.o Socket.o
g++ -Wall -g Socket.o ClientSocket.o Client.o myprog.o -o myprog
myprog.o: myprog.cc
g++ -Wall -g -c myprog.cc
Client.o: Client.cc Client.h
g++ -Wall -g -c Client.cc
ClientSocket.o: ClientSocket.cc ClientSocket.h
g++ -Wall -g -c ClientSocket.cc
Socket.o: Socket.cc Socket.h
g++ -Wall -g -c Socket.cc
运行 make myprog 时出现错误:
cc mprog.o -o myprog
myprog.o: In function `std::__verify_grouping(char const*, unsigned int, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
myprog.cc:(.text+0xe): undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::size() const'
还有很多其他错误。喜欢:
myprog.cc:(.text+0x1a4): undefined reference to `std::cout'
myprog.o:(.eh_frame+0x12): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status
This is the first time I am writing a makefile. This doesn't work (for example, if I modify ClientSocket.cc it just says "uptodate". Also throws up lots of dependency errors if I run make myprog). Can you please tell what is wrong in this?
Thank you
myprog: myprog.o Client.o ClientSocket.o Socket.o
g++ -Wall -g Socket.o ClientSocket.o Client.o myprog.o -o myprog
myprog.o: myprog.cc
g++ -Wall -g -c myprog.cc
Client.o: Client.cc Client.h
g++ -Wall -g -c Client.cc
ClientSocket.o: ClientSocket.cc ClientSocket.h
g++ -Wall -g -c ClientSocket.cc
Socket.o: Socket.cc Socket.h
g++ -Wall -g -c Socket.cc
Errors when running make myprog:
cc mprog.o -o myprog
myprog.o: In function `std::__verify_grouping(char const*, unsigned int, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
myprog.cc:(.text+0xe): undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::size() const'
There are many others. Like:
myprog.cc:(.text+0x1a4): undefined reference to `std::cout'
myprog.o:(.eh_frame+0x12): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
确保您使用制表符缩进命令行(无法判断您是否来自帖子,因为它们可能会被转换为空格)。
编辑:
从
cc
行和错误消息来看,make似乎正在使用它的隐式链接规则,完全忽略您提供的规则(第一个)。我在 makefile 中看不到任何会导致此问题的内容。还有一些需要检查的事情:make Client.o
来运行其他规则,并查看它们是否有效。Makefile
我知道这些建议就像技术支持询问您的计算机是否已插入,但对我来说一切看起来都很好。我什至复制了它,修复了空白,然后在我的机器上运行它。一切似乎都很顺利。
Make sure you are using tabs to indent the command lines (it's impossible to tell whether you are or not from the post, since they would probably be converted to spaces).
EDIT:
From the
cc
line and the error messages, it looks like make is using its implicit rule for linking, completely ignoring the one you provided (the first one). I can't see anything in the makefile that would cause this. A few more things to check:make Client.o
and seeing if they work.Makefile
I know these suggestions are like tech support asking you whether your computer is plugged in, but everything really looks fine to me. I even copied it, fixed the whitespace, and ran it on my machine. Everything seemed to work.
您是否将文件“Makefile”命名为大写M?
您还可以尝试运行 make -d 来获取一些调试信息。
Did you name your file "Makefile" with a capital M?
You could also try running
make -d
for some debug information.它看起来不像 makefile 问题(因为正在调用编译器)。看起来像是 C++ 源代码问题。
具体来说,您似乎在定义
std::basic_string
的标头的myprog.cc
中缺少#include
。您是否尝试过不使用 make 直接执行编译器命令来查看源代码是否正确编译?
附录
当您运行
make
时,您的包含路径可能会有所不同。 make 有一个调试/跟踪选项,可以转储所有宏(但我不记得它是什么),这可能会有所帮助。It doesn't look like a makefile problem (because the compiler is being invoked). It looks like a C++ source code problem.
Specifically, it looks like you are missing a
#include
inmyprog.cc
of the header that definesstd::basic_string
.Have you tried executing the compiler commands directly without using make, to see if your source code compiles correctly?
Addendum
Perhaps your include path is different when you run
make
. There is a debug/trace option formake
that dumps all of its macros (but I don't remember what it is off the top of my head), which might help.