2012-05-17 13 views
7

Estoy trabajando en un proyecto que usa archivos .c y .cu. El paquete original estaba escrito completamente en C y tenía su propio Makefile (funcionaba perfectamente). Agregué el archivo .cu al proyecto y ahora quiero modificar el Makefile para que compile todo junto.Makefile para CUDA y C

Aquí es mi intento:

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 [email protected] $? 

$(SOURCEDIR)/%.o : $(SOURCEDIR)/%.c $(H_FILES) 
    $(CC) $(CFLAGS) $(IDIR) -c -o [email protected] $< 

$(SOURCEDIR)/%.o : $(SOURCEDIR)/%.cu $(H_FILES) 
    $(CC) $(NVCCFLAGS) $(IDIR) -c -o [email protected] $< 


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

La estructura del proyecto es el siguiente:

  • Proyecto
    • incluyen
    • sourcedir
    • que
    • otras carpetas

donde incluir tiene todos los archivos .h y sourcedir tiene la .cy los archivos .cu (hay sólo un archivo .cu); es tiene el archivo Makefile.

El problema con mi Makefile es que cuando lo haga hacer en la carpeta que consigo un montón de errores que me dice que el archivo que tiene la función main() (it.c en sourcedir carpeta) no se vincula a ninguna de las funciones de otras bibliotecas. Lo mismo vale para mi archivo .cu.

¿Puede darme algunas pistas sobre lo que podría estar mal con mi Makefile? He utilizado como referencia el siguiente post Stackoverflow: makefile for C++/CUDA project

Gracias por su ayuda,
Vlad

EDIT:
Aquí es el Makefile original, el que trabajó en los archivos .c. ¿Puede ayudarme con lo que necesito agregar para que el archivo .cu se compile junto con los demás? Gracias de nuevo. EDITAR

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 [email protected] 

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

TARDE:
me cambió el Makefile un poco más, lo limpió un poco, y ahora sólo recibe algunos errores que se relacionan con el hecho de que el .cu no se está ligado a la .c archivos y viceversa.

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 [email protected] 

%.cu.o : %.cu $(H_FILES) 
    nvcc -c $(INCLUDES) $(NVFLAGS) $< -o [email protected] 


$(EXE): $(HOST_OBJ) $(DEVICE_OBJ) 
    nvcc $(NVFLAGS) $(LDFLAGS) $(INCLUDES) -o [email protected] $^ 

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

Así que ahora estoy recibiendo estos errores:

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 

Gracias por su paciencia de leer mi post.

+1

¿Está seguro de que esto funcionó antes de presentar el archivo '.cu'? De todos modos, te sugiero que intentes cambiar '$?' A '$ ^'. – Beta

+0

Bueno, lo cambié; También puedo publicar el Makefile original ... tal vez eso ayude con lo que necesito agregar para que los archivos .cu se compilen también. – vburca

+0

No nos ha dado suficiente información (por ejemplo, el mensaje de error) para estar seguros del problema exacto, pero ha realizado muchos pequeños cambios entre estos dos archivos make. Pruebe con una búsqueda binaria: escriba un archivo MAKE con solo la mitad de estos cambios y vea si funciona. Si encuentra * un cambio * que marque la diferencia, y aún así no tiene sentido, infórmenos. – Beta

Respuesta

2

Bien, esto tomará algunas iteraciones.Prueba esto y comentar con los resultados:

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 [email protected] $^ $(LFLAGS) 

$(SOURCEDIR)/%.o : $(SOURCEDIR)/%.c 
    $(CC) $(NVCCFLAGS) $(IDIR) -c -o [email protected] $< 

$(SOURCEDIR)/%.o : $(SOURCEDIR)/%.cu $(H_FILES) 
    $(CC) $(NVCCFLAGS) $(IDIR) -c -o [email protected] $< 

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

EDIT: la ronda 2
He modificado el makefile. Pruebe make clean ; make y tenga en cuenta el resultado. Luego intente make ../sourcedir/cg_cuda.o.

EDIT: la ronda 3
bien, intentarlo de nuevo: make clean ; make.

+0

make: Advertencia: el archivo 'Makefile 'tiene un tiempo de modificación 3e + 02 s en el futuro 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: en la función 'main ': it.c :(. text + 0x188): referencia indefinida a' CG_CUDA' collect2: ld devuelto 1 estado de salida make: *** [it] Error 1 – vburca

+0

La primera vez que ejecuté generé un montón de advertencias, pero la segunda vez solo recibí el error publicado anteriormente. – vburca

+0

¡Muchas gracias por su ayuda! – vburca