this post was submitted on 28 May 2025
706 points (96.2% liked)

Programmer Humor

23563 readers
1073 users here now

Welcome to Programmer Humor!

This is a place where you can post jokes, memes, humor, etc. related to programming!

For sharing awful code theres also Programming Horror.

Rules

founded 2 years ago
MODERATORS
 

Also, do y'all call main() in the if block or do you just put the code you want to run in the if block?

(page 3) 50 comments
sorted by: hot top controversial new old
[–] jjjalljs@ttrpg.network 8 points 3 days ago

Call the function from the if block.

Now your tests can more easily call it.

I think at my last job we did argument parsing in the if block, and passed stuff into the main function.

[–] lmmarsano@lemmynsfw.com 6 points 3 days ago* (last edited 3 days ago) (1 children)

Alternative: put entry point code in file __main__.py & run the containing package (eg, some_package) as a top-level expression (eg, python -m some_package).

[–] nickwitha_k@lemmy.sdf.org 1 points 3 days ago

TIL. Thanks for that!

[–] onlinepersona@programming.dev 3 points 3 days ago* (last edited 3 days ago) (8 children)

Can someone explain to me how to compile a C library with "main" and a program with main? How does executing a program actually work? It has an executable flag, but what actually happens in the OS when it encounters a file with an executable file? How does it know to execute "main"? Is it possible to have a library that can be called and also executed like a program?

Anti Commercial-AI license

[–] MajorasMaskForever@lemmy.world 5 points 3 days ago* (last edited 3 days ago)

You don't. In C everything gets referenced by a symbol during the link stage of compilation. Libraries ultimately get treated like your source code during compilation and all items land in a symbol table. Two items with the same name result in a link failure and compilation aborts. So a library and a program with main is no bueno.

When Linux loads an executable they basically look at the program's symbol table and search for "main" then start executing at that point

Windows behaves mostly the same way, as does MacOS. Most RTOS's have their own special way of doing things, bare metal you're at the mercy of your CPU vendor. The C standard specifies that "main" is the special symbol we all just happen to use

[–] anton@lemmy.blahaj.zone 3 points 3 days ago* (last edited 2 days ago)

If you want to have a library that can also be a standalone executable, just put the main function in an extra file and don't compile that file when using the library as a library.
You could also use the preprocessor to do it similar to python but please don't.

Just use any build tool, and have two targets, one library and one executable:

LIB_SOURCES = tools.c, stuff.c, more.c
EXE_SOURCES = main.c, $LIB_SOURCES

Edit: added example

[–] Cratermaker@discuss.tchncs.de 2 points 3 days ago

I haven't done much low level stuff, but I think the 'main' function is something the compiler uses to establish an entry point for the compiled binary. The name 'main' would not exist in the compiled binary at all, but the function itself would still exist. Executable formats aren't all the same, so they'll have different ways of determining where this entry point function is expected to be. You can 'run' a binary library file by invoking a function contained therein, which is how DLL files work.

There are a lot of other helpful replies in this thread, so I won't add much, but I did find this reference, which you could read if you have a lot of free time. But I particularly liked reading this summary:

  • _start calls the libc __libc_start_main;
  • __libc_start_main calls the executable __libc_csu_init (statically-linked part of the libc);
  • __libc_csu_init calls the executable constructors (and other initialisatios);
  • __libc_start_main calls the executable main();
  • __libc_start_main calls the executable exit().
load more comments (4 replies)
[–] vga@sopuli.xyz 2 points 3 days ago (1 children)

Shouldn't the third panel just be empty?

[–] BuboScandiacus@mander.xyz 3 points 3 days ago

It’s fine like this

load more comments
view more: ‹ prev next ›