在编译golang程序的时候,出现如下错误:
cannot create <nil>/go.o: No such file or directory
make: *** [build/mds] Error 2
错误原因
go build的时候会创建临时文件,猜测与临时目录相关的问题,查看了下源代码
[src/cmd/ldlib.c 652-675] static void hostlinksetup(void) { char *p; if(linkmode != LinkExternal) return; // create temporary directory and arrange cleanup if(tmpdir == nil) { tmpdir = mktempdir(); atexit(rmtemp); } // change our output to temporary object file close(cout); p = smprint("%s/go.o", tmpdir); cout = create(p, 1, 0775); if(cout < 0) { diag("cannot create %s: %r", p); errorexit(); } free(p); }可以看到,是这个tmpdir目录,于是继续查找
[src/lib9/tempdir_unix.c 13-25] char* mktempdir(void) { char *tmp, *p; tmp = getenv("TMPDIR"); if(tmp == nil || strlen(tmp) == 0) tmp = "/var/tmp"; p = smprint("%s/go-link-XXXXXX", tmp); if(mkdtemp(p) == nil) return nil; return p; }于是发现临时目录是/var/tmp,看了下这个目录,发现空间呢居然满了,囧
df -lh /var/tmp/
Filesystem Size Used Avail Use% Mounted on
/dev/sda6 16G 16G 0 100% /var
解决办法:
1. 释放临时目录的部分空间;
2. export TMPDIR=/tmp,设置其他目录为临时目录
有疑问加站长微信联系(非本文作者)