726

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
[-] match@pawb.social 12 points 1 week ago
[-] barsoap@lemm.ee 11 points 1 week ago
[-] qx128@lemmy.world 11 points 1 week ago

Nothing prevents you from putting a call to “main()” in the global scope

[-] Static_Rocket@lemmy.world 7 points 1 week ago

Just cross your fingers nobody attempts to import it...

load more comments (1 replies)
load more comments (4 replies)
[-] jjjalljs@ttrpg.network 8 points 1 week 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 1 week ago* (last edited 1 week ago)

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).

load more comments (1 replies)
[-] onlinepersona@programming.dev 3 points 1 week ago* (last edited 1 week ago)

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 1 week ago* (last edited 1 week 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 1 week ago* (last edited 6 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 1 week 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.

load more comments (5 replies)
[-] joyjoy@lemm.ee 2 points 1 week ago

Its called runpy.run_script

load more comments
view more: ‹ prev next ›
this post was submitted on 28 May 2025
726 points (96.3% liked)

Programmer Humor

23627 readers
2180 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