简体   繁体   中英

I can't build HELLO, WORLD program?

I am new to learning C and I'm on Windows (I want to learn on Windows).

I have installed GCC on cygwin, and I'm using NetBeans IDE .

Source:

#include <stdio.h>

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

I get this error when building the above code:

"/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)

Is there any simple way to learn C on Windows? (visual studio gave me errors and seems to be mainly for c++, so that's not an option)

The problem is likely to be with your gcc or your NetBeans configuration

It's working fine here

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

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

Compile to myprog

$ gcc helloworld.c -o myprog

Run it

$ ./myprog
# => Hello, world!

Look at how your NetBeans is set up. The compilation that is failing is:

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 

There are two object files there; one looks like the object file for your helloworld.c with its main() , and another is the object file for a file main.c which presumably also contains a main() program.

Redefine your build rules so that you don't link main.o into your executable.

I know that this is an old post but you should look at the project's Source Files. There, NetBeans produces by default 2 main.c files. Erase one of them, leaving only one, and then try to build the project again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM