【问题标题】:I can't build HELLO, WORLD program?我无法构建 HELLO, WORLD 程序?
【发布时间】:2013-07-17 20:53:58
【问题描述】:

我是学习 C 的新手,我在 Windows 上(我想在 Windows 上学习)。

我已经在 cygwin 上安装了 GCC,并且我正在使用 NetBeans IDE

来源:

#include <stdio.h>

main()
{
    printf("Hello, world!\n");
    return 0;
}

我在构建上述代码时收到此错误

"/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
make[1]: Entering directory `/cygdrive/g/VS Projects/Hello World'
"/usr/bin/make"  -f nbproject/Makefile-Debug.mk dist/Debug/Cygwin_4.x-Windows/hello_world.exe
make[2]: Entering directory `/cygdrive/g/VS Projects/Hello World'
mkdir -p build/Debug/Cygwin_4.x-Windows
rm -f build/Debug/Cygwin_4.x-Windows/helloworld.o.d
gcc    -c -g -MMD -MP -MF build/Debug/Cygwin_4.x-Windows/helloworld.o.d -o build/Debug/Cygwin_4.x-Windows/helloworld.o helloworld.c
mkdir -p dist/Debug/Cygwin_4.x-Windows
gcc     -o dist/Debug/Cygwin_4.x-Windows/hello_world build/Debug/Cygwin_4.x-Windows/helloworld.o  build/Debug/Cygwin_4.x-Windows/main.o 
build/Debug/Cygwin_4.x-Windows/main.o: In function `main':
/cygdrive/g/VS Projects/Hello World/main.c:14: multiple definition of `main'
build/Debug/Cygwin_4.x-Windows/helloworld.o:/cygdrive/g/VS Projects/Hello World/helloworld.c:4: first defined here
collect2: error: ld returned 1 exit status
nbproject/Makefile-Debug.mk:63: recipe for target `dist/Debug/Cygwin_4.x-Windows/hello_world.exe' failed
make[2]: *** [dist/Debug/Cygwin_4.x-Windows/hello_world.exe] Error 1
make[2]: Leaving directory `/cygdrive/g/VS Projects/Hello World'
nbproject/Makefile-Debug.mk:60: recipe for target `.build-conf' failed
make[1]: *** [.build-conf] Error 2
make[1]: Leaving directory `/cygdrive/g/VS Projects/Hello World'
nbproject/Makefile-impl.mk:39: recipe for target `.build-impl' failed
make: *** [.build-impl] Error 2


BUILD FAILED (exit value 2, total time: 562ms)

有什么简单的方法可以在 Windows 上学习 C 吗?(visual studio 给了我错误,似乎主要针对 c++,所以这不是一个选项)

【问题讨论】:

  • 查看报错信息:main.c line 14 - multiple definition of main
  • 您正在使用 Netbeans 构建它。你试过用命令行编译吗?
  • 顺便提一下,main() 应该是 int main(void)int main(int argc, char **argv)

标签: c gcc netbeans compiler-errors


【解决方案1】:

问题可能出在您的 gcc 或您的 NetBeans 配置上

这里没问题

// helloworld.c
#include <stdio.h>

main()
{
    printf("Hello, world!\n");
    return 0;
}

编译成myprog

$ gcc helloworld.c -o myprog

运行它

$ ./myprog
# => Hello, world!

【讨论】:

  • 我总是使用make helloworld :-)
  • @meaning-matters 需要写入 Makefile。出于本示例的目的,我将尽可能保持直截了当。
  • 如果 OP 不知道如何编译和执行,请不要欺骗新手,&amp;&amp; ./hello。否则需要解释
  • @naomik 不,您不需要Makefile,因为有默认规则。你试过了吗?但为了清楚起见,最好按照您的方式进行。
【解决方案2】:

查看您的 NetBeans 是如何设置的。失败的编译是:

gcc -o dist/Debug/Cygwin_4.x-Windows/hello_world \
       build/Debug/Cygwin_4.x-Windows/helloworld.o \
       build/Debug/Cygwin_4.x-Windows/main.o 

那里有两个目标文件;一个看起来像 helloworld.c 的目标文件及其 main(),另一个是文件 main.c 的目标文件,它可能还包含一个 main() 程序。

重新定义您的构建规则,这样您就不会将main.o 链接到您的可执行文件中。

【讨论】:

    【解决方案3】:

    我知道这是一篇旧帖子,但您应该查看项目的源文件。在那里,NetBeans 默认生成 2 个 main.c 文件。擦除其中一个,只留下一个,然后再次尝试构建项目。

    【讨论】:

      猜你喜欢
      • 2016-04-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-18
      • 2022-01-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多