c++ - Makefile导致编译错误

标签 c++ makefile g++ openmp nvcc

我正在尝试编译一个程序(这不是我的):

make -f makefile

... 使用以下 makefile:

# Compiler for .cpp files
CPP  = g++

# Use nvcc to compile .cu files
NVCC = nvcc
NVCCFLAGS = -arch sm_20 # For fermi's in keeneland

# Add CUDA Paths
ICUDA = /usr/lib/nvidia-cuda-toolkit/include
LCUDA = /usr/lib/nvidia-cuda-toolkit/lib64

# Add CUDA libraries to the link line
LFLAGS += -lcuda -lcudart -L$(LCUDA) -lgomp

# Include standard optimization flags
CPPFLAGS = -O3 -c -I $(ICUDA) -Xcompiler -fopenmp -DTHRUST_DEVICE_BACKEND=THRUST_DEVICE_BACKEND_OMP

# List of all the objects you need
OBJECTS  = timer.o ar1.o kGrid.o vfInit.o parameters.o

# Rule that tells make how to make the program from the objects
main :  main.o $(OBJECTS)
        $(CPP) -o main main.o $(OBJECTS) $(LFLAGS)

# Rule that tells make how to turn a .cu file into a .o
%.o: %.cu
                $(NVCC) ${NVCCFLAGS} $(CPPFLAGS) -c $<

# How does make know how to turn a .cpp into a .o?  It's built-in!
# but if you wanted to type it out it would look like:
# %.o: %.cpp
#       $(CPP) $(CPPFLAGS) -c $<

clean :
        rm -f *.o
        rm -f core core.*

veryclean :
        rm -f *.o
        rm -f core core.*
        rm -f main

这会产生以下命令:

nvcc -arch sm_20  -O3 -c -I /usr/lib/nvidia-cuda-toolkit/include -Xcompiler -fopenmp -DTHRUST_DEVICE_BACKEND=THRUST_DEVICE_BACKEND_OMP -c main.cu
g++  -O3 -c -I /usr/lib/nvidia-cuda-toolkit/include -Xcompiler -fopenmp -DTHRUST_DEVICE_BACKEND=THRUST_DEVICE_BACKEND_OMP  -c -o timer.o timer.cpp
g++: error: unrecognized command line option â-Xcompilerâ
make: *** [timer.o] Error 1

我不明白 makefile:-xCompiler 标志(在变量 CPPFLAGS 中)只能由 使用>nvcc 编译器,而不是 g++。因此,我明白为什么我会收到错误。但是,我不明白,从我对上面的 makefile 的基本理解来看,为什么在某些时候变量 CPPFLAGS 遵循 g++ (变量 CPP)。我在 makefile 中没有看到任何这样的序列。

最佳答案

您的main 规则需要timer.otimer.o 没有明确的规则,因此 make 使用内置的隐式规则(如 makefile 末尾的注释中所述)。将 .cpp 文件转换为 .o 文件的隐式规则具有以下形式

$(CPP) $(CPPFLAGS) -c $<

因此它使用 CPPFLAGS 中的选项进行编译,其中包含 -Xcompiler。您可能希望 -Xcompiler 标志位于 NVCCFLAGS 而不是 CPPFLAGS

关于c++ - Makefile导致编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16472039/

相关文章:

c++ - OOP,赋值运算符不起作用

c++ - glClear() 不工作

c++ - 相同的外部结构只有一个功能不同

makefile - 匹配具有依赖性的任何模式规则

c++ - 引用命名空间内的结构类型

c++ - 我应该如何组织项目中的测试用例?

linux - 使用makefile执行程序时是否可以删除 "./"?

bash - Makefile中的Bash 'until'循环语法错误

c++ - 编译中的 Geany、g++ 和 SDL 错误

c++ - 最简单的传递引用需要什么?