MacOS Catalina环境下gfortran编译出现ld: file too small (length=0)错误的技术求助
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 createsfile.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:
Look for errors from the assembler (gfortran -v -c file.fas) 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 bashto 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 leftovercc*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:
It should outputxcode-select -p/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:
Make sure this path points to the version you installed, not a leftover system or old version.which gfortran
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

