Search code examples
ccudag++nvcc

g++ compiled binaries give “cannot execute binary file”


I am writing CUDA code and am using the following Makefile to compile and link it together.

DEBUG = TRUE
CUDA_PATH   = /usr/local/cuda
INC_DIR     = ../include
ICC = -I/usr/include -I$(INC_DIR) -I$(CUDA_PATH)/include
LIB_CUDA    = -L$(CUDA_PATH)/lib64
NVCC    = $(CUDA_PATH)/bin/nvcc
LINT    = cppcheck
LINK    = $(NVCC)
CXX = g++

C_SOURCES   = main.c
CUDA_SOURCES    = cuda_r_lib.cu
EXE     = r_lib
OBJS        = main.o
CUDA_OBJS   = cuda_r_lib.o
HFILES      = $(INC_DIR)/r_lib.h
MAKEFILE    = Makefile
REBUILDABLES    = $(CUDA_OBJS) $(OBJS) r_lib

LFLAGS      =  -lcuda $(LIB_CUDA)

ifdef DEBUG
CFLAGS = -Wall -ggdb -pthread -fPIC -O3
CDEFINES = $(ICC)
CUDA_FLAGS = -arch=sm_20 $(ICC)
else
CFLAGS = -Wall -pthread -fPIC -O3
CDEFINES = $(ICC) -DNDEBUG=1
CUDA_FLAGS = -arch=sm_20 $(ICC)
endif

$(EXE): $(OBJS) $(CUDA_OBJS)
    $(LINK) $(LFLAGS) $(OBJS) $(CUDA_OBJS) -o $(EXE)

$(OBJS): $(C_SOURCES) $(HFILES) $(MAKEFILE)
    $(CXX) $(CDEFINES) $(CFLAGS) -c $(C_SOURCES) -o $@
$(CUDA_OBJS): $(CUDA_SOURCES) $(HFILES) $(MAKEFILE)
    $(NVCC) $(CUDA_FLAGS) -c $(CUDA_SOURCES) -o $@

clean:
    rm -f *~ $(REBUILDABLES) *ii core
lint:
    $(LINT) --enable=all --inconclusive --std=posix *.c *.cu

I've got to the point where my code compiles and links cleanly. But the binary ./r_lib doesn't execute. I can't even change its permissions (tried chmod +x ...)

Here's what I get:

rinka@rinka-Desktop:/media/rinka/CUDA/dev/code$ make
g++ -I/usr/include -I../include -I/usr/local/cuda/include -Wall -ggdb -pthread -fPIC -O3 -c main.c -o main.o
/usr/local/cuda/bin/nvcc -arch=sm_20 -I/usr/include -I../include -I/usr/local/cuda/include -c cuda_r_lib.cu -o cuda_r_lib.o
/usr/local/cuda/bin/nvcc -lcuda -L/usr/local/cuda/lib64 main.o cuda_r_lib.o -o r_lib
rinka@rinka-Desktop:/media/rinka/CUDA/dev/code$ ll ./r_lib
-rw------- 1 rinka rinka 552223 Nov  6 19:08 ./r_lib
rinka@rinka-Desktop:/media/rinka/CUDA/dev/code$ file r_lib
r_lib: ELF 64-bit LSB  executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.24, BuildID[sha1]=8f2b88bf570a5d74c2c237969a93519f64636b86, not stripped
rinka@rinka-Desktop:/media/rinka/CUDA/dev/code$ ./r_lib
bash: ./r_lib: Permission denied
rinka@rinka-Desktop:/media/rinka/CUDA/dev/code$ chmod +x ./r_lib
rinka@rinka-Desktop:/media/rinka/CUDA/dev/code$ ./r_lib
bash: ./r_lib: Permission denied
rinka@rinka-Desktop:/media/rinka/CUDA/dev/code$ ll ./r_lib
-rw------- 1 rinka rinka 552223 Nov  6 19:08 ./r_lib

I can't for the life of me figure out what I'm doing wrong - maybe I'm just tired. I looked at: gcc compiled binaries give "cannot execute binary file" but I'm not using the -c option while linking...

Also - any feedback on the compiler & linker options for nvcc that will help throw up warnings more rigorously will be very welcome. I'm not really satisfied with the warnings I got so far.


Solution

  • This is not a message from your compiler or build-tool, but from your shell and originates from the OS.

    For the path /media/, it seems you have your files on external storage e.g. an USB-stick. That might be mounted with option noexec, so you cannot execute from there. Also, if that is VFAT, there are no permission-flags in the, so you cannot set them. Instead default flags are used by the OS and might also prevent execution a program from that partition. This is a security measure against malware.

    Try mount and check options.

    If I'm right, you should move the files to a normal filesystem, e.g. your home.