这个makefile 有什么问题吗?

发布于 2024-09-24 02:11:38 字数 1193 浏览 5 评论 0原文

这是我第一次写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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

锦爱 2024-10-01 02:11:38

确保您使用制表符缩进命令行(无法判断您是否来自帖子,因为它们可能会被转换为空格)。

编辑

cc行和错误消息来看,make似乎正在使用它的隐式链接规则,完全忽略您提供的规则(第一个)。我在 makefile 中看不到任何会导致此问题的内容。还有一些需要检查的事情:

  1. 尝试通过键入例如 make Client.o 来运行其他规则,并查看它们是否有效。
  2. 检查所有文件是否都在当前目录中,并且 makefile 名为 Makefile
  3. 再次,我确信这只是帖子中的格式,但您的第一条规则是缩进一个空格其他的。你说你检查过了,我相信你,但我真的看不出这里还有什么问题。

我知道这些建议就像技术支持询问您的计算机是否已插入,但对我来说一切看起来都很好。我什至复制了它,修复了空白,然后在我的机器上运行它。一切似乎都很顺利。

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:

  1. Try running other rules by typing, e.g., make Client.o and seeing if they work.
  2. Check that all the files are in your current directory, and that the makefile is named Makefile
  3. Again, I'm sure that it's just the formatting in the post, but your first rule is indented one more space than the other ones. You said you checked it, and I trust you, but I really can't see anything else wrong here.

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.

看透却不说透 2024-10-01 02:11:38

您是否将文件“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.

莫言歌 2024-10-01 02:11:38

它看起来不像 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 in myprog.cc of the header that defines std::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 for make that dumps all of its macros (but I don't remember what it is off the top of my head), which might help.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文