HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Avoid --compiler-options in makefile using nvcc

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
makefilenvccoptionsavoidusingcompiler

Problem

I am working with CUDA and I am currently using this makefile to automate my build process. However, there are multiple --compiler-options, basically one for each flag I want to pass to gcc.

How can I avoid repeating it over and over?

(Any suggestion about improving my makefile is also really welcome)

CC=nvcc
LIBS=-lm
CFLAGS=-Wno-deprecated-gpu-targets --compiler-options -Wextra --compiler-options -Wall --compiler-options -O3 --compiler-options -Wno-unused-result --compiler-options -Wno-unused-parameter

all:    main

main:   main.o
    $(CC) $(CFLAGS) $(LIBS) main.o -o main

main.o: main.cu
    $(CC) $(CFLAGS) $(LIBS) -c main.cu -o main.o

clean:
    rm -f *.o main

Solution

-
As mentioned in the comments, define two macros:

COMPILER_OPTIONS := -Wextra -Wall -O3 -Wno-unused-result -Wno-unused-parameters
CUDA_COMPILER_OPTIONS := $(foreach option, $(COMPILER_OPTIONS), --compiler-option $(option))


-
C in CFLAGS stands for compiler, and should contain only the compile phase options. Don't send CFLAGS to the linker, it is surely not interested in, say, -Wall. Conventionally the link phase options are passed in LDFLAGS.

-
I am surprised that

$(CC) $(CFLAGS) $(LIBS) main.o -o main


did pick the math library. The linker processes the command line once and sequentially. As written, there is no undefined symbols lm could resolve by the time it is processed. The bulletproof way is to specify libraries after objects:

$(CC) $(CFLAGS) main.o -o main $(LIBS)


-
Nitpick. CC is conventionally assumed to produce .o from .c. Since your source is .cu I recommend to alias nvcc to something else. NVCC perhaps.

Code Snippets

COMPILER_OPTIONS := -Wextra -Wall -O3 -Wno-unused-result -Wno-unused-parameters
CUDA_COMPILER_OPTIONS := $(foreach option, $(COMPILER_OPTIONS), --compiler-option $(option))
$(CC) $(CFLAGS) $(LIBS) main.o -o main
$(CC) $(CFLAGS) main.o -o main $(LIBS)

Context

StackExchange Code Review Q#161157, answer score: 5

Revisions (0)

No revisions yet.