Compiling cpp helper function with main.cu using NVCC compiler

63 Views Asked by At

I'm having an issue on creating a Makefile to compile my main file as a .cu file and my helper function as a cpp file. The reason I'm using cpp is that the helper function "sequence.cpp" is just a class that I use in my main.cu.

Update: I edited the makefile. Working on Windows 10 with VS code.

#compilers
CC = gcc
NVCC = nvcc 
CFLAGS = -std=c++17 -O3
NVCCFLAGS = -std=c++17 -O3
SOURCES = main.cu 

#directories
CUDA_INSTALL_PATH = /usr/local/cuda
CUDA_LIB = -L$(CUDA_INSTALL_PATH)/lib64 -lcudart

#source files
CPP_FILES = helper_functions.cpp sequence.cpp
CUDA_FILES = main.cu

#object files
OBJ_FILES = $(CPP_FILES:.cpp=.o)
CUDA_OBJ_FILES = $(CUDA_FILES:.cu=.o)

#Targets
all: executable

executable: $(OBJ_FILES) $(CUDA_OBJ_FILES)
$(NVCC) $(NVCCFLAGS) $(OBJ_FILES) $(CUDA_OBJ_FILES) -o executable $(CUDA_LIB)


%.o: %.cpp
$(CC) $(CFLAGS) -c $< -o $@

%.o: %.cu
$(NVCC) $(NVCCFLAGS) -c $< -o $@



clean:
rm -f $(OBJ_FILES) $(CUDA_OBJ_FILES) executable

This is the following output when I run make.

gcc -std=c++17 -O3 -c helper_functions.cpp -o helper_functions.o
gcc -std=c++17 -O3 -c sequence.cpp -o sequence.o
nvcc  -std=c++17 -O3 -c main.cu -o main.o
main.cu
tmpxft_000080e8_00000000-10_main.cudafe1.cpp
nvcc  -std=c++17 -O3 helper_functions.o sequence.o main.o -o executable -L/usr/local/cuda/lib64 -lcudart
helper_functions.o
sequence.o
main.o    
cl : Command line warning D9024 : unrecognized source file type 'helper_functions.o', object file assumed   
cl : Command line warning D9024 : unrecognized source file type 'sequence.o', object file assumed
cl : Command line warning D9024 : unrecognized source file type 'main.o', object file assumed
helper_functions.o : fatal error LNK1112: module machine type 'x86' conflicts with target machine type 'x64'
make: *** [Makefile:25: executable] Error 2

I initially thought to remove the "-o main.o" and "-o sequence.o". From what I found online, is that the c1 compiler is getting picked over NVCC and not sure how to say to use NVCC when I thought my "CXX = nvcc" does that.Another thing I tried was using another compiler command "CPP = gcc" for my sequence file. I thought this was the most straight forward way to do this. Should I just make my helper function a cu file? Thanks!

(update) I am now seeing the error message 'x86' conflicts with target machine type 'x64'. Is this error message also related to the unrecognized source files main.o and sequence.o? Thanks!

1

There are 1 best solutions below

4
Russell Taylor On

The error is reporting that one of the .o files is 32-bit (described as x86 for historical reasons) and the other is 64-bit (x64).

From the make output, it looks like sequence.cpp was not recompiled, so presumably it had earlier been compiled using a different architecture. Try deleting both .o files and running make again.