Thread: Compiled without a main function

  1. #1
    Registered User
    Join Date
    Dec 2007
    Location
    Germany
    Posts
    30

    Compiled without a main function

    Hello,
    i compiled and linked an executable program using some ncurses++ libraries.
    After adding quellcode with a main function and compiling, it still worked.
    Though i dont have a problem with that, isnt it strange?
    Ben

  2. #2
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321



    edit:

    I once climed mount everest barefoot. But I won't show you the pics :P

  3. #3
    Banned
    Join Date
    Nov 2007
    Posts
    678
    abk, lol! haha! // very funny.

    and yeah, all suspects are guilty!

  4. #4
    Registered User
    Join Date
    Dec 2007
    Location
    Germany
    Posts
    30
    The ncurses libraries are very strange, i had quite often some problems with them. You cant trust them, i think the vast usage of makros is their problem, but i cant say what really happend there.

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    72
    benshi ; paste the code ;p let me see?

  6. #6
    Registered User
    Join Date
    Dec 2007
    Location
    Germany
    Posts
    30
    The code is very simple:
    withoutmain.cpp
    Code:
    
    
    To compile install the ncurses++ libraries and enter the following command:
    g++ withoutmain.cpp -lncurses++ -lpanel
    ->You'll get an executable a.out (which in fact does nothing besides being executable)
    Success, you wrote the shortest Program the world has ever seen!
    To proove, that i'm not kidding, add a main function now:
    Code:
    int main(){
    }
    and compile again.
    Ben

  7. #7
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    I'm not sure why you think that's weird. libncurses++ contains a main() function which is only linked if you don't provide your own. This is normal linker behavior. main() is not special.

    Read the ncurses++ documentation.

  8. #8
    Registered User
    Join Date
    Dec 2007
    Location
    Germany
    Posts
    30
    Ok,
    i think its strange, but maybe there might be a reason for this behaviour. It's ok, shall it compile, i dont mind.
    Ben

  9. #9
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by benshi View Post
    Ok,
    i think its strange, but maybe there might be a reason for this behaviour. It's ok, shall it compile, i dont mind.
    Ben
    The important thing is that if you provide your own main(), it will be used instead of the ncurses++ main(). Honestly, I don't know what the default main() does.

  10. #10
    Registered User
    Join Date
    Dec 2007
    Location
    Germany
    Posts
    30
    At least i can now understand why this worked. Its a kind of c++ easteregg

  11. #11
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    No, it's not a C easter egg. It's a ncurses++ easter egg.

    The following is technically dependent on operating system, but is often true practically.

    If you link a bunch of object files and libraries together to form an executable, any functions called that can't be found will be searched for in the libraries. If a function is found in the object files, it will not be searched for in the libraries. So, if you don't supply a main() function in your object files, the linker will find it in the ncurses++ library. If you do supply main() in your object files, the linker will find that, and not look for main() in the ncurses++ library.

    Provide two object files that both have a main() function, then your linker will complain bitterly regardless of whether you link in ncurses++ or not, because it can't pick between the two available functions.

  12. #12
    Registered User
    Join Date
    Dec 2007
    Location
    Germany
    Posts
    30
    Sorry... its ncurses related, not c++...
    So hopefully i will never link two object files containing a main function.
    After all, i stick to it, ncurses programs are difficult to handle, especially, for those who dont have enough knowlege of C (like me). Yesterday I compiled a ncurses Program and used some functions from <time.h> (bzw. <ctime>). It compiled, but then crashed with a segfault.
    I really dont know why, but after declaring a C++ string between the ctime functions and the use of ncurses(++) this problem was solved. I also once had another problem with ncurses - I used a STL list and misteriously the list didnt work (segfaults...).
    Last edited by benshi; 05-15-2008 at 04:46 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM