【问题标题】:GCC can't compile a runnable Hello World in CygwinGCC 无法在 Cygwin 中编译可运行的 Hello World
【发布时间】:2014-11-23 13:45:27
【问题描述】:

我正在尝试编译一个简单的 hello world 程序:

#include <stdio.h>

int main() 
{
    printf("Hello World!");
    return 0;
}

有了这个makefile:

TARGET    = main.exe
OBJECT_01 = main.o
SOURCE_01 = main.c

$(TARGET): $(OBJECT_01)
    gcc -g -o $(TARGET) $(OBJECT_01) -lstdc++

$(OBJECT_01) : $(SOURCE_01)
    gcc -g -c $(SOURCE_01) -o $(OBJECT_01)

all : $(TARGET)

clean :
    -rm $(TARGET) $(OBJS)

它编译成 main.exe 就好了,但是当我尝试使用 ./main.exe 运行它时,它说该文件不存在并删除它。

我是否缺少一些 cygwin 包或其他东西?

更新:这是完整的终端命令序列:

andrey@andrey-PC ~/gcc-test
$ ls
main.c  main.exe  main.o  makefile

andrey@andrey-PC ~/gcc-test
$ make clean
rm main.exe

andrey@andrey-PC ~/gcc-test
$ make
gcc -g -o main.exe main.o

andrey@andrey-PC ~/gcc-test
$ ls
main.c  main.exe  main.o  makefile

andrey@andrey-PC ~/gcc-test
$ ./main.exe
-bash: ./main.exe: No such file or directory

andrey@andrey-PC ~/gcc-test
$ ls
main.c  main.o  makefile

【问题讨论】:

  • 如果是 C 程序,为什么要与 c++ 库链接?
  • 您的 makefile 看起来不错,不知道为什么要链接到 -lstdc++。 gcc 将为 C 程序自动链接 C 库。您不妨删除-lstdc++。编译后确实会产生main.exec 对吧?
  • @BlueMoon 删除 -lstdc++ 时会发生完全相同的事情。
  • @andreyg 我并不是建议删除 -libstd++ 来解决它。这是一个相关的注释,因为您有 C 代码。在执行main.exec 之前,你真的看到main.exe 生成了吗?
  • 会不会是某个病毒检查程序出了问题?如果在Makefile 中仅将main.exe 替换为main,结果如何? Cygwin 仍会使用名称main.exe 生成它,我不知道它为什么会有所作为,但也许它仍然值得一试。只是疯狂地猜测......

标签: c gcc cygwin


【解决方案1】:

不是一个直接的答案,但希望对你有趣和有帮助:你的Makefile 太复杂了。 以下Makefile 将完美地为您完成这项工作:

CFLAGS+=-g

.PHONY: all
all: main

.PHONY: clean
clean:
        $(RM) main.o main

(大多数Makefile 教程都很糟糕,我还没有看到好的教程 - 很抱歉我不得不说这样的咆哮,但我真的相信这是重要且真实的。)

【讨论】:

  • 不仅不是直接答案,而且根本不是问题的答案。
猜你喜欢
  • 2021-02-21
  • 1970-01-01
  • 2013-04-25
  • 1970-01-01
  • 2014-11-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多