# How do I  link a 32 bit program with the C library on FreeBSD64



## adihodos (Oct 29, 2010)

On Fedora I did [cmd=]ld -dynamic-linker /lib/ldlinux.so.2 -lc proc.o -o proc[/cmd] 

How to do it in FreeBSD64 ?


----------



## expl (Oct 29, 2010)

`% ld -L/usr/lib32 -lc proc.o -o proc`

or

`% cc -L/usr/lib32 -B/usr/lib32 proc.o -o proc`

Should work, donÂ´t have a freebsd box around at this moment to test this however.


----------



## adihodos (Oct 29, 2010)

I ran [cmd=]ld -m elf_i386_fbsd -L/usr/lib32 -lc add_lists.o -o add_lists[/cmd] but it said that it cannot find lc.

When I ran [cmd=]ls -al /usr | grep -i lib[/cmd] it showed only these directories:


```
drwxr-xr-x  6 root wheel    11776 Jul  8 00:06 lib
drwxr-xr-x  5 root wheel      512 Nov 21  2009 libdata
drwxr-xr-x  5 root wheel     1536 Nov 21  2009 libexec
```
. 

So I must install the 32bit libraries, but I don't know the package name. Will I have to recompile the kernel in order to run 32 bit apps ?


----------



## expl (Oct 29, 2010)

Do

```
# cd /usr/src
# make build32 install32
# ldconfig -v -m -R /usr/lib32
```

You do not need to rebuild the kernel, just part of the base.


----------



## adihodos (Oct 29, 2010)

Thanks for the help. I also had to add to the command line the libraries crt1.0, crti.o, crtn.o, crtbegin.o and crtend.o, like this : 

[cmd=]ld -m elf_i386_fbsd -L/usr/lib32 /usr/lib32/crt1.o /usr/lib32/crtbegin.o /usr/lib32/crti.o /usr/lib32/crtn.o /usr/lib32/crtend.o -lc add_lists.o -o add_lists[/cmd]

Shouldn't the dependencies on the crt startup/end code be automatically pulled in by ld ?


----------



## expl (Oct 29, 2010)

ld needs to be told since it does not know what files have what. It would get pulled automatically (along with lgcc though) if you link via gcc since it is smarter and passes such stuff to ld.


----------

