Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Static linking against libstdc++ in Binary Build mode #2113

Closed
wants to merge 6 commits into from

Conversation

soumith
Copy link
Member

@soumith soumith commented Jul 15, 2017

Copy link
Member

@colesbury colesbury left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this work? Specifically the change to setup.py to add libstdc++.a to the extra_link_args.

I was reading:
https://stackoverflow.com/questions/13636513/linking-libstdc-statically-any-gotchas

It sounds like this missing symbols might have to do with dlopen or other C++ libraries we link dynamically against. I'm a little surprised that adding the libstdc++.a to extra_link_args would fix this, but if it works we should do it.

We could also bundle libstdc++.so and add it to the RPATH like we do for other libraries.

setup.py Outdated
# get path of libstdc++ and link manually.
# for reasons unknown, -static-libstdc++ doesn't fully link some symbols
CXXNAME = 'g++'
if os.getenv('CXX'):

This comment was marked as off-topic.

setup.py Outdated
CXXNAME = 'g++'
if os.getenv('CXX'):
CXXNAME = os.getenv('CXX')
path = subprocess.check_output([CXXNAME, '-print-file-name=libstdc++.a'])

This comment was marked as off-topic.

@soumith
Copy link
Member Author

soumith commented Jul 16, 2017

shipping libstdc++.so is terrible terrible advice, I dont know why the author of that stackoverflow post even suggested it. Maybe the author was talking in the context of independent executables that dont interoperate with anything else (in this context it slightly makes sense).

It sounds like this missing symbols might have to do with dlopen or other C++ libraries we link dynamically against.

No, this was a multi-day investigation and I still haven't concluded the exact cause, but this is the commit that introduced the regression where static-libstdc++ stopped working: 702a2e3

By stopped working, I mean: compiled with linker flag -static-libstdc++, but at runtime on another machine with an older / different libstdc++, the program was failing to load with unresolved symbols like _ZTISt12bad_weak_ptr (and nm -u _C.so confirms the same).

The commit adds usage of weak_ptr, but somehow static-libstdc++ is not linking destructor and other minor symbols of this class (and a similar story around other particular std:: symbols).
Here's a debug output for example:

nm -u /opt/python/cp27-cp27m/lib/python2.7/site-packages/torch/_C.so | grep bad_weak
                 U _ZNSt12bad_weak_ptrD1Ev
                 U _ZTISt12bad_weak_ptr
                 U _ZTVSt12bad_weak_ptr

From reading a bunch of stuff online, it seems like the only problematic issues with missing symbols might have to do with dlopen come when loading dynamic libraries that themselves might have c++ symbols not resolved (these dependent dylibs are not statically linked against libstdc++), this doesn't apply to us, as each of the dylibs we care about, we do link against libstdc++.

@apaszke
Copy link
Contributor

apaszke commented Jul 16, 2017

Don't we also need to link with libgcc? It's still linked dynamically, so if someone has an incompatible one it could interact with our static symbols in some weird ways

@colesbury
Copy link
Member

@apaszke, do you mean glibc? The manylinux1 strategy is to dynamically link against a sufficiently old version of glibc (specifically from CentOS 5.11). Newer versions of glibc are backwards compatible, so they're OK.

See https://www.python.org/dev/peps/pep-0513/#versioning-of-core-shared-libraries for why they chose not to statically link glibc.

@apaszke
Copy link
Contributor

apaszke commented Jul 17, 2017

No I meant libgcc. There's even a -static-libgcc flag

@soumith
Copy link
Member Author

soumith commented Jul 17, 2017

i've checked on the libgcc thing empirically. We dont need it for now atleast.

@soumith
Copy link
Member Author

soumith commented Jul 19, 2017

merged into master

@soumith soumith closed this Jul 19, 2017
@soumith soumith deleted the binaryfix branch July 20, 2017 17:31
zou3519 pushed a commit to zou3519/pytorch that referenced this pull request Mar 30, 2018
IvanYashchuk pushed a commit to IvanYashchuk/pytorch that referenced this pull request Oct 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants