Linker Error Undefined Reference To Fork
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up I am getting error “undefined reference to 'fork' ” [duplicate] up vote 0 down vote favorite Possible Duplicate: Why does my compiler not accept fork(), despite my inclusion of ? Possible duplicate of: Why does my compiler not accept fork despite my inclusion of unistd.h This program is not working on gcc......and giving an linker error..i.e. undefined reference to fork... #include #include #include int main() { int a=10; if (a==10 && fork()) printf("hello"); else printf("world"); system("pause"); return 0; } c fork share|improve this question edited Oct 27 '12 at 10:14 Matthew Murdoch 16.7k2171111 asked Oct 27 '12 at 10:04 Tushar Gaurav 320112 marked as duplicate by alk, md5, Daniel Fischer, Kjuly, Andrew Marshall Oct 29 '12 at 4:01 This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question. operating system? –Aniket Oct 27 '12 at 10:04 add a comment| 2 Answers 2 active oldest votes up vote 5 down vote The fact that you're using system("pause") (a bad idea, by the way, when you can simply use getchar()) leads me to believe that you're running on Windows. Unless you use an emulation layer like CygWin, fork is not available on that platform. To be honest, I'm not sure how you even managed to compile since Windows doesn't generally have unistd.h either unless perhaps you've installed SFU or MinGW (which, despite having unistd.h to ease the task of compiling code (less conditional compilation), does not provide more functionality than what you get with Windows). share|improve this answer edited Oct 27 '12 at 10:38 answered Oct 27 '12 at 10:06 paxdiablo 491k1189731422 add a comment| up vote 2 down vote fork does not exist on Windows (except if you are using Cygwin). Use CreateProcess instead. share|improve this answer edited Oct 27 '12 at 10:10 answered Oct 27 '12 at 10:05 md5 17.5k22069 doesn't either. If he tried compiling it would have said can't compile anyway. –Anik
Programming Boards C Programming linker error Getting started with C or C++ | C Tutorial | C++ Tutorial | C and C++ FAQ | Get a compiler | Fixes for common problems Thread: linker error Thread Tools Show Printable Version Email this Page… Subscribe to this Thread… Display Linear Mode Switch to Hybrid Mode Switch to Threaded Mode 01-23-2005 #1 zell View Profile View Forum Posts Registered User Join Date Jan 2005 Posts 37 linker error Code: #include #include #include #include int main(void) { char *my_args[5]; pid_t http://stackoverflow.com/questions/13099137/i-am-getting-error-undefined-reference-to-fork pid; my_args[0] = "child.exe"; my_args[1] = "arg1"; my_args[2] = "arg2"; my_args[3] = NULL; puts ("fork()ing"); switch ((pid = fork())) { case -1: /* Fork() has failed */ perror ("fork"); break; case 0: /* This is processed by the child */ execv ("child.exe", my_args); puts("Uh oh! If this prints, execv() must have failed"); exit(EXIT_FAILURE); break; default: /* This is processed by the parent */ puts ("This is http://cboard.cprogramming.com/c-programming/60911-linker-error.html a message from the parent"); break; } puts ("End of parent program"); return 0; } /* * Program output: fork()ing This is a message from the parent End of parent program I am the child Arg 1 arg1 Arg 2 arg2 * */ hi, i am new here, nice to meet u guys btw i am learning operating systems now and i tried some coding using dev-c++ the above code is actually taken from the faqs section however i keep having this error, so i couldn't not compile [Linker error] undefined reference to `fork' anyone can help me? thanks in advance 01-23-2005 #2 Brain Cell View Profile View Forum Posts Visit Homepage former member Join Date Feb 2004 Posts 472 Whats your Operating system? My Tutorials : - Bad programming practices in : C - C\C++ Tips (constrcutive criticism is very welcome) - Brain Cell 01-23-2005 #3 zell View Profile View Forum Posts Registered User Join Date Jan 2005 Posts 37 i am using windows xp now 01-23-2005 #4 Brain Cell View Profile View Forum Posts Visit Homepage former member Join Date Feb 2004 Posts 472 as far as i know , those header files you're
compile the following program. It's simple but for some reason its not working. #include #include int main() { int n, fd[2], pid; const int MAXLINE = 4096; char https://www.daniweb.com/programming/software-development/threads/94600/linker-error-help line[MAXLINE]; if(pipe(fd) < 0) { printf("Pipe error"); } if((pid = port()) < 0) { printf("Fork error"); } else if(pid > 0){ close(fd[0]); write(fd[1], "Hello world\n", 12); } else { close(fd[1]); n = read(fd[0], line, MAXLINE); write(STDOUT_FILENO, line, n); } system("PAUSE"); return 0; } This is my compile log, Compiler: Default compiler Executing gcc.exe... gcc.exe "C:\Dev-Cpp\Projects\Test\main.c" -o "C:\Dev-Cpp\Projects\Test\main.exe" -I"C:\Dev-Cpp\include" -L"C:\Dev-Cpp\lib" {My directory}\Temp/ccEVcaaa.o(.text+0x65):main.c: undefined reference to linker error `pipe' {My directory}\Temp/ccEVcaaa.o(.text+0x7a):main.c: undefined reference to `port' collect2: ld returned 1 exit status Execution terminated Could someone help me out and let me know what I am doing wrong? Do I have to run it with some sort of cmd line param's? Thanks in advance for the help. -Barefoot c Barefootsanders 165 posts since Oct 2006 Community Member 4Contributors 4Replies 7Views 8 YearsDiscussion Span 8 linker error undefined Years Ago Last Post by Duoas 0 Salem 5,138 8 Years Ago You're compiling on a windows box, and windows doesn't do pipe's. I've no idea what port() is. 0 Discussion Starter Barefootsanders 8 Years Ago That would explain it... thanks a lot. So my only option is to compile things through linux then right? 0 Ancient Dragon 5,243 8 Years Ago Here is an example of using pipes in MS-Windows programs. Scroll down a bit and there is some example code. 0 Duoas 1,025 8 Years Ago You typed "port()" but I think you meant fork(). That's an OS-specific call. On Windows you have to use CreateProcess(). If you are just doing homework at home, you can ssh into your school and do it over the internet, or you can download Cygwin or MSys to compile it with the GCC (which is likely the same compiler you are using at school). I use MSys myself and if you want more info about installing it let me know. (It's relatively easy and doesn't do anything weird to your Windows box.) Hope this helps. c This article has been dead for over six month