$ make g++ module.cpp -c -O2 ar cq libmodule.a module.o g++ test.cpp module.o -o from-object g++ test.cpp -Wl,--whole-archive libmodule.a -Wl,--no-whole-archive \ -o from-lib
$ ./from-object Yay life is good. $ ./from-lib Yay life is good.
#include "module.hpp" #include <iostream> using namespace std; void foo() { cout << "Yay life is good." << endl; }
#include "module.hpp" int main() { return 0; }
void foo() __attribute__((constructor));
default: g++ module.cpp -c -O2 ar cq libmodule.a module.o g++ test.cpp module.o -o from-object g++ test.cpp -Wl,--whole-archive libmodule.a -Wl,--no-whole-archive -o from-lib