Thread: execve() and the compiler

  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,057
    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,059
    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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    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.

Popular pages Recent additions subscribe to a feed