【问题标题】:Makefile for CUDA and CCUDA 和 C 的 Makefile
【发布时间】:2012-05-25 05:22:13
【问题描述】:

我正在开发一个使用 .c 和 .cu 文件的项目。原始包完全用 C 语言编写,并且有自己的 Makefile(它运行良好)。我将 .cu 文件添加到项目中,现在我想修改 Makefile 以便将所有内容编译在一起。

这是我的尝试:

CC = nvcc

SOURCEDIR = ../sourcedir

EXE   = it

#C_SOURCES = $(wildcard $(SOURCEDIR)/*.c)
#CU_SOURCES = $(wildcard $(SOURCEDIR)/*.cu)

SOURCES  = $(SOURCEDIR)/it.c \
           $(SOURCEDIR)/em.c \
           $(SOURCEDIR)/art.c \
           $(SOURCEDIR)/cg.c \
           $(SOURCEDIR)/amatrix.c \
           $(SOURCEDIR)/itreadargs.c \
           $(SOURCEDIR)/sparse.c \
           $(SOURCEDIR)/misc.c \
           $(SOURCEDIR)/eval.c \
           $(SOURCEDIR)/imgtools.c \
           $(SOURCEDIR)/calc.c \
           $(SOURCEDIR)/egif_lib.c \
           $(SOURCEDIR)/dgif_lib.c \
           $(SOURCEDIR)/gif_err.c \
           $(SOURCEDIR)/gif_hash.c
        
CU_SOURCES = $(SOURCEDIR)/cg_cuda.cu

