How to specify GLIBCXX_USE_CXX11_ABI=1 when building Pytorch from source?

username: Shisho_Sama
cooked: <p>Hello everyone,<br>
I want to build Pytorch with <code>GLIBCXX_USE_CXX11_ABI=1</code> how should I go about setting this?<br>
Im following the official guide which only says :</p>
<pre><code class="lang-python">export CMAKE_PREFIX_PATH=${CONDA_PREFIX:-"$(dirname $(which conda))/../"}
python setup.py install
</code></pre>
<p>I couldn’t find anything regarding this. its not even indicated in the <code>setup.py</code><br>
Thanks a lot in advance</p>

username: Shisho_Sama
cooked: <p>I also found <a href="https://discuss.pytorch.org/t/issues-linking-with-libtorch-c-11-abi/29510/4">this</a> which says where we can change this in cmake  :</p>
<pre><code class="lang-auto">if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
  set(TORCH_CXX_FLAGS "-D_GLIBCXX_USE_CXX11_ABI=0")
endif()
</code></pre>
<p>for me though I had to change the <code>/cmake/TorchConfig.cmake.in</code> which is :</p>
<pre><code class="lang-auto"># When we build libtorch with the old GCC ABI, dependent libraries must too.
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
  set(TORCH_CXX_FLAGS "-D_GLIBCXX_USE_CXX11_ABI=@GLIBCXX_USE_CXX11_ABI@")
endif()
</code></pre>
<p>but is there another way to set the variable instead of hardcoding  this into cmake file?</p>
<p><strong>Update:</strong></p>
<p>Will doing something like  this suffice? <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=12" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"></p>
<pre><code class="lang-auto">export CMAKE_PREFIX_PATH=${CONDA_PREFIX:-"$(dirname $(which conda))/../"}
export TORCH_CXX_FLAGS="-D_GLIBCXX_USE_CXX11_ABI=1"
python setup.py install
</code></pre>
<p>OK I did this and the build process finished successfully <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=12" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"></p>
<p><strong>Update 2:</strong><br>
However, upon importing the torch now I get <img src="https://discuss.pytorch.org/images/emoji/apple/face_with_raised_eyebrow.png?v=12" title=":face_with_raised_eyebrow:" class="emoji" alt=":face_with_raised_eyebrow:" loading="lazy" width="20" height="20"></p>
<pre><code class="lang-auto">In [1]: import torch                                                            
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
&lt;ipython-input-1-eb42ca6e4af3&gt; in &lt;module&gt;
----&gt; 1 import torch

~/Desktop/Pytorch1_6/pytorch-1.6.0/torch/__init__.py in &lt;module&gt;
    333 
    334 # If you edit these imports, please update torch/__init__.py.in as well
--&gt; 335 from .random import set_rng_state, get_rng_state, manual_seed, initial_seed, seed
    336 from .serialization import save, load
    337 from ._tensor_str import set_printoptions

~/Desktop/Pytorch1_6/pytorch-1.6.0/torch/random.py in &lt;module&gt;
      2 import warnings
      3 
----&gt; 4 from torch._C import default_generator
      5 
      6 

ImportError: cannot import name 'default_generator' from 'torch._C' (unknown location)

</code></pre>
<p>Whats wrong?<br>
OK found the cause :</p>
<blockquote>
<p>If you try to import torch while inside the top level pytorch source tree it will import the torch directory and not the installed torch module. Try changing your current working directory and see if the issue resolves.</p>
</blockquote>
<p>With this now fixed, mission is now accomplished <img src="https://discuss.pytorch.org/images/emoji/apple/sweat_smile.png?v=12" title=":sweat_smile:" class="emoji" alt=":sweat_smile:" loading="lazy" width="20" height="20"></p>
<pre data-code-wrap="python"><code class="lang-python">In [1]: import torch                                                            

In [2]: torch._C._GLIBCXX_USE_CXX11_ABI                                         
Out[2]: True

</code></pre>
<p>final note, to create the whl file, simply repalce python <code>setup.py</code> with <code>pip wheel .</code> or simply do:</p>
<pre data-code-wrap="python"><code class="lang-python">export CMAKE_PREFIX_PATH=${CONDA_PREFIX:-"$(dirname $(which conda))/../"}
export TORCH_CXX_FLAGS="-D_GLIBCXX_USE_CXX11_ABI=1"
pip wheel .
</code></pre>
<p><img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=12" title=":slight_smile:" class="emoji only-emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"></p>