# How to compile CUDA applications?



## sav (Jul 6, 2013)

I'm trying to get CUDA to work on:

```
FreeBSD silvers 9.1-RELEASE-p4 FreeBSD 9.1-RELEASE-p4 #0: Sun Jun 30 01:07:52 MSK 2013     root@silvers:/usr/obj/usr/src/sys/GENERIC  amd64
```
I installed toolkit 2.3 (32 bit) for _F_edora 10 but when *I* run `nvcc cuda.cpp` *I* get:

```
[CMD=sh-3.2$]/usr/local/cuda/bin/nvcc cuda.cpp[/CMD]
/usr/local/cuda/bin/../lib/libcudart.so: could not read symbols: File in wrong format
```
*I* suppose this is because 32-bit _NVCC_ runs _a_ 64-bit compiler. how do *I* use _a_ 32-bit native _L_inux enviro_n_ment for building _a_ _CUDA_ app_lication_? 

_T_hank you.


----------



## nslay (Jul 6, 2013)

Unfortunately, there is no native CUDA toolchain. Although, I did see some Japanese blog (I can't read Japanese) a while ago that seemed to suggest that the author was able to compile and run CUDA applications on FreeBSD through the Linux CUDA toolchain (through Linux emulation). 

That aside, does the FreeBSD nVidia driver blob even officially support CUDA?


----------



## sav (Jul 7, 2013)

From what I've seen while googling about FreeBSD and CUDA, it seems that FreeBSD Nvidia drivers support CUDA, so basically, a compiled application should run fine. The problem is to compile it using 32-bit _L_inux capability. I guess *I* need to chroot to /compat/linux and compile CUDA applications from there, but there is no Linux GCC. I saw that there was _a_ package linux_devel, but there is no one now.


----------



## sav (Jul 7, 2013)

I've managed to compile _a_ simple CUDA application under _L_inux emulation, but can't execu_t_e the compiled application:

```
./a.out: error while loading shared libraries: libcudart.so.2: cannot open shared object file: No such file or directory
```
I have libcudart.so.2 in /usr/local/cuda/lib and symlinked it into /usr/local/lib.

Any ideas?


----------



## nslay (Jul 7, 2013)

The libcudart.so should be in your /compat/linux/usr/lib and not /usr/local/lib. For Linux applications, the runtime linker will implicitly look in /compat/linux (if this is where your Linux environment lives).

You could also add libcudart.so to /etc/libmap.conf to help the runtime linker locate the dependency (See libmap.conf(5)). That's a little more direct than a symlink.


----------



## sav (Jul 7, 2013)

@nslay thank you very much. The problem is solved. nvcc compiled _an_ application with some warnings. _The a_pp_lication_ starts ok, but I just found that my card doesn't support CUDA (I thought it does), so it crashes with _a_ message that the card is not supported. 

In case somebody needs it, here is a summary:

Set up Linux emulation (I used linux_base-f10-10_5) and download and install _the_ CUDA toolkit 2.3 (32 bit) for Fedora 10 in /compat/linux. Actually you don't need to "install" it, just unpack say to /compat/linux/bin/local/cuda. 
[*]Using rpm2cpio.pl (`pkg_add -r rpm2cpio`) add some Fedora 10 RPMs from here: http://archives.fedoraproject.org/pub/archive/fedora/linux/releases/10/Fedora/i386/os/Packages/. You'll need the following packages (sorry if I forget something):
cpp, binutils-devel, gcc, gcc-c++, glibc-devel, glibc-common, glibc-headers, kernel-devel, kernel-headers, libgcc, mpfr, gmp. To install them `cd` to /compat/linux and run
`rpm2cpio.pl < package_name.rpm | cpio -id`.

Chroot to /compat/linux and create in /usr/lib the symbolic links to the libraries located in cuda/lib. 

To compile _an_ application run `/usr/local/cuda/bin/nvcc app.cu`


----------

