tags:

views:

291

answers:

1

Using GCC4 in MAC OSX, Linux and Windows.

Thanks.

+1  A: 

Just use the linker option -static as shown below.

edd@ron:/tmp$ cat helloworld.c
#include <stdio.h>

int main(void) {
    printf("Hello, world\n");
}
edd@ron:/tmp$ gcc -o helloworld.dyn helloworld.c
edd@ron:/tmp$ gcc -static -o helloworld.static helloworld.c
edd@ron:/tmp$ ls -l helloworld.*
-rw-r--r-- 1 edd edd     69 2009-08-18 07:09 helloworld.c
-rwxr-xr-x 1 edd edd   6667 2009-08-18 07:10 helloworld.dyn
-rwxr-xr-x 1 edd edd 576348 2009-08-18 07:10 helloworld.static
edd@ron:/tmp$ ./helloworld.dyn
Hello, world
edd@ron:/tmp$ ./helloworld.static
Hello, world
edd@ron:/tmp$ ldd helloworld.static
        not a dynamic executable
edd@ron:/tmp$ ldd helloworld.dyn
        linux-gate.so.1 =>  (0xb7efc000)
        libc.so.6 => /lib/i686/cmov/libc.so.6 (0xb7d83000)
        /lib/ld-linux.so.2 (0xb7efd000)
edd@ron:/tmp$
Dirk Eddelbuettel
Won't work for MinGW on Windows. AFAIK, there is no way to escape the dependancy on MSVCRT.DLL using MinGW, though I'd be happy to be proved wrong.
anon
Ah, yes, that other OS. Well at least I got two out three covered.
Dirk Eddelbuettel