GNAT GPS中无法编译运行简单Ada应用求助
ld: library not found for -lSystem错误 Hey there! Let's break down what's going on here and fix that linker error you're hitting. First, let's recap your setup details:
你的代码(main.adb)
with Ada.Text_IO; procedure Main is begin Ada.text_IO.Put_Line("Hello world!"); null; end Main;
收到的构建错误
gprbuild -d -P/Users/myname/Desktop/Test/default.gpr /Users/myname/Desktop/Test/src/main.adb Compile [Ada] main.adb Bind [gprbind] main.bexch [Ada] main.ali Link [link] main.adb ld: library not found for -lSystem collect2: error: ld returned 1 exit status gprbuild: link of main.adb failed gprbuild: failed command was: /users/myname/opt/gnat/2019/bin/gcc main.o b__main.o -L/Users/myname/Desktop/Test/obj/ -L/Users/myname/Desktop/Test/obj/ -L/users/myname/opt/gnat/2019/lib/gcc/x86_64-apple-darwin17.7.0/8.3.1/adalib/ /users/myname/opt/gnat/2019/lib/gcc/x86_64-apple-darwin17.7.0/8.3.1/adalib/libgnat.a -Wl,-rpath,@executable_path/ -Wl,-rpath,@executable_path/../../..//opt/gnat/2019/lib/gcc/x86_64-apple-darwin17.7.0/8.3.1/adalib -o main [2019-10-19 11:34:54] process exited with status 4, elapsed time: 01.95s
问题原因&解决步骤
First off, your source file being on the desktop isn't the issue—this error happens at the linking stage, not when compiling your Ada code. It's a common compatibility problem between older GNAT versions (like 2019) and newer macOS releases, where the linker can't locate the system library paths properly.
Here's how to fix it step by step:
Make sure Xcode Command Line Tools are installed
ThelibSystem.dylibfile is part of Apple's developer tools. Open Terminal and run this command to install (or confirm it's already set up):xcode-select --installFollow the on-screen prompts to complete the installation if needed.
Specify the system SDK path for gprbuild
Older GNAT versions need explicit directions to find the macOS system libraries. You can set this up in GNAT GPS:- Open your project in GPS
- Navigate to
Project > Project Properties > Build > Switches > Ada - Add this line to the linker flags field:
(Note: If you're on a newer macOS version, the SDK path might have a version number like-L/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/libMacOSX13.sdk—check what's in that directory first to use the correct path.)
You can also test this directly in Terminal to rule out GPS configuration issues:
gnatmake main.adb -largs -L/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/libUpgrade to a newer GNAT version (highly recommended)
GNAT 2019 is quite outdated and doesn't play well with modern macOS releases (like Ventura, Sonoma, etc.). The latest GNAT Community versions have built-in fixes for these system compatibility issues. Grab the latest macOS release from AdaCore's official resources.Verify your GNAT installation
After making changes, try compiling your Hello World again in GPS. If it still fails, rungnat --versionin Terminal to confirm your GNAT installation is properly configured and added to your system PATH.
内容的提问来源于stack exchange,提问作者user12243624

