R.. GitHub STOP HELPING ICE
R.. GitHub STOP HELPING ICE

Reputation: 215287

Is there an easy way to make gcc omit crtbegin.o/crtend.o?

Aside from using -nostdlib and linking crt1.o -lc -lgcc yourself, is there any easy way to prevent gcc from linking crtbegin[S].o and crtend[S].o? These files are not that large, but I'm playing around with making small binaries, and would like to remove the useless C++ support code that's not needed for C programs. (Presumably, gcc links them even for C programs in case you're using a C++ library with global object variables. I'll spare everyone the rant about how it should be generating safe one-time initialization calls everywhere the global object is referenced in C++ modules rather than initializing global objects prior to main...)

I wouldn't be opposed to hacking the gcc specs file to make linking of the C++ support files conditional on such-and-such, but I'm not sure how I would do that. Perhaps there's already a nice way?

Upvotes: 13

Views: 5409

Answers (2)

R.. GitHub STOP HELPING ICE
R.. GitHub STOP HELPING ICE

Reputation: 215287

gcc -wrapper sh,-c,'z= ; for i ; do [ "$z" ] || set -- ; z=1 ;
    case "$i" in *crtbegin*.o|*crtend*.o) ;; *) set -- "$@" "$i" ;; esac ;
    done ; exec "$0" "$@"'

Upvotes: 8

leppie
leppie

Reputation: 117260

I think you need the -nostartfiles option. That's what I need for embedded stuff anyways.

Upvotes: 4

Related Questions