How can i link my c++ program statically with libstdc++ on osx using clang?
Asked Answered
L

1

20

i'm writting c++ program and i want to distribute it on Mac OS X versions 10.6 and above. The problem is when i compile the program with clang and it's dynamically linked with libstdc++ and it causes problems with older systems.

There is a key -static-stdc++ in gcc but there's no one in clang. How can i link my program statically with clang?

My main goal is to compile binary on Mac OS X 10.9 and be able to run it on earlier versions. Maybe there's a different way?

Thank you.

Leticialetisha answered 5/5, 2014 at 12:3 Comment(0)
L
21

Under Linux, this command works:

clang --std=c++11 -stdlib=libstdc++ loopy.cpp -o loopy -static -lstdc++

Where loopy is of course the name of my program.

Update:

It appears that Apple strongly discourages static linking so it suggests another approach is needed.

This answer on creating backwards compatible OS X software may be of use to you instead.

Larocca answered 5/5, 2014 at 12:19 Comment(6)
this doesn't work on Mac OSX: ld: library not found for -lcrt0.oLeticialetisha
You'll need static versions of all libraries including libcrt0.a. See this answer for more details on that.Larocca
It should be possible to statically link libc++ without statically linking libc. The reasoning for discouraging static linking does not apply to libc++; only to libSystem.Gerardgerardo
@Gerardgerardo but how to link with libc++ statically?Indeterminacy
@Gerardgerardo still wanting to know how to link with libc++ staticallyIndeterminacy
You can use zig c++ to statically link libc++Gerardgerardo

© 2022 - 2024 — McMap. All rights reserved.