Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

static linking with gcc on OSX 10.6

I am having problems regarding static linking for OS X 10.6. I want to compile with -static, in which there is no 'crt0.o'. I understand normally one should not use -static, but it makes sense in my case. Here is the issue and Apple's
response.

http://developer.apple.com/mac/library/qa/qa2001/qa1118.html


Apple recommends:

"get the Csu module from Darwin and try building crt0.o statically"

I downloaded the Csu module from:

http://www.opensource.apple.com/source/Csu/Csu-79/

Did a 'make install' and then tried a test program. Does anybody know where
I can get a 'crt0.o' that will work or what I did wrong? I am using a MacBook
with a 10.6 upgrade. Thanks. Here is output of simple test programs.

//32-bit static - compiles fine
[167]gcc -static -m32 -g t.c
[168]a.out
Bus error
[169]


//gdb from above "Bus error"
(gdb) r
Starting program: /private/tmp/a.out

Program received signal EXC BADACCESS, Could not access memory.
Reason: KERN PROTECTIONFAILURE at address: 0x00000001
0x00000001 in ?? ()

//64-bit static
[172]gcc -static t.c
ld: warning: in /usr/lib/gcc/i686-apple-darwin10/4.2.1/libgcc_static.a,
file is not of required architecture
Undefined symbols:
"_exit", referenced from:
start in crt0.o
ld: symbol(s) not found
collect2: ld returned 1 exit status

//64-bit static
[172]gcc -static t.c
ld: warning: in /usr/lib/gcc/i686-apple-darwin10/4.2.1/libgcc_static.a,
file is not of required architecture
Undefined symbols:
"_exit", referenced from:
start in crt0.o
ld: symbol(s) not found
collect2: ld returned 1 exit status


//64-bit static
[172]gcc -static t.c
ld: warning: in /usr/lib/gcc/i686-apple-darwin10/4.2.1/libgcc_static.a,
file is not of required architecture
Undefined symbols:
"_exit", referenced from:
start in crt0.o
ld: symbol(s) not found
collect2: ld returned 1 exit status


//now 32-bit no longer works due to the Csu 'make install' overridding
// /usr/lib/crt1.10.6.o
[175]gcc -m32 t.c
ld: warning: in /usr/lib/crt1.10.6.o, file is not of required
architecture ld: could not find entry point "start" (perhaps missing
crt1.o)
collect2: ld returned 1 exit status

Mac OS X (10.6)

Posted on Dec 2, 2009 12:25 PM

Reply
20 replies

Dec 2, 2009 4:16 PM in response to Andrewvv

Andrewvv wrote:
I am having problems regarding static linking for OS X 10.6.


What sort of problems?

I understand normally one should not use -static, but it makes sense in my case.


I'm not convinced of that.

Here is the issue and Apple's response.

http://developer.apple.com/mac/library/qa/qa2001/qa1118.html


Apple recommends:

"get the Csu module from Darwin and try building crt0.o statically"


No, not really. Apple said:

Static linking of user binaries is not supported on Mac OS X. ... Any change to Mac OS X, in a system update, security update, or major revision, may break statically linked code.


As others have pointed out, that note was from a long time ago. It would be helpful if you explained your actual problem rather than focusing on implementing a 7 year old hack that Apple strongly discouraged, even then.

Dec 3, 2009 7:34 AM in response to etresoft

All I want to do is compile gcc with the -static flag. You can try a simple
example on your system

file t.c:

int main(void) { return(0); }

%gcc -static t.c

will complain about library not found -lcrt0.o. I don't really care to
get into the static vs dynamic issues. Long story short, I am trying to compile
a compiler which produces assembly. I need the assembly to be able to produce
static style code. This make life easier than producing different code for
each system ABI.

All I need is a working crt0.o for 32/64, I understand the issues that this
may cause across systems, but would really like to compile with -static. This
is why I attempted to get the Csu module installed. I did and it failed to
work, which was my original post.

Dec 3, 2009 12:27 PM in response to Andrewvv

Andrewvv wrote:
Long story short, I am trying to compile
a compiler which produces assembly. I need the assembly to be able to produce
static style code. This make life easier than producing different code for
each system ABI.


