【问题标题】:Compiling and linking c++ program - make file编译和链接c++程序-make文件
【发布时间】:2013-06-27 07:27:33
【问题描述】:

我正在为我的程序构建一个 make 文件。 主要在:twitServer.cpp 及其使用:Command.h Client.h

我构建了一个 make 文件,但它似乎不起作用。 你能帮我弄清楚吗?

twitServer: twitServer.o Command.o Client.o
     g++ -Wall -o twitServer.o Command.o Client.o twitServer

twitServer.o: twitServer.cpp Command.h Client.h
     g++ -c -Wall twitServer.cpp

Command.o: Command.cpp Command.h
     g++ -c -Wall Command.cpp

Client.o: Client.cpp Client.h
     g++ -c -Wall Client.cpp

clean:
     rm twitServer Client.o Command.o Client.o

我的错误是: g++: twitServer: 没有这样的文件或目录

虽然我得到了所有这些文件: Client.cpp Client.o Command.h Makefile twitServer.cpp Client.h Command.cpp Command.o Makefile~ twitServer.o

【问题讨论】:

  • 请详细说明“似乎不起作用”。你有错误吗?
  • 您确定命令开头有 TAB 特殊字符吗? (在 g++ 之前)。当您尝试运行该 makefile 时收到的错误消息是什么?
  • 这是错误:g++: twitServer: No such file or directory
  • @ondav 是的,我知道了,谢谢询问

标签: c++ compilation makefile


【解决方案1】:

这个:

g++ -Wall -o twitServer.o Command.o Client.o twitServer

错了。您不希望编译器输出到twitServer.o 链接twitServer 文件,是吗?这两个应该翻转,像这样:

g++ -Wall -o twitServer Command.o Client.o twitServer.o

【讨论】:

  • 谢谢,但现在修复后,我尝试运行程序并得到:twitServer: command not found
  • @fgfjhgrjrerjhm 在其他答案上阅读我的 cmets 还是我需要在这里重复?
【解决方案2】:

改变-o标志的位置:

twitServer: twitServer.o Command.o Client.o
     g++ -Wall twitServer.o Command.o Client.o -o twitServer

【讨论】:

  • 谢谢,但现在我得到: twitServer: command not found 尝试运行程序时
  • @fgfjhgrjrerjhm ./twitServer
  • 为什么会这样?我怎样才能让它在没有 ./ 的情况下运行?
  • @fgfjhgrjrerjhm 放到$PATH的目录之一,例如/usr/bin
  • 等等,抱歉打扰了,但我过去制作了 makefile,我可以在我的目录中制作一个可运行的文件。不使用 ./
猜你喜欢
  • 2021-11-23
  • 1970-01-01
  • 1970-01-01
  • 2013-09-04
  • 1970-01-01
  • 1970-01-01
  • 2021-10-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多