gpt4 book ai didi

c++ - CUDA 编译器对象到可执行文件

转载 作者:行者123 更新时间:2023-11-28 01:34:28 25 4
gpt4 key购买 nike

我正在尝试让 CUDA 应用程序使用命令行进行编译,但我遇到了麻烦。

我知道运行命令

nvcc -ccbin "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin" -c -D_DEBUG -DWIN32 -D_CONSOLE -D_MBCS -Xcompiler /EHsc,/W3,/nologo,/Od,/Zi,/MTd -o test.obj test.cu

从我的批处理文件生成一个 obj 文件,我需要使用链接器来生成可执行文件。我在使用 Visual Studio 时遇到了几乎无法解决的问题,所以我试图避免它,但我不知道如何完成我的程序编译。

我需要知道的是:我应该如何处理我的目标文件才能获得我可以实际运行的可执行文件?

最佳答案

假设您的 test.cu 是一个完整的应用程序(例如,有一个 main 函数等),对命令行的两个更改可能是删除 -c 开关并更改 -o 开关以指定典型的 windows 文件可执行名称,例如 test.exe,即 -o测试.exe.

以下测试用例在 Windows 7、CUDA 8 和 VS 2015 上演示了这一点:

C:\Users\bob\Documents>type test.cu
#include <stdio.h>

__global__ void t(){
printf("hello\n");
}

int main(){

t<<<1,1>>>();
cudaDeviceSynchronize();
printf("%s\n", cudaGetErrorString(cudaGetLastError()));
return 0;
}
C:\Users\bob\Documents>nvcc -ccbin "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin" -D_DEBUG -DWIN32 -D_CONSOLE -D_MBCS -Xc
ompiler /EHsc,/W3,/nologo,/Od,/Zi,/MTd -o test.exe test.cu
nvcc warning : The 'compute_20', 'sm_20', and 'sm_21' architectures are deprecated, and may be removed in a future release (Use -Wno-depreca
ted-gpu-targets to suppress warning).
test.cu
Creating library test.lib and object test.exp

C:\Users\bob\Documents>test
hello
no error

C:\Users\bob\Documents>

请注意,即使更改 -o 开关也不是绝对必要的。此开关仅指定输出文件 name 并且对实际编译流程不做任何事情(在这种情况下由 -c 开关的存在或不存在控制 - - 见 here )。您可以通过保持 -o 开关不变来向自己证明这一点。然后,您将拥有一个名为 test.obj 的可执行文件,您可以通过在命令提示符下键入 test.obj 来向自己证明这一点。它将使用与上面显示的相同的输出执行。

但这可能会造成混淆,并且不是典型的 Windows 文件命名约定。

关于c++ - CUDA 编译器对象到可执行文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49953359/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com