I often work on my netbook, so I prefer to use Sublime Text with Makefiles instead of full-featured IDEs. To automate the build process I’ve constructed (with help of Vasily Picard and examples from the Internet) a universal Makefile. It assumes the following structure of files.
All sources are placed in the src subdirectory. Intermediate object files are placed in the obj subdirectory, which must be created before compilation. The resulting binary file is placed in the bin subdirectory. If you use gengetopt as the command-line argument parser, name the ggo file cmdline.ggo, and the *.c and *.h files will be updated automatically if the ggo file is changed.
The following makefile is set to use MPI compilers, include the OpenGL, CUDA, and GLUT libraries, and compile CUDA source code as well. Feel free to remove unnecessary fragments and to change the application binary name (in the first line).
1APPNAME := bin/app
2SOURCES := $(wildcard src/*.cpp src/*.cu src/*.c)
3OBJECTS := $(patsubst src%,obj%, $(patsubst %.cu,%.device.o, $(patsubst %.cpp,%.o, $(patsubst %.c,%.o, $(SOURCES)))))
4
5INCLUDE := -I/usr/local/cuda/include
6LIBPATH := -L/usr/local/cuda/lib64
7LIBS := -lcudart -lGL -lglut
8
9FLAGS := -O3 -ffast-math -Wall -Werror -fopenmp
10CCFLAGS := $(FLAGS)
11CXXFLAGS := $(FLAGS) -std=c++0x
12
13GENCODE_FLAGS := -gencode arch=compute_20,code=sm_20 -gencode arch=compute_30,code=sm_30 -gencode arch=compute_35,code=sm_35
14NVCCFLAGS := $(GENCODE_FLAGS) --compiler-options -fno-strict-aliasing -lineinfo -use_fast_math -Xptxas -dlcm=cg
15
16CC := mpicc
17CXX := mpicxx
18NVCC := /usr/local/cuda/bin/nvcc
19
20all: $(OBJECTS)
21 $(CXX) $(CXXFLAGS) $(INCLUDE) $(OBJECTS) -o $(APPNAME) $(LIBPATH) $(LIBS)
22
23obj/cmdline.o: src/cmdline.c
24 $(CC) -Wno-unused-but-set-variable -c $< -o $@
25
26src/cmdline.c: src/cmdline.ggo
27 gengetopt --input=src/cmdline.ggo --output-dir=src --include-getopt
28
29%.o: ../src/%.c
30 $(CC) $(CCFLAGS) $(INCLUDE) -c $< -o $@
31
32%.o: ../src/%.cpp
33 $(CXX) $(CXXFLAGS) $(INCLUDE) -c $< -o $@
34
35%.device.o: ../src/%.cu
36 $(NVCC) $(NVCCFLAGS) -c $< -o $@
37
38clean:
39 rm -rf obj/*
40 rm -f $(APPNAME)