- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
考虑三个文件:main.cpp、func_prototypes.h 和 test_kernels.cu。我想从这三个文件中构建一个项目。我试图基于 CUDA 示例来构建“makefile”,但我失败了。 make 的执行返回 undefined reference 的错误。下面是我的三个文件和makefile
main.cpp
#include <iostream>
#include <cstdlib>
#include <stdlib.h>
#include <stdio.h>
#include <new>
#include <cuda.h>
using namespace std;
#include "func_prototypes.h"
typedef float mytype;
int main(){
mytype *vec;
unsigned int N = 1024;
vec = new mytype[N];
for(int i = 0; i < N; i++){
vec[i] = i;
}
cout << "Calling CUDA function.\n";
getSquares(vec,N);
cout << "result:\n";
for(int i = 0; i < N; i++){
cout << vec[i] << " ";
}
ResetCUDA();
return EXIT_SUCCESS;
}
func_prototypes.h
template <class type>
void getSquares(type *v, unsigned const int N);
void ResetCUDA();
test_kernels.cu
#include <cuda.h>
#include <new>
#define BlockSize 256
template <class type>
__global__
void getSquareKernel(type *v, unsigned const int N){
int tIdx = blockIdx.x*blockDim.x + threadIdx.x;
if(tIdx < N){
v[tIdx] *= v[tIdx];
}
}
template <class type>
void getSquares(type *v, unsigned const int N){
int threads = BlockSize;
int blocks = ceil(N/threads);
type *d_v;
cudaMalloc(&d_v,N*sizeof(type));
cudaMemcpy(d_v,v,N*sizeof(type),cudaMemcpyHostToDevice);
getSquareKernel<<<blocks,threads>>>(d_v,N);
cudaMemcpy(v,d_v,N*sizeof(type),cudaMemcpyDeviceToHost);
cudaFree(d_v);
}
void ResetCUDA(){
cudaDeviceReset();
}
生成文件
############################# Makefile ##########################
CUDA_PATH ?= /usr/local/cuda-5.0
CUDA_INC_PATH ?= $(CUDA_PATH)/include
CUDA_BIN_PATH ?= $(CUDA_PATH)/bin
ifeq ($(OS_SIZE),32)
CUDA_LIB_PATH ?= $(CUDA_PATH)/lib
else
CUDA_LIB_PATH ?= $(CUDA_PATH)/lib64
endif
ifeq ($(OS_SIZE),32)
LDFLAGS := -L$(CUDA_LIB_PATH) -lcudart
CPPFLAGS := -m32
else
LDFLAGS := -L$(CUDA_LIB_PATH) -lcudart
CPPFLAGS := -m64
endif
# Debug build flags
ifeq ($(dbg),1)
CPPFLAGS += -g
NVCCFLAGS = -g -G
endif
INCLUDES := -I$(CUDA_INC_PATH) -I. -I.. -I../../common/inc
CPP = icpc
NVCC = $(CUDA_BIN_PATH)/nvcc
SOURCE = main.cpp
AUX = test_kernels.cu
all: test
test_kernels.o: $(AUX)
$(NVCC) $(NVCCFLAGS) -o test_kernels.o -c $(AUX) $(NVCCFLAGS) $(INCLUDES)
main.o: $(SOURCE)
$(CPP) $(CPPFLAGS) -o main.o -c $(SOURCE) $(CPPFLAGS) $(INCLUDES)
test: test_kernels.o main.o
$(CPP) -o test test_kernels.o main.o $(LDFLAGS)
run: test
./test
clean:
rm -rf test *.o
返回的错误是main.o:main.cpp:function main: error: undefined reference to 'void getSquares<float>(float*, unsigned int)'
make: *** [test] Error 1
有谁知道我的错误在哪里?
编辑:作为记录,我的操作系统是 Ubuntu 12.04 x86_64,内核 3.2.0-39
最佳答案
这是 standard gotcha 的体现在模板编译中。
您的宿主函数 getSquares
和内核 getSquareKernel
从未在定义它们的编译单元中实例化(即在 test_kernels.cu 中)。因此,编译器永远不会为 getSquares
发出任何代码,并且链接失败。
因为您在 test_kernels.cu 中使用组合的主机代码/设备代码编译轨迹,正确的解决方案是通过添加如下内容显式实例化您在 test_kernels.cu 中需要的模板代码的所有变体:
template __global__ void getSquareKernel<float>(float *, unsigned int);
template void getSquares<float>(float *, unsigned int);
到 test_kernels.cu 的底部。这将确保链接时需要链接的设备和主机代码实例都存在。
另一种选择是将包含 main 的文件更改为 .cu 文件,并将 test_kernels.cu 包含到该文件中,然后使用 nvcc 编译整个文件。在这种情况下,main()
中宿主类的实例化应该会触发同一编译单元中完整模板链的编译。
免责声明:我面前没有一台我可以对此进行测试的机器,所以至少要注意代码...
关于c++ - makefile: cpp + cu 文件 - undefined reference 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15969163/
这个问题在这里已经有了答案: Why can templates only be implemented in the header file? (18 个答案) 关闭 7 年前。 我的 .hpp
我想使用 yaml-cpp 发出一个带引号的字符串,所以它看起来像 时间戳:“2011 年 8 月 10 日 01:37:52” 在输出yaml文件中。我该怎么做?谢谢。 最佳答案 YAML::Emi
我理解了模板的概念以及为什么我们需要在头文件中定义模板成员函数。另一种选择是在 cpp 文件中定义模板函数并显式实例化模板类,如下所示。 模板.h #include using namespace
是否可以发出和读取(解析)二进制数据(图像、文件等)?如下所示: http://yaml.org/type/binary.html我如何在 yaml-cpp 中执行此操作? 最佳答案 截至revisi
我尝试查找此内容并使用头文件等得到混合结果。 基本上我有多个 .cpp 文件,其中包含我为使用二叉树而制作的所有函数,BST , 链表等 我不想复制和粘贴我需要的函数,我只想能够做一个: #inclu
我正在发出一个 YAML 文档,如下所示: YAML::Node doc; // ...populate doc... YAML::Emitter out; out << doc; 在节点层次结构的某
这个问题在这里已经有了答案: Access extern variable in C++ from another file (1 个回答) 关闭 4 年前。 考虑以下场景: MyFile.cpp:
所以我在上基础编程课,我们正在学习如何将文件链接在一起。问题是我遇到了一个似乎没有人能够修复的错误。我已经去过我的教授、学生助理和校园里的编程辅助实验室,但运气不佳。 我还在这里搜索了至少 10 篇与
在下面的代码中,我在使用 parser.GetNextDocument(doc); 解析我的 .yaml 文件时遇到了一些问题。经过大量调试后,我发现这里的(主要)问题是我的 for 循环没有运行,因
我们有以下类(class)考试成绩:完成本类(class)的学生中有 75 人参加了考试。我们想知道学生在考试中的表现如何,并给出了 75 名学生的分数。我们想编写一个程序,按以下方式总结和分析结果:
主要.cpp #include #include #include #include "cootie.h" using namespace std; int main() { cout
试图制作电子鸡程序,但编译器抛出未定义的对“Tamagotchi::age()”错误的引用 理想情况下,这段代码会返回电子鸡的年龄,它应该在开始时由类的构造函数初始化为 0。 我显然在某个地方搞砸了,
我一直在开发一个使用 Microsoft Visual Studio 2010 命令提示符编译原始 .cpp 文件并分析其输出的应用程序。我遇到了很多麻烦,网上似乎没有太多关于这个的资料。这是麻烦的代
我试图从另一个 .cpp 文件调用 c++ 函数。我使用了 .h header 。看看下面我做了什么。 我有一个f.h文件: #ifndef PACKAGENAME_ADD_H #define PAC
我在 CPP 中有一个函数,其原型(prototype)如下: char* complexFunction(char* arg1, ...); 我使用 DLLImport 属性从 C# 导入它。问题是
也许这是一个幼稚的问题 - 但有没有办法构建/安装 yaml-cpp,以便在构建包含 yaml.h 的项目时不需要使用 Boost 库 header ? IE:我正在开发一个使用 yaml-cpp 结
我有一个在 .cpp 函数中声明的静态函数,我不能在 header 中声明它,因为它不应该是可见的。我想在同一项目的另一个 .cpp 中重新使用它。 这有可能吗? 最佳答案 这里有两个问题: 这可能吗
我正在使用 php-cpp 为我的 php 代码创建扩展,当我尝试编译 main.cpp 文件的简单结构时,我得到这个错误。这是编译错误: main.cpp:15:5: error: ‘PHPCPP_
我决定将必要的代码减少到显示此错误所需的最低限度。我有一个存在于 hc_list.h 文件中的 STL 列表包装器模板类。完整代码如下: // hc_list.h file #ifndef HC_LI
您好,我目前正在尝试通过 AMQPCPP 将 RabbitMQ 集成到我的 VisualStudio 项目中。我只能使用 Windows PC,这对安装来说是一件很痛苦的事情。我想我能够使用 CMAK
我是一名优秀的程序员,十分优秀!