This is a VERY platform (OS + GCC version) sensitive way to solve the problem, as a target using GNU Make (I presume you know where the tabs go):
myfoo : foo.cpp $(CXX) -o $@ $^ \ -nodefaultlibs \ -Wl,-Bstatic \ $(shell $(CXX) -print-file-name=libstdc++.a) \ $(shell $(CXX) -print-file-name=libgcc.a) \ $(shell $(CXX) -print-file-name=libgcc_eh.a) \ -Wl,-Bdynamic \ -lpthread -lc -lm -ldl
CAVEAT: Use with caution! This may work on, say, Solaris, and be completely wrong for, say, AIX or Linux. And the libraries may be correct for GCC 3.2, and wrong for GCC 3.3. Your mileage may vary.
HTH, --Eljay