You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MacOS Catalina环境下gfortran编译出现ld: file too small (length=0)错误的技术求助

Fixing ld: file too small (length=0) Error with gfortran on macOS Catalina

Let's break down and fix this frustrating issue step by step. First, some quick context: that error means the linker (ld) is trying to use an empty object file (.o) generated during compilation. The temporary file disappears because your system automatically cleans up failed compilation artifacts, and the T// in the path is just a harmless shell parsing quirk (it's equivalent to T/).

Here are targeted fixes to try, ordered from simplest to more involved:

1. Isolate the Compilation Step (Skip Linking)

The linker error might be hiding a failure in the earlier compilation phase. Let's test just generating the object file first:

  • Run gfortran -c file.f — this skips linking and only creates file.o.
    • If this command fails with an explicit error, that's your root cause (e.g., syntax issues in your Fortran code, or a broken gfortran setup).
    • If it creates an empty file.o, enable verbose logging to see exactly where things go wrong:
      gfortran -v -c file.f
      
      Look for errors from the assembler (as) or other tools in the output — this will point you to the exact step that's failing.

2. Clean Terminal Compilation Caches (No Xcode Required)

You don't need Xcode to do a "clean" — we can manually clear cached files and reset your shell environment:

  • Start a clean shell: Close your terminal and reopen it, or run env -i bash to launch a shell with no custom environment variables (this eliminates issues from misconfigured paths/exports).
  • Delete gfortran-specific caches: If you have a gfortran cache directory, wipe it:
    rm -rf ~/.cache/gfortran
    
  • Clear temporary compilation files: Use your system's temp directory (find it with echo $TMPDIR) to delete leftover cc* files (these are the temporary objects gfortran uses):
    rm -rf $(echo $TMPDIR)/cc*
    

3. Verify Command Line Tools Configuration

Reinstalling CLT doesn't always fix path misconfigurations:

  • Check your current CLT path with:
    xcode-select -p
    
    It should output /Library/Developer/CommandLineTools.
  • If it shows something else (like an old Xcode path), reset it with:
    sudo xcode-select -s /Library/Developer/CommandLineTools
    
  • Confirm CLT is working: Run clang --version — you should get a valid version string without errors.

4. Ensure gfortran and CLT Compatibility

macOS Catalina has stricter compatibility rules, so double-check your gfortran setup:

  • If you installed gfortran via Homebrew/MacPorts, make sure it's built for Catalina. For Homebrew, you might need to use a tap that supports older macOS versions, or try a slightly older gfortran release (like 9.x, since you already tested 10.x and 12.x).
  • Confirm you're using the correct gfortran binary:
    which gfortran
    
    Make sure this path points to the version you installed, not a leftover system or old version.

5. Test with a Minimal Fortran Program

Rule out issues with your source code by compiling a dead-simple program:
Create a file hello.f with:

program hello
  print *, "Hello World"
end program hello

Then compile it:

gfortran -o hello hello.f

If this still throws the same error, your environment is definitely the problem — not your code.


内容的提问来源于stack exchange,提问作者Steve Lord

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 05:19:07