c++ - 修改 CUDA 和 cpp Makefile

标签 c++ cuda makefile

我正在尝试修改我的 makefile 以支持 .cpp 和 .cu,但是,我不断收到如下错误:

/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crt1.o(.text+0x20): error: undefined reference to 'main'
hostCode.o:displayfunc.cpp:function readScene(char*): error: undefined reference to 'camera'
hostCode.o:displayfunc.cpp:function readScene(char*): error: undefined reference to 'camera'
hostCode.o:displayfunc.cpp:function readScene(char*): error: undefined reference to 'camera'
hostCode.o:displayfunc.cpp:function readScene(char*): error: undefined reference to 'camera'
hostCode.o:displayfunc.cpp:function readScene(char*): error: undefined reference to 'sphereCount'
hostCode.o:displayfunc.cpp:function readScene(char*): error: undefined reference to 'sphereCount'
hostCode.o:displayfunc.cpp:function readScene(char*): error: undefined reference to 'sphereCount'
hostCode.o:displayfunc.cpp:function readScene(char*): error: undefined reference to 'spheres'
hostCode.o:displayfunc.cpp:function readScene(char*): error: undefined reference to 'spheres'
hostCode.o:displayfunc.cpp:function readScene(char*): error: undefined reference to 'sphereCount'
hostCode.o:displayfunc.cpp:function idleFunc(): error: undefined reference to 'updateRendering()'
hostCode.o:displayfunc.cpp:function reshapeFunc(int, int): error: undefined reference to 'reInitCamera(bool)'
hostCode.o:displayfunc.cpp:function keyFunc(unsigned char, int, int): error: undefined reference to 'reInitCamera(bool)'

生成文件

CXX = g++
NVCC = nvcc -ccbin $(CXX)


INCLUDES := -I/home/cuda_app/inc/

LDFLAGS = -lGL -lGLU -lglut -lpthread


ALL:= test

test:    hostCode.o deviceCode.o

        $(NVCC) $(INCLUDES) -o $@ $< $(LDFLAGS)

deviceCode.o: SmallPtCUDA.cu

        $(NVCC) $(INCLUDES) -o $@ -c $< $(LDFLAGS)


hostCode.o: displayfunc.cpp

        $(CXX) $(INCLUDES) -o $@ -c $< $(LDFLAGS)

clean:

        rm -rf  *.o  $(ALL)

如何同时编译 .cpp.cu

有人可以帮忙吗?谢谢

最佳答案

当询问有关构建错误的问题时,最好不仅包括错误,还包括调用编译器或链接器的命令。有了这样的错误,问题就在那里;查看错误消息会有所帮助,但不是决定性的。

在您的情况下,问题是您的 makefile 中的链接命令不正确:

test:    hostCode.o deviceCode.o
        $(NVCC) $(INCLUDES) -o $@ $< $(LDFLAGS)

您正在使用 $<此处仅扩展为第一个 先决条件。如果您包含链接行并检查它,您会看到 deviceCode.o链接行上不存在文件。您希望您的链接命令看起来像这样:

test:    hostCode.o deviceCode.o
        $(NVCC) $(INCLUDES) -o $@ $^ $(LDFLAGS)

使用 $^这扩展到所有先决条件。

此外,请注意,按照惯例 LDFLAGS包含链接器选项,如 -L , 和 LDLIBS用于链接器选项,如 -l .但是,您的设置会起作用(只是不符合常规)。

关于c++ - 修改 CUDA 和 cpp Makefile,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28937214/

相关文章:

c++ - float1 与 CUDA 中的 float

c++ - 您使用什么库在 CUDA 上进行矩阵计算?

linux - 我如何在 Makefile 中获取用户名?

makefile - 如何在 GNU Make 模式规则中包含路径前缀

c++ - 如果将指针值作为引用传递,该值的生存期是多久?

C++ - 有没有办法只继承一些函数一次,而其他函数在多重继承中多次继承?

c++ - 如何在 QTreeView 中获取选择更改通知

c++ - C++ 中未对齐访问的正确性

cuda - 如何在 CUDA 设备代码中使用 Try-Catch 之类的东西

c++ - 编译 C++ 代码时如何解决版本控制问题?