execve() and the compiler

This is a discussion on execve() and the compiler within the C Programming forums, part of the General Programming Boards category; I have a main and another function in two different .C files and when i try to compile the function.c ...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    5

    execve() and the compiler

    I have a main and another function in two different .C files and when i try to compile the function.c it gives me the error "undefined reference to main". How do i compile function() so that i get two separate executables, one for main and one for the function. I am trying to call execve in the main on this other function and i need an executable to call from correct?. Thanks.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    You can't create an executable out of just one function. Every executable needs a main() so that the operating system knows where to start executing the program.

    If you want two programs, you need two main() functions.

    But really, you shouldn't be using execve() just to call another part of your program! What's wrong with a straight function call?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    5
    That's how i would normally do it but it is a project for a class. Should my function have its own main then?

  4. #4
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    As noted by dwks if you want two executables, function.c needs its own main() otherwise when the program is loaded into memory the OS will have no clue where to even begin executing it.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    gcc main.c function.c

    If you have
    Code:
    // main.c
    extern void function();
    int main ( ) {
      function();
      return 0;
    }
    
    
    // function.c
    void function ( ) {
      // code goes here
    }
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21