llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
28.69k stars 11.87k forks source link

-static-libstdc++ unused on macOS #76945

Open h-2 opened 9 months ago

h-2 commented 9 months ago

We are building mac binaries with llvm17 installed from homebrew. The built binaries depend on /opt/homebrew/opt/llvm/lib/c++/libc++.1.dylib. We would like the binaries to not depend on the presence of homebrew, therefore we added -static-libstdc++ to the build, but this is ignored:

clang++: warning: argument unused during compilation: '-static-libstdc++' [-Wunused-command-line-argument]

On Linux this works. What are we doing wrong?

DimitryAndric commented 9 months ago

You are linking libc++, so the option only applies to the GNU specific libstdc++ (which is default on Linux). Unfortunately there seems to be no equivalent -static-libc++ option. You may have to play games with -Wl,-Bstatic, though I am unsure if that will work as expected on macOS. (In general, static linking is discouraged on macOS, it seems.)

h-2 commented 9 months ago

You are linking libc++, so the option only applies to the GNU specific libstdc++ (which is default on Linux).

On Linux, -static-libstdc++ -stdlib=libc++ statically links libc++. So at least on Linux, the option refers to whatever c++ standard library is used.

(In general, static linking is discouraged on macOS, it seems.)

Yeah, we don't want to statically link system stuff, but the libc++ is effectively an application dependency when used from homebrew.

llvmbot commented 9 months ago

@llvm/issue-subscribers-clang-driver

Author: Hannes Hauswedell (h-2)

We are building mac binaries with llvm17 installed from homebrew. The built binaries depend on `/opt/homebrew/opt/llvm/lib/c++/libc++.1.dylib`. We would like the binaries to not depend on the presence of homebrew, therefore we added `-static-libstdc++` to the build, but this is ignored: ``` clang++: warning: argument unused during compilation: '-static-libstdc++' [-Wunused-command-line-argument] ``` On Linux this works. What are we doing wrong?
poyaoc97 commented 7 months ago

It's orthogonal to the chosen standard library (irrelevant to either libc++ or libstdc++).

The Darwin toolchain doesn't contain the logic found in the GNU counterpart, probably due to the lack of -Bstatic counterpart in ld64:

https://github.com/llvm/llvm-project/blob/5143a1241362616840af826d18c067025dae1111/clang/lib/Driver/ToolChains/Gnu.cpp#L557-L563

Hardcoding it in the linker arguments is likely the way to go for your use case (no -stdlib=libc++ and no change to compiler options):

-nostdlib++ -Wl,/opt/homebrew/opt/llvm/lib/c++/libc++.a,/opt/homebrew/opt/llvm/lib/c++/libc++abi.a

Alternatively, you can download and build libc++ as a dependency like so: https://github.com/poyaoc97/cxxlab/blob/main/cmake/FetchLibcxx.cmake.