MacOS下CLion运行MKL+Eigen3可执行文件时库加载失败
This is a super common macOS issue—CLion doesn’t inherit the same environment variables your terminal uses, which is why the executable runs fine in bash but fails in the IDE. Here are three solid fixes tailored to different workflows:
1. Manually add MKL’s library path to CLion’s run environment
CLion’s runtime session doesn’t automatically pick up the MKL setup scripts you source in your terminal. Here’s how to fix that per-run configuration:
- Go to Run > Edit Configurations
- Select your executable’s run configuration from the list
- Click the pencil icon next to Environment variables to edit them
- Add a new variable:
- Name:
DYLD_LIBRARY_PATH - Value: Your MKL library directory (e.g.,
/opt/intel/oneapi/mkl/latest/lib—adjust this to match your actual installation path)
- Name:
- Save the config and try running again.
2. Hardcode the rpath in your CMakeLists.txt
This is a permanent fix that embeds the library path directly into your executable, so you won’t rely on environment variables anymore:
First, ensure your CMake config correctly finds MKL and Eigen:
find_package(MKL REQUIRED) find_package(Eigen3 REQUIRED)
Then add these lines after defining your executable:
add_executable(my_exe your_source_files.cpp) # Link Eigen and MKL to your executable target_link_libraries(my_exe Eigen3::Eigen MKL::MKL) # Embed MKL's library path into the executable's rpath set_target_properties(my_exe PROPERTIES BUILD_WITH_INSTALL_RPATH ON INSTALL_RPATH "${MKL_LIBRARIES}" )
Alternatively, you can explicitly set the rpath via link options:
target_link_options(my_exe PRIVATE -Wl,-rpath,/opt/intel/oneapi/mkl/latest/lib)
Re-run CMake, rebuild your project, and CLion should find the library without issues.
3. Launch CLion from your terminal to inherit environment variables
If you use MKL regularly and want to skip manual configs, start CLion from your terminal after loading MKL’s environment:
- In bash/zsh, source the MKL setup script (adjust path to your installation):
source /opt/intel/oneapi/setvars.sh - Launch CLion directly from the terminal:
open -a CLion
This makes CLion inherit all the environment variables your terminal has, including those needed to locate MKL libraries.
The core issue here is that your terminal session loads MKL’s environment variables on startup, but CLion runs in a separate session that doesn’t have those variables set. Any of these fixes should resolve the "image not found" error.
内容的提问来源于stack exchange,提问作者John Doe

