为什么添加链接器标志`-save-temps`可以解决clang-8错误:“无法创建临时文件”?

qltillow  于 2023-04-19  发布在  其他
关注(0)|答案(1)|浏览(149)

我正在使用Windows + Cygwin + Eclipse + LLVM工具链构建一个C/C++项目。
这些文件可以用clang C编译器编译,但是用clang++链接多个error: unable to make temporary file: No such file or directory会失败。

clang++ -o test "source1.bc" "source2.bc" "source3.bc" 
clang-8: error: unable to make temporary file: No such file or directory
clang-8: error: unable to make temporary file: No such file or directory
clang-8: error: unable to make temporary file: No such file or directory

链接器为每个对象文件生成一个错误。
当我将-save-temps选项添加到链接器时,它工作正常!
有什么问题吗?
添加--verbose得到:

clang version 8.0.1 (tags/RELEASE_801/final) Target: x86_64-unknown-windows-cygnus
Thread model: posix
InstalledDir: /usr/bin
clang-8: error: unable to make temporary file: No such file or directory
clang-8: error: unable to make temporary file: No such file or directory
clang-8: error: unable to make temporary file: No such file or directory

我已经检查了TMPTEMP变量-一切看起来都很好。我试着改变TMPTEMP以包括尾随斜杠/,我还试着用反斜杠\或正斜杠/来修改整个路径-没有任何帮助。
下面是一个更详细的输出。
首先,下面是带有标志-save-temps-###的输出:

"/usr/bin/clang-8" "-cc1" "-triple" "x86_64-unknown-windows-cygnus" "-S" "-save-temps=cwd" "-disable-free" "-disable-llvm-verifier" "-discard-value-names" "-main-file-name" "my_files\\my_file1.bc" "-mrelocation-model" "pic" "-pic-level" "2" "-mthread-model" "posix" "-fmath-errno" "-masm-verbose" "-mconstructor-aliases" "-munwind-tables" "-target-cpu" "x86-64" "-dwarf-column-info" "-debugger-tuning=gdb" "-momit-leaf-frame-pointer" "-v" "-resource-dir" "/usr/lib/clang/8.0.1" "-fdebug-compilation-dir" "/cygdrive/c/my_projects/prj1/Debug" "-ferror-limit" "19" "-fmessage-length" "0" "-fobjc-runtime=gcc" "-fseh-exceptions" "-fdiagnostics-show-option" "-o" "my_files\\my_file1.s" "-x" "ir" "my_files\\my_file1.bc" "-faddrsig"

下面是没有-save-temps的输出,只有-###

"/usr/bin/clang-8" "-cc1" "-triple" "x86_64-unknown-windows-cygnus" "-emit-obj" "-mrelax-all" "-disable-free" "-disable-llvm-verifier" "-discard-value-names" "-main-file-name" "my_files\\my_file1.bc" "-mrelocation-model" "pic" "-pic-level" "2" "-mthread-model" "posix" "-fmath-errno" "-masm-verbose" "-mconstructor-aliases" "-munwind-tables" "-target-cpu" "x86-64" "-dwarf-column-info" "-debugger-tuning=gdb" "-momit-leaf-frame-pointer" "-v" "-resource-dir" "/usr/lib/clang/8.0.1" "-fdebug-compilation-dir" "/cygdrive/c/my_projects/prj1/Debug" "-ferror-limit" "19" "-fmessage-length" "0" "-fobjc-runtime=gcc" "-fseh-exceptions" "-fdiagnostics-show-option" "-o" "" "-x" "ir" "my_files\\my_file1.bc" "-faddrsig"

所以"-o" "my_files\\my_file1.s""-o" ""似乎是不同的。有什么解释吗?
此外,还有一些带有标志-save-temps-###的输出:

"/usr/bin/ld" "-m" "i386pep" "--wrap" "_Znwm" "--wrap" "_Znam" "--wrap" "_ZnwmRKSt9nothrow_t" "--wrap" "_ZnamRKSt9nothrow_t" "--wrap" "_ZdlPv" "--wrap" "_ZdaPv" "--wrap" "_ZdlPvRKSt9nothrow_t" "--wrap" "_ZdaPvKSt9nothrow_t" "-Bdynamic" "--tsaware" "-o" "myexecutable" "/usr/lib/crt0.o" "/usr/lib/gcc/x86_64-pc-cygwin/10/crtbegin.o" "-LC:/mylibraries" "-L/usr/lib/gcc/x86_64-pc-cygwin/10" "-L/usr/x86_64-pc-cygwin/lib" "-L/usr/lib" "-L/usr/lib/w32api" "my_files\\my_file1.o" "my_files\\my_file2.o" " my_files\\my_file3.o" "-lnaturedsp" "-lgcc_s" "-gcc" "-lcygwin" "-ladvapi32" "-lshell32" "-luser32" "-lkernel32" "/usr/lib/default-manifest.o" "/usr/lib/gcc/x86_64-pc-cygwin/10/crtend.o"

-###

"/usr/bin/ld" "-m" "i386pep" "--wrap" "_Znwm" "--wrap" "_Znam" "--wrap" "_ZnwmRKSt9nothrow_t" "--wrap" "_ZnamRKSt9nothrow_t" "--wrap" "_ZdlPv" "--wrap" "_ZdaPv" "--wrap" "_ZdlPvRKSt9nothrow_t" "--wrap" "_ZdaPvKSt9nothrow_t" "-Bdynamic" "--tsaware" "-o" "myexecutable" "/usr/lib/crt0.o" "/usr/lib/gcc/x86_64-pc-cygwin/10/crtbegin.o" "-LC:/mylibraries" "-L/usr/lib/gcc/x86_64-pc-cygwin/10" "-L/usr/x86_64-pc-cygwin/lib" "-L/usr/lib" "-L/usr/lib/w32api" "" "" "" "-lnaturedsp" "-lgcc_s" "-lgcc" "-lcygwin" "-ladvapi32" "-lshell32" "-luser32" "-lkernel32" "/usr/lib/default-manifest.o" "/usr/lib/gcc/x86_64-pc-cygwin/10/crtend.o"

因此,"my_files\\my_file1.o" "my_files\\my_file2.o" "my_files\\my_file3.o"再次丢失并被空字符串"" "" ""替换。
还有一件事,正如clang邮件列表中指出的:“驱动程序选项-save-temps和链接器选项--save-temps(驱动程序选项-Wl、--save-temps)都不遵守TMP/TEMP/TMPDIR。临时文件相对于当前工作目录保存...”
所以,这不是关于临时目录位置。

guicsvcw

guicsvcw1#

这个错误的一个潜在原因是tmp环境变量被分配给了一个不存在的路径。(可能还有其他原因。)当-save-temps参数被传递给clang++.exe时,错误不会被触发,因为临时文件被写入了当前的工作目录,并且使用tmp的分支没有被执行。
接下来,您可能会遇到错误unable to make temporary file: invalid argument。一个常见的原因是分配给tmp的路径周围有引号。例如,您可能在批处理文件中编写了以下内容:

REM  This is the quoted, fully-qualified path to this script.
set script_directory=%~dp0:~0,-1%
set script_directory="%script_directory%"

REM  clang++.exe requires a writable temporary directory.
set tmp=%script_directory%

%script_directory%\bin\clang++.exe ^
    my_program.exe ^
   -Xclang -main-file-name %script_directory%\src\main.cpp

在那个批处理文件中,脚本目录用引号括起来,这样当从包含空格的路径运行时就不会失败。那些引号会导致错误。在为tmp赋值时不要包含它们。

相关问题