Mac CUDA: Problem compiling NVCC output file with g++

eldudorinio

macrumors member
Original poster
Jan 16, 2010
54
1
Hello,

I have installed nVidia CUDA 4.2 on my Macbook 5,1 (Aluminum, late 2008). Installation went fine, following the instructions from this guide. Examples were compiled successfully and some of them were run as well.

I have compiled a .cu file using the below code:
Code:
$ nvcc -c -o cuda.o program.cu
Then compiled using g++ and got the following message:
Code:
$ g++ -o b cuda.o 
ld: warning: ignoring file cuda.o, file was built for i386 which is not the architecture being linked (x86_64): cuda.o
Undefined symbols for architecture x86_64:
  "_main", referenced from:
      start in crt1.10.6.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
I am suspecting that CUDA is not using the 64-bit libraries, since I had a similar experience with Ubuntu a few days ago.

Does anyone know how this can be fixed?
 

chown33

Moderator
Staff member
Aug 9, 2009
8,558
4,619
inter-prandial
Which OS version are you using?

Which g++ version? How/where did you get it (Xcode, fink, something else)?

Have you looked at the g++ man page, for the -arch option?

Do you have i386 runtime libraries installed?


Please post the output of each of these commands:
Code:
file cuda.o
g++ -arch i386 -o foo cuda.o
 

eldudorinio

macrumors member
Original poster
Jan 16, 2010
54
1
Hi,

I forgot to mention the OS. I am using Mac OS X Lion 10.7.4

I got g++ along with the Command Line Tools from Xcode 4.4.1.
Here's the version:
Code:
$ g++ -v
Using built-in specs.
Target: i686-apple-darwin11
Configured with: /private/var/tmp/llvmgcc42/llvmgcc42-2336.11~28/src/configure --disable-checking --enable-werror --prefix=/Applications/Xcode.app/Contents/Developer/usr/llvm-gcc-4.2 --mandir=/share/man --enable-languages=c,objc,c++,obj-c++ --program-prefix=llvm- --program-transform-name=/^[cg][^.-]*$/s/$/-4.2/ --with-slibdir=/usr/lib --build=i686-apple-darwin11 --enable-llvm=/private/var/tmp/llvmgcc42/llvmgcc42-2336.11~28/dst-llvmCore/Developer/usr/local --program-prefix=i686-apple-darwin11- --host=x86_64-apple-darwin11 --target=i686-apple-darwin11 --with-gxx-include-dir=/usr/include/c++/4.2.1
Thread model: posix
gcc version 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.11.00)
I just looked at the -arch option in g++ man page.

Here's the output for the commands you asked:
Code:
$ file cuda.o 
cuda.o: Mach-O object i386

$ g++ -arch i386 -o foo cuda.o 
Undefined symbols for architecture i386:
  "___cudaRegisterFatBinary", referenced from:
      __sti____cudaRegisterAll_48_tmpxft_000002d8_00000000_4_functionName_cpp1_ii_number()      in cuda.o
  "___cudaUnregisterFatBinary", referenced from:
      __cudaUnregisterBinaryUtil()      in cuda.o
ld: symbol(s) not found for architecture i386
collect2: ld returned 1 exit status
 

chown33

Moderator
Staff member
Aug 9, 2009
8,558
4,619
inter-prandial
The output from the 'file' command shows the cuda.o file is definitely 32-bit i386.

The linker fails because it can't find two CUDA support functions. This suggests that something else needs to be added in order to finish linking. Maybe a CUDA support library of some kind, but I'm just guessing.

You could search the CUDA SDK or libraries for those two functions, or google them to see if anyone else has ever encountered the same problem. Or if there's a CUDA forum, you could try asking there.
 

drsoong

macrumors member
Mar 24, 2008
56
1
Munich
chown33 might be right. You might have to link against libcudart, i.e.

g++ -arch i386 -o foo cuda.o -lcudart

I recently also tried my first steps with CUDA, but I also ran into i386/x86_64 clashes etc., and haven't done much with it since.
 

eldudorinio

macrumors member
Original poster
Jan 16, 2010
54
1
Hi guys,

Thanks for your suggestions.

I have actually managed to figure out my initial problem.

I have compiled a .cu file using the below code:
Code:
$ nvcc -c -o cuda.o program.cu
Then compiled using g++ and got the following message:
Code:
$ g++ -o b cuda.o
ld: warning: ignoring file cuda.o, file was built for i386 which is not the architecture being linked (x86_64): cuda.o
Undefined symbols for architecture x86_64:
"_main", referenced from:
start in crt1.10.6.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
CUDA was compiling in 32-bit architecture because .bash_profile was not loading correctly. This mean that instead of CUDA using lib64 that I listed in .bash_profile as follows:
Code:
export PATH=/usr/local/cuda/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH
it was using the default 32-bit lib.

This however only sorts out the -arch i386 part.

Unfortunately I do not have my Macbook for a couple of days, so I will have to get back to you regarding the additional CUDA libraries. This must be the issue since I did not include any CUDA libraries in my code.

Thank you again for the replies and I will get back to you when my Macbook is at hand :)

----------

You could search the CUDA SDK or libraries for those two functions, or google them to see if anyone else has ever encountered the same problem. Or if there's a CUDA forum, you could try asking there.
Unfortunately the nVidia forums were shut down due to some security issues as you can see here. I hope they open the forums again soon :)