new to C(java programmer) Help with Main() and more please?

This is a discussion on new to C(java programmer) Help with Main() and more please? within the C Programming forums, part of the General Programming Boards category; Hi guys, I'm new to C, just came over from the darkside of Java. I'm writing a List.c file that ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    2

    Question new to C(java programmer) Help with Main() and more please?

    Hi guys,
    I'm new to C, just came over from the darkside of Java. I'm writing a List.c file that serves as a doubly linked list for a client module. Only problem is that List.c only compiles when it has a main function. I dont need this and when I remove the main function it gives me this error

    Code:
    > cc List.c
    Undefined                       first referenced
     symbol                             in file
    main                                /opt/SUNWspro/WS6U2/lib/crt1.o
    ld: fatal: Symbol referencing errors. No output written to a.out
    Otherwise it compiles just fine with an empty main function in place.

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    main is like start point of any C code.

    without the main, the compiler doesn't know where to start. with the main the compiler comes to know this is the starting point.

    you might call many other sub function with the main. it might be user defines or any library functions

    main always return a value

    Code:
    int main()
    {
       ...
       ...
       ...
    
       return 0;
    }
    ssharish2005

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    2
    Quote Originally Posted by ssharish2005
    main is like start point of any C code.

    without the main, the compiler doesn't know where to start. with the main the compiler comes to know this is the starting point.

    you might call many other sub function with the main. it might be user defines or any library functions

    main always return a value

    Code:
    int main()
    {
       ...
       ...
       ...
    
       return 0;
    }
    ssharish2005
    Ok, but why do I see C programs that dont have main?
    I can post up code without a main() if you want.... does it require special compiling?
    If you are saying all C programs need a main, I know that is incorrect.
    Remember, I am looking to use List.c for a client module(which has a main) which will utilize List.c via List.h...I dont know how else to say it.

  4. #4
    Mad OnionKnight's Avatar
    Join Date
    Jan 2005
    Location
    Umeň, Sweden
    Posts
    555
    You don't need a main() to compile your code. You will need an entry point if you're compiling it to an executable program. Compilers (I have yet to see one that doesn't) will create an executable if you don't pass any specific options to it. In your case you seem to want to compile it to object code, check the manual for your compiler or 'cc --help' to see which flag that represents, usually it's '-c'.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,590
    > Ok, but why do I see C programs that dont have main
    A C program needs a main
    A C file (like list.c) does not.

    My current work project has ~3000 files, but only one of them has main().

    To make a program, you would need
    cc prog.c list.c

    Your prog.c might look like this
    Code:
    #include "list.h"
    int main ( ) {
      List *mylist = ListNew();
      // more code
      ListDelete(mylist);
      return 0;
    }
    The 'main' is what brings everything together in a single highest level function which is your entire program.
    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.

  6. #6
    C 1337 Meshal's Avatar
    Join Date
    Nov 2006
    Posts
    70
    without main you cann`t test the execution of your program , you only can compile it to detect errors(compile time errors / suntax errors) !!

  7. #7
    Registered User
    Join Date
    Nov 2006
    Posts
    2

    Lightbulb Thank you guys

    Thanks a lot everyone. This makes sense now.
    I hope to be good enough with C that I will be helping others here aswell...I owe it to the board and this community

  8. #8
    Registered User
    Join Date
    Nov 2006
    Posts
    2

    Lightbulb Thank you guys

    Thanks guys! This is all starting to make a little more sense now.
    The other problem I'm getting is "parse error". I searched about it and it usually means I typed something wrong or missed a semicolon somewhere. But I'm getting parse errors at the fourth line! How does that happen? The fourth line is blank and is a space. Above it is include files...aswell as my own #include "List.h". Does this mean something is wrong with List.h? And whats a good way to check if everything is ok? I'm probably spoiled because Java really holds your hand through errors and debugging.

  9. #9
    Registered User
    Join Date
    Nov 2006
    Posts
    2

    Lightbulb Thank you guys

    Thanks guys! This is all starting to make a little more sense now.
    The other problem I'm getting is "parse error". I searched about it and it usually means I typed something wrong or missed a semicolon somewhere. But I'm getting parse errors at the fourth line! How does that happen? The fourth line is blank and is a space. Above it is include files...aswell as my own #include "List.h". Does this mean something is wrong with List.h? And whats a good way to check if everything is ok? I'm probably spoiled because Java really holds your hand through errors and debugging.

  10. #10
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    it would make more sense when we have look at your code.

    EDIT: you can actullay delete your double post. Click on edit button which is would displayed on to your right bottm of your post and click delete

    ssharish2005

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