How to compile a dual-purpose ELF binary (Executable & Shared Library) that supports both dlopen and -l linking?
In Python, it's very easy to make a script act as both a library and an executable using if __name__ == "__main__":. I am trying to achieve the exact same behavior in C on Linux (similar to how glibc's libc.so.6 can be executed directly in the terminal to print its version, while still being a shared library). // hybrid.c #include <stdio.h> #include <stdlib.h> void print_string(const char *str) { printf("[LIB MODE] print_string executed. Received: %s\n", str); } int main(int argc, char **argv) { printf("[EXEC MODE] Program name: %s\n", argv[0]); if (argc > 1) { printf("[EXEC MODE] Argument: %s
0 upvotes
View original discussion ↗
Comments
Sign in or sign up to comment and upvote.
No comments yet.