H_FILES = $(wildcard $(IDIR)/*.h)

IDIR      = -I../include

OBJS        = $(SOURCES:.c=.o)
CU_OBJS = $(CU_SOURCES:.cu=.o)

CFLAGS     = -O3 
#-finline-functions -Winline -Wall -falign-loops=2 -falign-jumps=2 -falign-functions=2 -Wstrict-prototypes

NVCCFLAGS  = -arch=sm_20

#CFLAGS      = -g -Wstrict-prototypes -Winline -Wall

LFLAGS      = -lm


$(EXE) : $(OBJS) $(CU_OBJS) 
    $(CC) $(CFLAGS) $(NVCCFLAGS) -o $@ $?

$(SOURCEDIR)/%.o : $(SOURCEDIR)/%.c $(H_FILES)
    $(CC) $(CFLAGS) $(IDIR) -c -o $@ $<

$(SOURCEDIR)/%.o : $(SOURCEDIR)/%.cu $(H_FILES)
    $(CC) $(NVCCFLAGS) $(IDIR) -c -o $@ $<


clean:
    rm -f $(OBJS) $(EXE)

项目结构如下:

  • 项目
    • 包括
    • 源目录
    • 其他文件夹

其中 include 包含所有 .h 文件,sourcedir 包含 .c 和 .cu 文件(只有一个 .cu 文件); 有Makefile。

我的 Makefile 的问题是,当我在 it 文件夹中执行 ma​​ke 时,我收到一堆错误,告诉我具有 main() 的文件函数(sourcedir 文件夹中的 it.c)未链接到其他库中的任何函数。我的 .cu 文件也是如此。

您能否就我的 Makefile 可能有什么问题给我一些提示?我将以下 Stackoverflow 帖子用作参考: makefile for C++/CUDA project

感谢您的帮助,
弗拉德

编辑:
这是原始的 Makefile,它适用于 .c 文件。您能帮我添加一些内容以便将 .cu 文件与其他文件一起编译吗?再次感谢。

CC = gcc

SOURCEDIR = ../sourcedir

EXE   = it

SOURCES  = $(SOURCEDIR)/it.c \
           $(SOURCEDIR)/em.c \
           $(SOURCEDIR)/art.c \
           $(SOURCEDIR)/cg.c \
           $(SOURCEDIR)/amatrix.c \
           $(SOURCEDIR)/itreadargs.c \
           $(SOURCEDIR)/sparse.c \
           $(SOURCEDIR)/misc.c \
           $(SOURCEDIR)/eval.c \
           $(SOURCEDIR)/imgtools.c \
           $(SOURCEDIR)/calc.c \
           $(SOURCEDIR)/egif_lib.c \
           $(SOURCEDIR)/dgif_lib.c \
           $(SOURCEDIR)/gif_err.c \
           $(SOURCEDIR)/gif_hash.c

IDIR      = -I../include

OBJS        = $(SOURCES:.c=.o)

CFLAGS     = -O3 -finline-functions -Winline -Wall -falign-loops=2 -falign-jumps=2 -falign-functions=2 -Wstrict-prototypes

#CFLAGS      = -g -Wstrict-prototypes -Winline -Wall

LFLAGS      = -lm


$(EXE) : $(OBJS)
    $(CC) $(CFLAGS) -o $(EXE) $(OBJS) $(LFLAGS)

%.o : %.c
    $(CC) -c $(IDIR) $(CFLAGS) $< -o $@

clean:
    rm -f $(OBJS) $(EXE)

稍后编辑:
我对 Makefile 进行了一些更改,对其进行了一些清理,现在我只收到了一些错误,这些错误与 .cu 未链接到 .c 文件这一事实有关,反之亦然。

CC := gcc

SOURCEDIR := ../sourcedir

EXE   := it

SOURCES  := $(SOURCEDIR)/it.c \
           $(SOURCEDIR)/em.c \
           $(SOURCEDIR)/art.c \
           $(SOURCEDIR)/cg.c \
           $(SOURCEDIR)/amatrix.c \
           $(SOURCEDIR)/itreadargs.c \
           $(SOURCEDIR)/sparse.c \
           $(SOURCEDIR)/misc.c \
           $(SOURCEDIR)/eval.c \
           $(SOURCEDIR)/imgtools.c \
           $(SOURCEDIR)/calc.c \
           $(SOURCEDIR)/egif_lib.c \
           $(SOURCEDIR)/dgif_lib.c \
           $(SOURCEDIR)/gif_err.c \
           $(SOURCEDIR)/gif_hash.c
        
CU_SOURCES := $(SOURCEDIR)/cg_cuda.cu

IDIR      := ../include

INCLUDES := -I../include

H_FILES := $(IDIR)/analyze.h \
           $(IDIR)/calc.h \
           $(IDIR)/eval.h \
           $(IDIR)/gif_hash.h \
           $(IDIR)/gif_lib.h \
           $(IDIR)/imgtools.h \
           $(IDIR)/iradon.h \
           $(IDIR)/iradoninc.h \
           $(IDIR)/it.h \
           $(IDIR)/itini.h \
           $(IDIR)/misc.h \
           $(IDIR)/sparse.h         

CFLAGS := -g -O3

NVCCFLAGS  := -g -G -O3 -arch=sm_20

LDFLAGS     := -lGL -lGLU -lglut -lpthread -lcuda

HOST_OBJ := $(SOURCES:.c=.c.o)
DEVICE_OBJ := $(CU_SOURCES:.cu=.cu.o)

%.c.o : %.c $(HFILES)
    $(CC) -c $(INCLUDES) $(CFLAGS) $< -o $@

%.cu.o : %.cu $(H_FILES)
    nvcc -c $(INCLUDES) $(NVFLAGS) $< -o $@ 


$(EXE): $(HOST_OBJ) $(DEVICE_OBJ)
    nvcc $(NVFLAGS) $(LDFLAGS) $(INCLUDES) -o $@ $^

clean:
    rm -f $(OBJS) $(EXE)

所以现在我收到了这些错误:

nvcc  -lGL -lGLU -lglut -lpthread -lcuda -I../include -o it ../sourcedir/it.c.o ../sourcedir/em.c.o ../sourcedir/art.c.o ../sourcedir/cg.c.o ../sourcedir/amatrix.c.o ../sourcedir/itreadargs.c.o ../sourcedir/sparse.c.o ../sourcedir/misc.c.o ../sourcedir/eval.c.o ../sourcedir/imgtools.c.o ../sourcedir/calc.c.o ../sourcedir/egif_lib.c.o ../sourcedir/dgif_lib.c.o ../sourcedir/gif_err.c.o ../sourcedir/gif_hash.c.o ../sourcedir/cg_cuda.cu.o
../sourcedir/it.c.o: In function `main':
/home/vburca/CUDA_Research_2012/Recon2D/it/../sourcedir/it.c:280: undefined reference to `CG_CUDA'
../sourcedir/cg_cuda.cu.o: In function `CGUpdateAddVector(Vector*, Vector*, Vector*, float)':
tmpxft_00004e1d_00000000-1_cg_cuda.cudafe1.cpp:(.text+0x44): undefined reference to `Error(char*, ...)'
../sourcedir/cg_cuda.cu.o: In function `CG_CUDA(SparseMatrix*, Vector*, Vector*)':
tmpxft_00004e1d_00000000-1_cg_cuda.cudafe1.cpp:(.text+0x15f): undefined reference to `Print(int, char*, ...)'
tmpxft_00004e1d_00000000-1_cg_cuda.cudafe1.cpp:(.text+0x18c): undefined reference to `ReadFIF(char*)'
tmpxft_00004e1d_00000000-1_cg_cuda.cudafe1.cpp:(.text+0x1a2): undefined reference to `ImageToVector(Image*)'
tmpxft_00004e1d_00000000-1_cg_cuda.cudafe1.cpp:(.text+0x1b8): undefined reference to `FreeImage(Image*)'
tmpxft_00004e1d_00000000-1_cg_cuda.cudafe1.cpp:(.text+0x1c7): undefined reference to `DeviationVector(Vector*)'
tmpxft_00004e1d_00000000-1_cg_cuda.cudafe1.cpp:(.text+0x248): undefined reference to `Print(int, char*, ...)'
tmpxft_00004e1d_00000000-1_cg_cuda.cudafe1.cpp:(.text+0x255): undefined reference to `InitVector(int)'
tmpxft_00004e1d_00000000-1_cg_cuda.cudafe1.cpp:(.text+0x269): undefined reference to `InitVector(int)'
tmpxft_00004e1d_00000000-1_cg_cuda.cudafe1.cpp:(.text+0x27d): undefined reference to `InitVector(int)'
tmpxft_00004e1d_00000000-1_cg_cuda.cudafe1.cpp:(.text+0x291): undefined reference to `InitVector(int)'
tmpxft_00004e1d_00000000-1_cg_cuda.cudafe1.cpp:(.text+0x2a5): undefined reference to `InitVector(int)'
tmpxft_00004e1d_00000000-1_cg_cuda.cudafe1.cpp:(.text+0x2c0): undefined reference to `Print(int, char*, ...)'
tmpxft_00004e1d_00000000-1_cg_cuda.cudafe1.cpp:(.text+0x2e0): undefined reference to `MultSparseMatrixVector(SparseMatrix*, Vector*, Vector*)'
tmpxft_00004e1d_00000000-1_cg_cuda.cudafe1.cpp:(.text+0x328): undefined reference to `MultSparseTMatrixVector(SparseMatrix*, Vector*, Vector*)'
tmpxft_00004e1d_00000000-1_cg_cuda.cudafe1.cpp:(.text+0x37c): undefined reference to `MultSparseMatrixVector(SparseMatrix*, Vector*, Vector*)'
tmpxft_00004e1d_00000000-1_cg_cuda.cudafe1.cpp:(.text+0x395): undefined reference to `MultVectorVector(Vector*, Vector*)'
tmpxft_00004e1d_00000000-1_cg_cuda.cudafe1.cpp:(.text+0x3b1): undefined reference to `Print(int, char*, ...)'
tmpxft_00004e1d_00000000-1_cg_cuda.cudafe1.cpp:(.text+0x3fb): undefined reference to `Print(int, char*, ...)'
tmpxft_00004e1d_00000000-1_cg_cuda.cudafe1.cpp:(.text+0x414): undefined reference to `MultVectorVector(Vector*, Vector*)'
tmpxft_00004e1d_00000000-1_cg_cuda.cudafe1.cpp:(.text+0x47b): undefined reference to `ConstrainVector(Vector*, float, float)'
tmpxft_00004e1d_00000000-1_cg_cuda.cudafe1.cpp:(.text+0x4ce): undefined reference to `MultSparseTMatrixVector(SparseMatrix*, Vector*, Vector*)'
tmpxft_00004e1d_00000000-1_cg_cuda.cudafe1.cpp:(.text+0x4e7): undefined reference to `MultVectorVector(Vector*, Vector*)'
tmpxft_00004e1d_00000000-1_cg_cuda.cudafe1.cpp:(.text+0x55b): undefined reference to `MultSparseMatrixVector(SparseMatrix*, Vector*, Vector*)'
tmpxft_00004e1d_00000000-1_cg_cuda.cudafe1.cpp:(.text+0x581): undefined reference to `SaveIteration(Vector*, int, char*)'
tmpxft_00004e1d_00000000-1_cg_cuda.cudafe1.cpp:(.text+0x5ab): undefined reference to `L2NormVector(Vector*, Vector*, float)'
tmpxft_00004e1d_00000000-1_cg_cuda.cudafe1.cpp:(.text+0x602): undefined reference to `Print(int, char*, ...)'
tmpxft_00004e1d_00000000-1_cg_cuda.cudafe1.cpp:(.text+0x61f): undefined reference to `VectorToImage(Vector*, int, int)'
tmpxft_00004e1d_00000000-1_cg_cuda.cudafe1.cpp:(.text+0x650): undefined reference to `L2NormVector(Vector*, Vector*, float)'
tmpxft_00004e1d_00000000-1_cg_cuda.cudafe1.cpp:(.text+0x66a): undefined reference to `Print(int, char*, ...)'
tmpxft_00004e1d_00000000-1_cg_cuda.cudafe1.cpp:(.text+0x679): undefined reference to `FreeVector(Vector*)'
tmpxft_00004e1d_00000000-1_cg_cuda.cudafe1.cpp:(.text+0x69c): undefined reference to `RenameImage(Image*, char*)'
collect2: ld returned 1 exit status
make: *** [it] Error 1

感谢您耐心阅读我的帖子。

【问题讨论】:

  • 在引入.cu 文件之前,您确定这有效吗?无论如何,我建议你尝试将$? 更改为$^
  • 好吧,我改了;我也可以发布原始的 Makefile ......也许这将有助于我需要添加的内容,以便 .cu 文件也能编译。
  • 您没有向我们提供足够的信息(例如错误消息)来确定确切的问题,但是您在这两个 makefile 之间进行了许多小的更改。尝试二分查找:编写一个只包含其中一半更改的 makefile,看看它是否有效。如果您发现 一个改变 可以产生影响,但仍然没有意义,请告诉我们。
  • 这是错误的一部分(对于每个定义的函数,我得到了很多类似的行):>
  • 所有.cu文件必须在相同编译单元中编译。

标签: c cuda makefile nvcc


【解决方案1】:

好的,这将需要几次迭代。试试这个并评论结果:

CC = nvcc

SOURCEDIR = ../sourcedir

EXE   = it

SOURCES  = $(SOURCEDIR)/it.c \
           $(SOURCEDIR)/em.c \
           $(SOURCEDIR)/art.c \
           $(SOURCEDIR)/cg.c \
           $(SOURCEDIR)/amatrix.c \
           $(SOURCEDIR)/itreadargs.c \
           $(SOURCEDIR)/sparse.c \
           $(SOURCEDIR)/misc.c \
           $(SOURCEDIR)/eval.c \
           $(SOURCEDIR)/imgtools.c \
           $(SOURCEDIR)/calc.c \
           $(SOURCEDIR)/egif_lib.c \
           $(SOURCEDIR)/dgif_lib.c \
           $(SOURCEDIR)/gif_err.c \
           $(SOURCEDIR)/gif_hash.c

IDIR      = -I../include

OBJS        = $(SOURCES:.c=.o)

CFLAGS     = -O3

NVCCFLAGS  = -arch=sm_20

LFLAGS      = -lm

$(EXE) : $(OBJS) $(SOURCEDIR)/cg_cuda.o
    $(CC) $(CFLAGS) -o $@ $^ $(LFLAGS)

$(SOURCEDIR)/%.o : $(SOURCEDIR)/%.c
    $(CC) $(NVCCFLAGS) $(IDIR) -c -o $@ $<

$(SOURCEDIR)/%.o : $(SOURCEDIR)/%.cu $(H_FILES)
    $(CC) $(NVCCFLAGS) $(IDIR) -c -o $@ $<

clean:
    rm -f $(OBJS) $(EXE)

编辑:第 2 轮
我已经修改了makefile。尝试make clean ; make 并注意结果。那就试试make ../sourcedir/cg_cuda.o

编辑:第三轮
好的,再试一次:make clean ; make

【讨论】:

  • make: 警告: 文件Makefile' has modification time 3e+02 s in the future nvcc -O3 -o it ../sourcedir/it.o ../sourcedir/em.o ../sourcedir/art.o ../sourcedir/cg.o ../sourcedir/amatrix.o ../sourcedir/itreadargs.o ../sourcedir/sparse.o ../sourcedir/misc.o ../sourcedir/eval.o ../sourcedir/imgtools.o ../sourcedir/calc.o ../sourcedir/egif_lib.o ../sourcedir/dgif_lib.o ../sourcedir/gif_err.o ../sourcedir/gif_hash.o -lm ../sourcedir/it.o: In function main': it.c:(.text+0x188): undefined reference to `CG_CUDA' collect2: ld returned 1 exit status make: *** [it ] 错误 1
  • 我第一次运行 make 它产生了一堆警告等,但第二次我只收到上面发布的错误。
  • 我意识到这并不能直接回答您的问题,但我发现使用 cmake 更容易用于 cuda 项目。我指定 cmake 列表文件,然后让 cmake 为我生成 Makefile。如果有兴趣,我可以向您发送一个简单的示例项目,说明这是如何工作的。
  • 很抱歉这么晚才回复,但运行 CUDA 的机器出现问题,无法尝试您的建议。感谢您的耐心等待!
  • 用于清洁; make ::::: make: Warning: File Makefile' has modification time 3.9e+02 s in the future nvcc -O3 -o it ../sourcedir/it.o ../sourcedir/em.o ../sourcedir/art.o ../sourcedir/cg.o ../sourcedir/amatrix.o ../sourcedir/itreadargs.o ../sourcedir/sparse.o ../sourcedir/misc.o ../sourcedir/eval.o ../sourcedir/imgtools.o ../sourcedir/calc.o ../sourcedir/egif_lib.o ../sourcedir/dgif_lib.o ../sourcedir/gif_err.o ../sourcedir/gif_hash.o -lm ../sourcedir/it.o: In function main': it.c:(.text+0x5b9): undefined reference to 'CG_CUDA' collect2: ld returned 1 exit status make: *** [it] Error 1
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-05
  • 2013-03-10
  • 2013-11-06
  • 2017-06-14
相关资源
最近更新 更多