twerdster
twerdster

Reputation: 5027

Problem compiling program using fork in cygwin

I am trying to run a simple program in cygwin that includes fork and wait. I thought it would be very easy to compile but I am having problems.

#include <stdio.h>  
#include <unistd.h>

void testFork(){}

int main(int argc,char* argv[]){

    if (fork()==0) {testFork();return 0;}
    while (wait() == -1);
    return 0;
}

Compiled using: gcc -Wall -Wextra -o test.o test

I get the following error:

C:\Users\Aaron\AppData\Local\Temp\ccgh3MfS.o:ostest.c:(.text+0x11): undefined reference to `fork'
C:\Users\Aaron\AppData\Local\Temp\ccgh3MfS.o:ostest.c:(.text+0x22): undefined reference to `wait'
collect2: ld returned 1 exit status

I'm sure I'm missing something trivial. Any ideas?

Upvotes: 1

Views: 4430

Answers (3)

Mohammed Kasfy
Mohammed Kasfy

Reputation: 53

It seems that MinGW gcc is being invoked because the cygwin gcc package is not installed.

You can verify that by calling the "cygcheck -c" in the cygwin commandline which will list all the installed packages, if you can't find the gcc in the list you need to install it

Upvotes: 0

Keith Thompson
Keith Thompson

Reputation: 263617

C:\Users\Aaron\AppData\Local\Temp\ccgh3MfS.o is a Windows-style path. If you're using Cygwin, you only be seeing Cygwin-style paths, perhaps something like /cygdrive/C/Users/Aaron/AppData/Local/Temp/ccgh3MfS.o.

You said your command line was

gcc -Wall -Wextra -o test.o test

but it was probably

gcc -Wall -Wextra -o test.o test.c

Are you invoking gcc from the Cygwin command line? What does type gcc say?

Upvotes: 0

dvorak
dvorak

Reputation: 126

The linker can't find the standard C libraries.

Did you install Cygwin in the normal way? (Here's a simple guide: http://www.eecg.utoronto.ca/~aamodt/ece242/cygwin.html).

Have you been able to compile even simpler programs:

#include <stdio.h>

int main(int argc, char **argv) {
    printf("Found C library.\n");
}

If that doesn't compile, you might just want to try removing and reinstalling Cygwin - something is broken.

Upvotes: 3

Related Questions