Thread: linker error

  1. #1
    Registered User zell's Avatar
    Join Date
    Jan 2005
    Posts
    37

    Smile linker error

    Code:
    #include <stdio.h> 
    #include <stdlib.h> 
    #include <sys/types.h>
    #include <unistd.h> 
    
    
    int main(void)
    {
      char *my_args[5];
      pid_t 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 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

  2. #2
    former member Brain Cell's Avatar
    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

  3. #3
    Registered User zell's Avatar
    Join Date
    Jan 2005
    Posts
    37
    i am using windows xp now

  4. #4
    former member Brain Cell's Avatar
    Join Date
    Feb 2004
    Posts
    472
    as far as i know , those header files you're using (wich fork() is defined in) are for linux (not standard header files) , thats why you're getting a linker error.
    My Tutorials :
    - Bad programming practices in : C
    - C\C++ Tips
    (constrcutive criticism is very welcome)


    - Brain Cell

  5. #5
    Registered User zell's Avatar
    Join Date
    Jan 2005
    Posts
    37
    so i am not able to do those in windows i suppose? or is there anyway out?
    learning c programming for operating systems...
    learning openGL for graphics design...

  6. #6
    former member Brain Cell's Avatar
    Join Date
    Feb 2004
    Posts
    472
    I never had to use those headers\functions so i have no idea. I'll leave this question for someone else to answer
    My Tutorials :
    - Bad programming practices in : C
    - C\C++ Tips
    (constrcutive criticism is very welcome)


    - Brain Cell

  7. #7
    Registered User zell's Avatar
    Join Date
    Jan 2005
    Posts
    37
    Quote Originally Posted by Brain Cell
    I never had to use those headers\functions so i have no idea. I'll leave this question for someone else to answer
    alright
    thanks for the replies anyway
    learning c programming for operating systems...
    learning openGL for graphics design...

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > so i am not able to do those in windows i suppose?
    Yes, install cygwin from www.cygwin.com
    It gives you that lush unix feeling inside windows.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User zell's Avatar
    Join Date
    Jan 2005
    Posts
    37
    Quote Originally Posted by Salem
    > so i am not able to do those in windows i suppose?
    Yes, install cygwin from www.cygwin.com
    It gives you that lush unix feeling inside windows.
    i already got it installed...
    so i do i solve the linker error problems?
    learning c programming for operating systems...
    learning openGL for graphics design...

  10. #10
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    So you are compiling from dev-C++ or from the cygwin shell?

    ~/

  11. #11
    Registered User zell's Avatar
    Join Date
    Jan 2005
    Posts
    37
    from dev-c++

    sorry if this is trivial, but i dun know how to compile from cygwin shell
    learning c programming for operating systems...
    learning openGL for graphics design...

  12. #12
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    well, you would use this command in the shell

    Code:
    gcc -o myprog myprog.c
    This invokes the compiler (gcc) which produces an executable program called myprog. Of course you would replace 'myprog' and 'myprog.c' with the name of your program.

    Also, its been a while since I have used Cygwin, but I think you will have to copy the program into your home directory in the Cygwin folder (someone else can correct me if this is not necessary) in order to be able to find it within the shell.

    ~/
    Last edited by kermit; 01-23-2005 at 12:10 PM.

  13. #13
    Registered User zell's Avatar
    Join Date
    Jan 2005
    Posts
    37
    Quote Originally Posted by kermit
    well, you would use this command in the shell

    Code:
    gcc -o myprog myprog.c
    This invokes the compiler (gcc) which produces an executable program called myprog. Of course you would replace 'myprog' and 'myprog.c' with the name of your program.

    Also, its been a while since I have used Cygwin, but I think you will have to copy the program into your home directory in the Cygwin folder (someone else can correct me if this is not necessary) in order to be able to find it within the shell.

    ~/

    err...i got this error
    gcc: command not found
    so i still cannot compile

    and yeah, i can see the file in the shell already by copying it to the home folder
    Last edited by zell; 01-23-2005 at 12:20 PM.
    learning c programming for operating systems...
    learning openGL for graphics design...

  14. #14
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > gcc: command not found
    Rerun the setup tool, open up the development tools option and select gcc
    Then try again.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  15. #15
    Registered User zell's Avatar
    Join Date
    Jan 2005
    Posts
    37
    okie, finally i am able to compile

    now another noobie qns (sorry i am new to unix stuuf), how do you run the .exe file that is created?
    wat is the command?
    learning c programming for operating systems...
    learning openGL for graphics design...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  2. Crazy errors caused by class, never seen before..
    By Shamino in forum C++ Programming
    Replies: 2
    Last Post: 06-10-2007, 11:54 AM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. pointer to array of objects of struct
    By undisputed007 in forum C++ Programming
    Replies: 12
    Last Post: 03-02-2004, 04:49 AM
  5. Linking error
    By DockyD in forum C++ Programming
    Replies: 10
    Last Post: 01-20-2003, 05:27 AM