本文介绍了makefile for C ++ / CUDA项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已阅读几乎所有关于CUDA,C ++& makefile在这里,但仍然不能解决我的问题。

I've read almost all questions about CUDA, C++ & makefiles here, but still can't figure solution to my problem.

我有一些 .cpp 文件& ;一些 .cu 文件在我的项目的 src / 目录中( .h & .cuh ),我想用makefile创建我的应用程序。

I have a some .cpp files & some .cu files inside src/ directory of my project (along with .h & .cuh), and I'd like to build my application with a makefile.

我尝试这样做:

SRC_DIR   = src
OBJ_DIR   = obj

CPP_FILES = $(wildcard $(SRC_DIR)/*.cpp)
CU_FILES  = $(wildcard $(SRC_DIR)/*.cu)

H_FILES   = $(wildcard $(SRC_DIR)/*.h)
CUH_FILES = $(wildcard $(SRC_DIR)/*.cuh)

OBJ_FILES = $(addprefix $(OBJ_DIR)/,$(notdir $(CPP_FILES:.cpp=.o)))
CUO_FILES = $(addprefix $(OBJ_DIR)/,$(notdir $(CU_FILES:.cu=.cu.o)))

$(TARGET) : $(OBJ_FILES) $(CUO_FILES)
    $(LD) $(LDFLAGS) $(LIB_CUDA) -o $@ $?

$(CUO_FILES) : $(CU_FILES) $(CUH_FILES)
    $(NVCC) $(NVCCFLAGS) $(INCLUDES) -c -o $@ $<

$(OBJ_DIR)/%.o : $(SRC_DIR)/%.cpp $(H_FILES)
    $(NVCC) $(NVCCFLAGS) $(INCLUDES) -c -o $@ $<

这是正常的,直到我有一个.cu文件。
然后我尝试:

And it was OK until I've got a second .cu file.And then I tried:

<... previous part stays the same ...>
OBJS =  $(patsubst %.cpp,$(OBJ_DIR)/%.o,$(notdir $(CPP_FILES)))
OBJS += $(patsubst %.cu,$(OBJ_DIR)/%.cu.o,$(notdir $(CU_FILES)))

$(TARGET) : $(OBJS)
    $(LD) $(LDFLAGS) $(LIB_CUDA) -o $@ $?

$(OBJ_DIR)/%.cu.o : $(SRC_DIR)/%.cu $(CUH_FILES)
    $(NVCC) $(NVCCFLAGS) $(INCLUDES) -c -o $@ $<

$(OBJ_DIR)/%.o : $(SRC_DIR)/%.cpp $(H_FILES)
    $(NVCC) $(NVCCFLAGS) $(INCLUDES) -c -o $@ $<

make 无法弄清楚如何制作 .cuoo 文件。
我应该如何修改这个东西来构建我的应用程序?

But make can't figure out how to make any of the .cu.o files now.How should I modify this thing to build my application?

提前感谢!

strong>更新 - 使用第二个makefile输出make:

Upd - output of make with second makefile:

/usr/local/cuda/bin/nvcc  -I/usr/local/cuda/include -c -o obj/main.o src/main.cpp
/usr/local/cuda/bin/nvcc  -I/usr/local/cuda/include -c -o obj/util.o src/util.cpp
make: *** No rule to make target `obj/thrust.cu.o', needed by `DCG'.  Stop.

项目文件(src /):

project files (src/):


  • main.cpp

  • utils.h

  • util.cpp

  • thrust.cu

  • thrust.cuh

  • cuda-utils.cu

  • cuda-utils.cuh

  • main.cpp
  • utils.h
  • util.cpp
  • thrust.cu
  • thrust.cuh
  • cuda-utils.cu
  • cuda-utils.cuh

推荐答案

或者你有一个语法错误在你没有显示的地方,的项目不是你所描述的。如果我使用你的Makefile的这个模型:

Either you have a syntax error in your Makefile somewhere you are not showing, or the layout of your project isn't as you have described. If I take this model of your Makefile:

TARGET    = nothing
SRC_DIR   = src
OBJ_DIR   = obj

CPP_FILES = $(wildcard $(SRC_DIR)/*.cpp)
CU_FILES  = $(wildcard $(SRC_DIR)/*.cu)

H_FILES   = $(wildcard $(SRC_DIR)/*.h)
CUH_FILES = $(wildcard $(SRC_DIR)/*.cuh)

OBJ_FILES = $(addprefix $(OBJ_DIR)/,$(notdir $(CPP_FILES:.cpp=.o)))
CUO_FILES = $(addprefix $(OBJ_DIR)/,$(notdir $(CU_FILES:.cu=.

OBJS =  $(patsubst %.cpp,$(OBJ_DIR)/%.o,$(notdir $(CPP_FILES)))
OBJS += $(patsubst %.cu,$(OBJ_DIR)/%.cu.o,$(notdir $(CU_FILES)))

$(TARGET) : $(OBJS)
    echo "linking rule : " -o $@ $?

$(OBJ_DIR)/%.cu.o : $(SRC_DIR)/%.cu $(CUH_FILES)
    echo ".cu.o rule : " $@ $<
    touch $@

$(OBJ_DIR)/%.o : $(SRC_DIR)/%.cpp $(H_FILES)
    echo ".o rule : " $@ $<
    touch $@

然后我创建一个你所描述的模型:

and then I make a model of what you have described:

$ mkdir src
$ mkdir obj
$ touch  src/main.cpp
$ touch  src/cuda-utils.cuh
$ touch  src/thrust.cu
$ touch  src/cuda-utils.cu
$ touch  src/util.cpp
$ touch  src/main.cpp

$ ls
Makefile    obj     src

$ ls src
cuda-utils.cu   cuda-utils.cuh  main.cpp    thrust.cu   util.cpp

$ ls obj

然后我运行make:

$ make
echo ".o rule : " obj/main.o src/main.cpp
.o rule :  obj/main.o src/main.cpp
touch obj/main.o
echo ".o rule : " obj/util.o src/util.cpp
.o rule :  obj/util.o src/util.cpp
touch obj/util.o
echo ".cu.o rule : " obj/cuda-utils.cu.o src/cuda-utils.cu
.cu.o rule :  obj/cuda-utils.cu.o src/cuda-utils.cu
touch obj/cuda-utils.cu.o
echo ".cu.o rule : " obj/thrust.cu.o src/thrust.cu
.cu.o rule :  obj/thrust.cu.o src/thrust.cu
touch obj/thrust.cu.o
echo "linking rule : " -o nothing obj/main.o obj/util.o obj/cuda-utils.cu.o obj/thrust.cu.o
linking rule :  -o nothing obj/main.o obj/util.o obj/cuda-utils.cu.o obj/thrust.cu.o

$ ls obj
cuda-utils.cu.o main.o      thrust.cu.o util.o

我得到的是正确的。所以如果你有一个问题,它不是从你发布的问题(在原来的版本中的几个拼写错误修复后)。

I get exactly what is expected. So if you are having a problem, it is not coming from what you have posted in your question (after the several "typos" in the original version were fixed).

这篇关于makefile for C ++ / CUDA项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 15:21