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

MacOS下GDAL C++编译执行报错:动态库加载不兼容

Fixing "dyld: Library not loaded: @rpath/libgdal.20.dylib" on macOS

Hey there, let's work through this GDAL dynamic linking issue you're hitting. That error typically pops up because your compiled program is trying to load a GDAL library that doesn't match the version you built against, or the macOS dynamic linker can't find the correct Anaconda-based GDAL library at runtime. Here's how to fix it:

Step 1: Verify your GDAL environment is consistent

First, make sure you're using the same GDAL version for compilation and runtime:

  • Check the version of your Anaconda GDAL:
    gdal-config --version
    
  • Double-check that your shell prioritizes the Anaconda GDAL over any system-wide installation:
    which gdalinfo
    
    This should output /anaconda2/bin/gdalinfo—if it shows a different path (like /usr/bin/gdalinfo), you've got a conflicting system GDAL installation interfering with your setup.

Step 2: Update your compilation command to include runtime path (rpath)

The key fix here is telling the linker to embed the Anaconda library path directly into your executable, so it knows exactly where to find the right GDAL library at runtime. Modify your g++ command to add the -Wl,-rpath,/anaconda2/lib flag:

g++ myprogram.cpp -I/anaconda2/include -L/anaconda2/lib -Wl,-rpath,/anaconda2/lib -lgdal -o myprogram

Note: Parameter order matters! -lgdal should come after your source file, and the -rpath flag needs to be passed to the linker via -Wl,.

Step 3: Verify the executable's library dependencies

After compiling, check that your program points to the correct Anaconda GDAL library:

otool -L ./myprogram

Look for a line like /anaconda2/lib/libgdal.20.dylib (not a system path or a generic @rpath entry without the resolved path). If it's still pointing to the wrong location, you can manually fix it with install_name_tool:

install_name_tool -change @rpath/libgdal.20.dylib /anaconda2/lib/libgdal.20.dylib ./myprogram

Step 4: Ensure your Anaconda environment is active

If you installed GDAL in a specific Anaconda environment, activate it before compiling and running your program:

source activate root  # For Anaconda2's default root environment

This ensures your shell uses the Anaconda versions of g++, gdal-config, and the GDAL libraries consistently.

Quick Note on Avoiding Future Conflicts

Try not to mix system-wide GDAL installations with Anaconda's—they often have different versions that can cause this exact incompatibility error. Stick to using the Anaconda-provided tools and libraries when working on this project.

内容的提问来源于stack exchange,提问作者Duccio Piovani

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.22 09:24:36