CUDA: Problem compiling NVCC output file with g++

Discussion in 'Mac Programming' started by eldudorinio, Aug 26, 2012.

  1. eldudorinio macrumors member


    Jan 16, 2010

    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:
    $ nvcc -c -o cuda.o 
    Then compiled using g++ and got the following message:
    $ 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?
  2. chown33 macrumors 604

    Aug 9, 2009
    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:
    file cuda.o
    g++ -arch i386 -o foo cuda.o
  3. eldudorinio thread starter macrumors member


    Jan 16, 2010

    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:
    $ 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/ --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:
    $ 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
  4. chown33 macrumors 604

    Aug 9, 2009
    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.
  5. drsoong macrumors member

    Mar 24, 2008
    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.
  6. eldudorinio thread starter macrumors member


    Jan 16, 2010
    Hi guys,

    Thanks for your suggestions.

    I have actually managed to figure out my initial problem.

    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:
    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 :)


    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 :)

Share This Page