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)

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

    rm -f *.o main
  • \$\begingroup\$ You may use a makefile template and a loop to inject the --compiler-options flag to a list like -Wextra -Wall -O3 ... \$\endgroup\$ – πάντα ῥεῖ Apr 19 '17 at 1:35
  • 1
    \$\begingroup\$ @πάνταῥεῖ Comments are for seeking clarification to the question. Please put all suggestions in answers. \$\endgroup\$ – 200_success Apr 19 '17 at 3:20
  • 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.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.