【问题标题】:Host and device function makefile compilation error主机和设备函数makefile编译错误
【发布时间】:2015-07-01 15:36:43
【问题描述】:

我有一个包含几个文件(.cu、.cpp、.h)的项目,我想编译并链接它。

我的文件如下:

1) Graph.cpp - 只是 c++ 代码

2) Graph.h - 上面的标题(工作正常)

3) Common.h - __host__ __device__ 函数

#ifndef COMMON_CUH
#define COMMON_CUH

#include "cuda_runtime.h"
#include "device_launch_parameters.h"

__host__ __device__ unsigned int fun1(int, int);
__host__ __device__ int fun2(int);
__host__ __device__ int fun3(int);
__host__ __device__ unsigned int fun4(int, int);

#endif

4) Common.cu - 上述实现

#include "cuda_runtime.h"
#include "device_launch_parameters.h"

#include "Common.h"


__host__ __device__ unsigned int fun1(int a, int n) 
{
    ...
}

...other functions...

5) kernel.cu - 主文件

#include "cuda_runtime.h"
#include "device_launch_parameters.h"

#include "Graph.h"
#include "LogCreate.h"
#include "Common.h"

some __global__ functions. c++ functions, main etc.

我尝试使用的一个 makefile:

CUDA_INSTALL_PATH ?= /usr/local/cuda

# Compilers
CXX := g++
CC := gcc
LINK := g++ -fPIC
NVCC  := nvcc -ccbin /usr/bin

# Includes
INCLUDES = -I. -I$(CUDA_INSTALL_PATH)/include

# Common flags
COMMONFLAGS += $(INCLUDES)
NVCCFLAGS += $(COMMONFLAGS)
NVCCFLAGS += -arch=sm_35
# Debug mode
NVCCFLAGS += --compiler-options -Wall -G
CXXFLAGS += $(COMMONFLAGS)
CFLAGS += $(COMMONFLAGS)

LIB_CUDA := -L$(CUDA_INSTALL_PATH)/lib64 -lcudart

OBJS = Common.cuh.o Graph.cpp.o LogCreate.cpp.o kernel.cu.o
TARGET = main
LINKLINE = $(LINK) -o $(TARGET) $(OBJS) $(LIB_CUDA)

.SUFFIXES: .c .cpp .h .cu .cuh .o

%.cuh.o: %.cu %.cuh
    $(NVCC) $(NVCCFLAGS) -dc $< -o $@

%.cu.o: %.cu
    $(NVCC) $(NVCCFLAGS) -dc $< -o $@

%.cpp.o: %.cpp %.h
    $(CXX) $(CXXFLAGS) -c $< -o $@

$(TARGET): $(OBJS) Makefile
    $(LINKLINE)

clean: 
    rm -rf *.o

这给了我:

Common.cuh.o: In function `__sti____cudaRegisterAll_41_tmpxft_0000107c_00000000_6_Common_cpp1_ii__Z3Powii()':
tmpxft_0000107c_00000000-3_Common.cudafe1.cpp:(.text+0x1c1): undefined reference to `__cudaRegisterLinkedBinary_41_tmpxft_0000107c_00000000_6_Common_cpp1_ii__Z3Powii'
kernel.cu.o: In function `__sti____cudaRegisterAll_41_tmpxft_000010ad_00000000_6_kernel_cpp1_ii_filename()':
tmpxft_000010ad_00000000-3_kernel.cudafe1.cpp:(.text+0x2f33): undefined reference to `__cudaRegisterLinkedBinary_41_tmpxft_000010ad_00000000_6_kernel_cpp1_ii_filename'
collect2: error: ld returned 1 exit status
make: *** [main] Error 1

我猜(Here's a link) 可能是我的问题的描述,但我不知道如何处理。

另一种编写makefile的尝试是(不太通用):

setting like before and main part:
all:
    $(NVCC) $(NVCCFLAGS) -dc Common.cu kernel.cu
    $(NVCC) $(NVCCFLAGS) -dlink Common.o kernel.o -o link.o
    $(CXX) $(CXXFLAGS) -c Graph.cpp Graph.h -o graph.o
    $(LINK) -o main Common.o kernel.o link.o graph.o $(LIB_CUDA)

这给了我:

g++ -fPIC -o main Common.o kernel.o link.o graph.o -L/usr/local/cuda/lib64 -lcudart
graph.o: file not recognized: File format not recognized
collect2: error: ld returned 1 exit status
make: *** [all] Error 1

我已经搜索了很多网站,但仍然不知道如何修复它。我阅读了我之前放置的链接的某些部分,但也许我错过了一些东西或者只是不明白。

请解释在 Makefile 中编译/链接时处理许多文件(使用主机和设备代码)的正确方法是什么。谢谢

【问题讨论】:

    标签: c++ cuda makefile


    【解决方案1】:

    您的第一个方法没有正确处理CUDA separate compilation(您没有设备链接步骤)。

    你的第二种方法更接近目标。

    这是错误的:

    $(CXX) $(CXXFLAGS) -c Graph.cpp Graph.h -o graph.o
    

    应该是:

    $(CXX) $(CXXFLAGS) -c Graph.cpp -o graph.o
    

    【讨论】:

    • 将来,为了避免浪费时间,甚至可能不得不发布问题,请尝试使用谷歌搜索 "g++ file not recognized: File format not recognized" 然后 take the first hit。也许我应该将此问题标记为与该问题重复。
    • 换句话说,为了概括建议,如果您有不理解的编译错误,您可以尝试使用谷歌搜索确切的编译错误输出。其他人很有可能遇到该错误,并且可能已经记录了问题所在以及如何修复它。在这种情况下,它可能会导致您提出与您的问题非常相似的问题,可能会节省您“浪费的几个小时”。
    • 我通过谷歌搜索了许多问题组合,搜索了许多网站,但没有找到任何东西。当然,我没有尝试过你输入的这个。所以这不是不尝试(我什至尝试过没有匹配结果的输出),而是不知道它可能是什么类型的错误:) 无论如何感谢您的建议。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-27
    • 2012-03-24
    相关资源
    最近更新 更多