Thread: C program without main()

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    2

    Question C program without main()

    Hi All,

    I want to compile a C prog which doesn't have a main() function call. How should i go abt it.

    Regards
    Satish.

    Example prog:
    =============

    #include <stdio.h>

    void print()
    {
    printf ("\nHello World\n");
    }

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    72
    Hi
    So you like to write a library, not a executable program.
    to do that you should reffer to your OS documentation.

    there is a way to specify which will be the entry point in your executable file but it differs from OS to OS and it is not so easy.
    And also there will be a lot of additional efforts to get access to the resources that yout OS manage.
    damyan

  3. #3
    Registered User Mangesh's Avatar
    Join Date
    Sep 2001
    Posts
    18

    it's library

    Dear Satish_Ram,

    Please let us know which operating systema nd which complier you are using. If you using Unix and standard compiler, you can create library file with : cc -c <progname>.c

    This will create <progname>.o file, which will be used as library while linking.

    Regards,
    Mangesh.

  4. #4
    Unregistered
    Guest

    Lightbulb

    No Issues. Just go ahead and compile. It will compile all right. The compiler DOES NOT check the presence of the function 'main'.
    However, it will not link. The linker checks the existance of main.

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    2

    its not library , i want an executable ...

    Hello friends,

    I am using HP-UX . I wanted to know how to create an executable from a C prog which doen't have a main() .

    main.c
    ------
    #include <stdio.h>
    print()
    {
    printf ("\nHello World\n");
    }

    ---------------------------------
    cc -c main.c

    ld -e print /opt/langtools/lib/crt0.o main.o -lm -lc

    here -e specifies the entry point ... as from the man pages of ld ..
    i am getting a.out , but on running it i am getting a core dump at printf() function call ...

    My doubt is can we write a C prog without main() and create an executable from it . And also what exactly the option -e in ld does.

    Thanx in Advance,
    Satish.

  6. #6
    Registered User alex's Avatar
    Join Date
    Sep 2001
    Posts
    132
    Hi!

    The entry point of a C program may be main() from the viewpoint of the programmer, but from the viewpoint of the OS, the entrypoint is somewhere in the code of crt0.o. After some setup procedures (io initialization, memory allocation structures, signal handlers, whatever...), crt0 calls the main function in your program.

    So you are telling the linker that your program should skip any initialisation and just start your entry function... From there you call printf which tries to write into a buffer that has not been allocated. There you are: segfault.

    greetinx,
    alex

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Good reasons for placing main at the end of the program
    By droseman in forum C Programming
    Replies: 3
    Last Post: 01-29-2009, 09:49 AM
  2. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 03:30 PM
  3. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  5. Replies: 2
    Last Post: 05-10-2002, 04:16 PM