I still don't get it. Is there a longer story? If I knew what your goal was, I might be able to provide a better way to approach it.

All I need is a working crt0.o for 32/64, I understand the issues that this
may cause across systems, but would really like to compile with -static. This
is why I attempted to get the Csu module installed. I did and it failed to
work, which was my original post.


I think you are taking a Linux-style approach and trying to apply it to MacOSX. That works 99% of the time. This is part of that 1%. Perhaps that "-static" flag flat out won't work on MacOS X. Not all features of gcc are implemented on MacOS X. Apple won't even be using gcc in future versions of the OS.

I don't know how to link using "-static". I can't think of any reason to do so on MacOSX. If I knew why you wanted to use "-static" I might be more interested in the problem. Right now, I just don't get it. By asking for help, you are essentially asking for collaborators on the project - even if it is only for 10 minutes. You need to get me interested.

Dec 3, 2009 4:18 PM in response to etresoft

I have a compiler which produces assembly code. Life is much easier to just
produce one -static version for memory access than support one for each ABI,
since the static is the same. I am attempting to port from Linux, and I am
< 1% of normal usage. Here is some assembly produced from gcc on the Mac
below. I have no desire to produce the first version with offsets of %ecx.



long l;
void foo(void)
{
l = 1;
}

/////////////////NO -static///////////////////////////
gcc -S -m32 t.c
//////////////////////////////////////////////////////


A
_foo:
pushl %ebp
movl %esp, %ebp
subl $8, %esp
call L3
"L00000000001$pb":
L3:
popl %ecx
leal L_l$non_lazy_ptr-"L00000000001$pb"(%ecx), %eax
movl (%eax), %eax
movl $1, (%eax)
leave
ret
.comm _l,4,2
.section __IMPORT,__pointers,non_lazy_symbol_pointers
L_l$non_lazy_ptr:
.indirect_symbol _l
.long 0
.subsections_via_symbols



/////////////////WITH -static///////////////////////////
gcc -S -m32 -static t.c
//////////////////////////////////////////////////////

.text
.globl _foo
_foo:
pushl %ebp
movl %esp, %ebp
subl $8, %esp
movl $1, _l
leave
ret
.comm _l,4,2
.subsections_via_symbols

Dec 3, 2009 7:00 PM in response to Andrewvv

Andrewvv wrote:
I am attempting to port from Linux, and I am < 1% of normal usage. </div>

Then your most efficient course of action is to give $195 to Apple Developer Tech Support and ask them. I even checked the Apple Mailing Lists and it has been five years since anyone has even asked this question. Then, the answer was that it was virtually impossible and strongly discouraged. I can't see how that has gotten any better now.

Perhaps someone else has a better answer for you. I admit that I know very little about this, but then I usually prefer to take the path of least resistance. Your destiny lies upon a different path from mine.

Nov 3, 2015 4:06 PM in response to ericmeyers

I'm having the same issue. My interest in using '-static' is probably the same as those who originally created the gcc option. In my model of coding, I write codes that extract object code from other libs on my system. I have typically in the past not cared about "-static". So my codes have been linking dynamically. When I move these binaries to other Macs, they fail on dynamic lib dependencies. Recently, I moved a code over that was compiled with gfortran. But the execution of that code on another Mac that did not have gfortran reported a dynamic linking error associated with some gfortran lib. I read that the solution to this issue (and the general issue of making codes portable), is to include all the dependencies inside the executable file. I can't see why this is ever a bad thing, except that it can make the executable file larger in size. Anyway, I'll probably find a workaround in an hour, but felt I should respond (and I might get educated further). BTW, if gcc isn't supported in future Macs, what will be?

Sep 9, 2011 2:01 AM in response to Andrewvv

I am also having the same issue and from what I can see above it seems that MAC does not support -static linking.


Why can't we build statically on MAC OSx?

I have a code that requires several additional libraries to be built and I want my student to use the program without using much time on compilation and building phases.

But apparently I do not have crt0.o in my /usr/lib and this results in :


ld: library not found for -lcrt0.o


Does anyone know how to "make" crt0.o?

static linking with gcc on OSX 10.6

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.