Created
June 9, 2020 19:49
-
-
Save sheredom/a6a513e7262a74324ae5506c4d59503a to your computer and use it in GitHub Desktop.
Weird macOS behaviour with linking
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// a.c | |
#include <stdio.h> | |
void is_not_called(void) __attribute__((constructor)) { | |
printf("This isn't called!\n"); | |
} | |
// b.c | |
#include <stdio.h> | |
void is_called(void) __attribute__((constructor)) { | |
printf("This is called!\n"); | |
} | |
void do_something_else(void) { | |
printf("Do something else!\n"); | |
} | |
// main.c | |
#include <stdio.h> | |
extern void do_something_else(void); | |
int main(int argc, char** argv) { | |
do_something_else(); | |
return 0; | |
} | |
// link command | |
clang -c a.c -o a.o && clang -c b.c -o b.o && ar qc static.a a.o b.o && clang main.c static.a -o main | |
// output | |
Neils-MacBook-Pro:weird_constructor neil$ ./main | |
This is called! | |
Do something else! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment