Skip to content

OpenMP dylib not found on MacOS #128870

Open
@vincentloechner

Description

@vincentloechner

Hi,

Compiling LLVM with the OpenMP project activated, it builds fine and everything seems to be installed ok, including the libomp.dylib.

But, when compiling an OpenMP C program using the newly built clang, the dylib is not found at runtime as I would expect it to be.

Without activating OpenMP everything is fine:

% /opt/llvm/bin/clang toto.c
% otool -L a.out
a.out:
	/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1351.0.0)
% ./a.out
1 threads

With OpenMP it fails:

% /opt/llvm/bin/clang toto.c -fopenmp
% otool -L a.out
a.out:
	@rpath/libomp.dylib (compatibility version 5.0.0, current version 5.0.0)
	/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1351.0.0)
% ./a.out
dyld[73817]: Library not loaded: @rpath/libomp.dylib
  Referenced from: <05684888-4242-32CB-9806-AE5BAF37FD0C> ./a.out
  Reason: no LC_RPATH's found
zsh: abort      ./a.out

Unless you add the path to libomp.dylib manually:

% DYLD_LIBRARY_PATH=/opt/llvm/lib ./a.out
14 threads
% 
% /opt/llvm/bin/clang toto.c -fopenmp -Wl,-rpath,/opt/llvm/lib
% otool -L a.out
a.out:
	@rpath/libomp.dylib (compatibility version 5.0.0, current version 5.0.0)
	/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1351.0.0)
% ./a.out
14 threads
% 

Using the DYLD_LIBRARY_PATH environment variable or extra flags CFLAGS/CXXFLAGS is not satisfactory, and it becomes (very) complicated when using this toolchain in another Cmake-enabled project.

This simple patch fixes the issue:

% git diff
diff --git a/clang/lib/Driver/ToolChains/CommonArgs.cpp b/clang/lib/Driver/ToolChains/CommonArgs.cpp
index 2b916f000336..9e2a8cf798ec 100644
--- a/clang/lib/Driver/ToolChains/CommonArgs.cpp
+++ b/clang/lib/Driver/ToolChains/CommonArgs.cpp
@@ -1104,6 +1104,10 @@ void tools::addOpenMPRuntimeLibraryPath(const ToolChain &TC,
       llvm::sys::path::parent_path(TC.getDriver().Dir);
   llvm::sys::path::append(DefaultLibPath, CLANG_INSTALL_LIBDIR_BASENAME);
   CmdArgs.push_back(Args.MakeArgString("-L" + DefaultLibPath));
+  #if defined(__APPLE__)
+    CmdArgs.push_back(Args.MakeArgString("-rpath"));
+    CmdArgs.push_back(Args.MakeArgString(DefaultLibPath));
+  #endif
 }

 void tools::addArchSpecificRPath(const ToolChain &TC, const ArgList &Args,

I ran this on an Apple M4Pro, MacOS Sequoia 15.3.1.
LLVM 18.1.8 from the git.
This is my cmake configuration (tried various things, the 2 last lines do not seem to change anything):

% cmake -G Ninja ../llvm \
  -DLLVM_ENABLE_PROJECTS="clang;openmp" \
  -DLLVM_BUILD_EXAMPLES=OFF \
  -DLLVM_TARGETS_TO_BUILD="host" \
  -DCMAKE_BUILD_TYPE=Release \
  -DLLVM_ENABLE_ASSERTIONS=ON \
  -DLLVM_ENABLE_LLD=ON \
  -DDEFAULT_SYSROOT="$(xcrun --show-sdk-path)" \
  -DCMAKE_INSTALL_PREFIX=/opt/llvm \
  -DLLVM_INSTALL_UTILS=ON \
  -DCMAKE_INSTALL_RPATH_USE_LINK_PATH=ON \
  -DCMAKE_INSTALL_RPATH=/opt/llvm/lib \
  -DCMAKE_MACOSX_RPATH=TRUE

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions