一个非常简单的Makefile应该是什么样子的,以便在linux下编译Cuda

发布于 2024-08-08 19:04:53 字数 216 浏览 3 评论 0原文

我想在Linux下编译一个非常基本的hello world级别的Cuda程序。我有三个文件:

  • 内核:helloWorld.cu
  • 主要方法:helloWorld.cpp
  • 公共头文件:helloWorld.h

你能给我写一个简单的 Makefile 来用 nvcc 和 g++ 编译它吗?

谢谢,
加博尔

I want to compile a very basic hello world level Cuda program under Linux. I have three files:

  • the kernel: helloWorld.cu
  • main method: helloWorld.cpp
  • common header: helloWorld.h

Could you write me a simple Makefile to compile this with nvcc and g++?

Thanks,
Gabor

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

内心激荡 2024-08-15 19:04:53

我以前从未听说过 Cuda,但从在线文档来看,X.cu 似乎应该编译成 Xo,因此拥有 helloWorld.cu 和 helloWorld.cpp 并不是一个好主意。在您的许可下,我将重命名“内核”helloKernel.cu,然后这应该可以工作:(

NVCC = nvcc

helloWorld.o: helloWorld.cpp helloWorld.h
    $(NVCC) -c %< -o $@

helloKernel.o: helloKernel.cu
    $(NVCC) -c %< -o $@

helloWorld: helloWorld.o helloKernel.o
    $(NVCC) %^ -o $@

请注意,那些前导空格是制表符。)

如果可以,请尝试更灵活的版本:

NVCC = nvcc

helloWorld.o: %.o : %.cpp %.h
helloKernel.o: %.o : %.cu

%.o:
    $(NVCC) -c %< -o $@

helloWorld: helloWorld.o helloKernel.o
    $(NVCC) %^ -o $@

I've never heard of Cuda before, but from the online documentation it looks as if X.cu is supposed to be compiled into X.o, so having helloWorld.cu and helloWorld.cpp is not a good idea. With your permission I'll rename the "kernel" helloKernel.cu, then this should work:

NVCC = nvcc

helloWorld.o: helloWorld.cpp helloWorld.h
    $(NVCC) -c %< -o $@

helloKernel.o: helloKernel.cu
    $(NVCC) -c %< -o $@

helloWorld: helloWorld.o helloKernel.o
    $(NVCC) %^ -o $@

(Note that those leading spaces are tabs.)

If that works, try a slicker version:

NVCC = nvcc

helloWorld.o: %.o : %.cpp %.h
helloKernel.o: %.o : %.cu

%.o:
    $(NVCC) -c %< -o $@

helloWorld: helloWorld.o helloKernel.o
    $(NVCC) %^ -o $@
长梦不多时 2024-08-15 19:04:53

以防万一,这是我的变体。我用它在 Mac 上编译 CUDA 项目,但我认为它也适合 Linux。它需要 CUDA SDK。

BINDIR = ./ # places compiled binary in current directory
EXECUTABLE := helloWorld

CCFILES := helloWorld.cpp
CUFILES := helloWorld.cu

# an ugly part - setting rootdir for CUDA SDK makefile
# look for common.mk - I don't know where SDK installs it on Linux -
# and change ROOTDIR accordingly 
ROOTDIR := /Developer/GPU\ Computing/C/common

include $(ROOTDIR)/../common/common.mk

Just in case, here's my variant. I use it to compile CUDA projects on Mac, but I think it will suit Linux too. It requires CUDA SDK.

BINDIR = ./ # places compiled binary in current directory
EXECUTABLE := helloWorld

CCFILES := helloWorld.cpp
CUFILES := helloWorld.cu

# an ugly part - setting rootdir for CUDA SDK makefile
# look for common.mk - I don't know where SDK installs it on Linux -
# and change ROOTDIR accordingly 
ROOTDIR := /Developer/GPU\ Computing/C/common

include $(ROOTDIR)/../common/common.mk
柠檬 2024-08-15 19:04:53

我的版本,冗长但透明:

myapp: myapp.o
    g++ -fPIC -o $@ 
lt; -L /usr/local/cuda/lib -lcudart

myapp.o: myapp.cu
    /usr/local/cuda/bin/nvcc --compiler-options -fno-strict-aliasing \
    -I/usr/local/cuda/include \
    -DUNIX -O2 -o $@ -c 
lt;

matrixMul: matrixMul.o
    g++ -fPIC -o $@ 
lt; -L /usr/local/cuda/lib -lcudart

# It MUST be named .cu or nvcc compiles as regular C !!! (no __global__)
matrixMul.o: matrixMul.cu
    /usr/local/cuda/bin/nvcc --compiler-options -fno-strict-aliasing \
    -I/usr/local/cuda/include \
    -DUNIX -O2 -o $@ -c 
lt;

My version, verbose but transparent:

myapp: myapp.o
    g++ -fPIC -o $@ 
lt; -L /usr/local/cuda/lib -lcudart

myapp.o: myapp.cu
    /usr/local/cuda/bin/nvcc --compiler-options -fno-strict-aliasing \
    -I/usr/local/cuda/include \
    -DUNIX -O2 -o $@ -c 
lt;

matrixMul: matrixMul.o
    g++ -fPIC -o $@ 
lt; -L /usr/local/cuda/lib -lcudart

# It MUST be named .cu or nvcc compiles as regular C !!! (no __global__)
matrixMul.o: matrixMul.cu
    /usr/local/cuda/bin/nvcc --compiler-options -fno-strict-aliasing \
    -I/usr/local/cuda/include \
    -DUNIX -O2 -o $@ -c 
lt;
忘你却要生生世世 2024-08-15 19:04:53

这是我当前项目的示例。正如你所看到的,有一些 OpenGL 库,

ce : cudaExample.c cudaExample.h
    cp cudaExample.c cudaExample.cu
    /usr/local/cuda/bin/nvcc -arch=sm_20 -o ce -lglut -lGL -lGLU -lXext -lXmu -lX11 -lm  cudaExample.cu

然后运行 ​​make ce
./ce

Here is an example what my current project looks like. As you can see there is a few OpenGL libraries

ce : cudaExample.c cudaExample.h
    cp cudaExample.c cudaExample.cu
    /usr/local/cuda/bin/nvcc -arch=sm_20 -o ce -lglut -lGL -lGLU -lXext -lXmu -lX11 -lm  cudaExample.cu

then run make ce
and ./ce

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文