Go编译报错go build: build output “dirmain“ already exists and is a directory

本文介绍在使用Go编译器时遇到的一个常见问题:当尝试编译一个与当前目录同名的包时出现错误。文章提供了两种解决方案:一是通过指定输出文件名避免与目录重名;二是切换到另一个目录进行编译。

比如当前目录是$GOPATH/src/GO1209,当前目录有个文件夹dirmain是一个包,如果指定包的文件夹进行编译,会报错输出文件dirmain在当前目录已经存在并且是个目录:

VMCatalina:src haypin$ tree Go1209
Go1209
├── dirmain
│   ├── go.mod
│   ├── gomain.go
│   ├── gomain2.go
│   └── gomain3.go
VMCatalina:src haypin$
VMCatalina:Go1209 haypin$ go build Go1209/dirmain
go build: build output "dirmain" already exists and is a directory

因为不论在Windows还是在Linux或者在Unix中,目录也是个文件,其中存储着目录中内容(子目录或文件)的名字——文件节点指针的映射,具体参考《鸟哥的Linux私房菜》基础篇讲解文件系统部分,因此同一目录下的子目录和文件不允许重名。

解决办法:

1、通过-o [output]选项指定生成文件名使不与子目录重名:

VMCatalina:Go1209 haypin$ go build -o pkgmain Go1209/dirmain
VMCatalina:Go1209 haypin$ tree ../Go1209
../Go1209
├── dirmain
│   ├── go.mod
│   ├── gomain.go
│   ├── gomain2.go
│   └── gomain3.go
├── pkgmain
VMCatalina:Go1209 haypin$ 

这里还有一个有意思的地方,如果-o [output]选项指定的生成文件名与包文件夹名最后一段同名,则会输出生成文件到包文件夹下,Go还是很聪明的,不会自己犯蠢:

VMCatalina:Go1209 haypin$ go build -o dirmain Go1209/dirmain
VMCatalina:Go1209 haypin$ tree ../Go1209
../Go1209
├── dirmain
│   ├── dirmain
│   ├── go.mod
│   ├── gomain.go
│   ├── gomain2.go
│   └── gomain3.go
VMCatalina:Go1209 haypin$

2、这还不简单,新建一个文件夹然后cd到新建文件夹再编译生成,那生成文件就在pwd下了:

VMCatalina:Go1209 haypin$ mkdir tempDir
VMCatalina:Go1209 haypin$ cd tempDir
VMCatalina:tempDir haypin$ go build Go1209/dirmain
VMCatalina:tempDir haypin$ cd ..
VMCatalina:Go1209 haypin$ tree ../Go1209
../Go1209
├── dirmain
│   ├── dirmain
│   ├── go.mod
│   ├── gomain.go
│   ├── gomain2.go
│   └── gomain3.go
└── tempDir
    └── dirmain
VMCatalina:Go1209 haypin$

 

我没懂你让我干什么 你发了好多文件 我按照你发的路径创建了 命令也执行了 我不知道我哪里做错了 “PowerShell 7 环境已加载 (版本: 7.5.2) PS C:\Users\Administrator\Desktop> cd E:\PyTorch_Build\pytorch PS E:\PyTorch_Build\pytorch> python -m venv rtx5070_env PS E:\PyTorch_Build\pytorch> .\rtx5070_env\Scripts\activate (rtx5070_env) PS E:\PyTorch_Build\pytorch> # 1. 创建兼容层目录 (rtx5070_env) PS E:\PyTorch_Build\pytorch> mkdir -p third_party/cuda_compat New-Item: An item with the specified name E:\PyTorch_Build\pytorch\third_party\cuda_compat already exists. (rtx5070_env) PS E:\PyTorch_Build\pytorch> (rtx5070_env) PS E:\PyTorch_Build\pytorch> # 2. 放置适配器代码 (rtx5070_env) PS E:\PyTorch_Build\pytorch> Set-Content third_party/cuda_compat/cudnn_adapter.cu cmdlet Set-Content at command pipeline position 1 Supply values for the following parameters: Value[0]: import torch Value[1]: from torch.utils.cpp_extension import CUDA_HOME Value[2]: (rtx5070_env) PS E:\PyTorch_Build\pytorch> (rtx5070_env) PS E:\PyTorch_Build\pytorch> # 3. 运行构建脚本 (rtx5070_env) PS E:\PyTorch_Build\pytorch> .\build_custom.ps1 Traceback (most recent call last): File "E:\PyTorch_Build\pytorch\tools\generate_missing_symbols.py", line 23, in <module> generate_symbols("E:/NVIDIA/CUDNN/v9.12/include/cudnn.h") File "E:\PyTorch_Build\pytorch\tools\generate_missing_symbols.py", line 6, in generate_symbols with open(header_path) as f: FileNotFoundError: [Errno 2] No such file or directory: 'E:/NVIDIA/CUDNN/v9.12/include/cudnn.h' error: patch failed: cmake/Dependencies.cmake:1234 error: cmake/Dependencies.cmake: patch does not apply Id Name PSJobTypeName State HasMoreData Location Command -- ---- ------------- ----- ----------- -------- ------- 1 Job1 BackgroundJob Running True localhost python setup.py install … gt: E:\PyTorch_Build\pytorch\build_custom.ps1:26 Line | 26 | python setup.py install --cmake 2>&1 | tee build.log | ~~ | The term 'gt' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the | spelling of the name, or if a path was included, verify that the path is correct and try again. &: E:\PyTorch_Build\pytorch\build_custom.ps1:26 Line | 26 | python setup.py install --cmake 2>&1 | tee build.log | ~~~ | The term 'amp' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the | spelling of the name, or if a path was included, verify that the path is correct and try again. 1 (rtx5070_env) PS E:\PyTorch_Build\pytorch> def verify_compat(): ParserError: Line | 1 | def verify_compat(): | ~ | An expression was expected after '('. (rtx5070_env) PS E:\PyTorch_Build\pytorch> print(f"Reported CUDNN: {torch.backends.cudnn.version()}") fReported CUDNN: {torch.backends.cudnn.version()}: The term 'fReported CUDNN: {torch.backends.cudnn.version()}' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. (rtx5070_env) PS E:\PyTorch_Build\pytorch> print(f"Actual CUDNN: {torch._C._cudnn_get_version()}") fActual CUDNN: {torch._C._cudnn_get_version()}: The term 'fActual CUDNN: {torch._C._cudnn_get_version()}' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. (rtx5070_env) PS E:\PyTorch_Build\pytorch> (rtx5070_env) PS E:\PyTorch_Build\pytorch> # 执行RNN操作验证兼容层 (rtx5070_env) PS E:\PyTorch_Build\pytorch> rnn = torch.nn.RNN(128, 256).cuda() InvalidOperation: Method invocation failed because [System.Int32] does not contain a method named 'cuda'. (rtx5070_env) PS E:\PyTorch_Build\pytorch> input = torch.randn(32, 10, 128).cuda() InvalidOperation: Method invocation failed because [System.Int32] does not contain a method named 'cuda'. (rtx5070_env) PS E:\PyTorch_Build\pytorch> output, _ = rnn(input) ParserError: Line | 1 | output, _ = rnn(input) | ~ | Missing argument in parameter list. (rtx5070_env) PS E:\PyTorch_Build\pytorch> print("RNN output shape:", output.shape) ParserError: Line | 1 | print("RNN output shape:", output.shape) | ~ | Missing expression after ','. (rtx5070_env) PS E:\PyTorch_Build\pytorch>”
最新发布
09-03
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值