title: Pybind11 and global C variables
body: <p>I don't manage to export a global variable from C to Python using pybind11. The problem can be reproduced from a simple example. Let's say we have a header file (global.h) like this:</p>
  <pre class="lang-cpp prettyprint-override"><code>#ifndef GLOBAL_H
  #define GLOBAL_H
  
  extern int array[];
  
  #endif 
  </code></pre>
  <p>The array is defined in a C file (global.c) like this:</p>
  <pre class="lang-cpp prettyprint-override"><code>#include &quot;global.h&quot;
  
  int array[] = {1, 2, 3, 4};
  </code></pre>
  <p>I want to export this array in a Python module using pybind11 and the following C++ file (pyglobal.cpp):</p>
  <pre class="lang-cpp prettyprint-override"><code>#include &lt;pybind11/pybind11.h&gt;
  
  extern &quot;C&quot;
  {
    #include &quot;global.h&quot;
  }
  
  PYBIND11_MODULE(pyglobal, m)
  {
    m.attr(&quot;array&quot;) = array;
  }
  </code></pre>
  <p>Everything works fine when I generate my library with CMake (CMakeLists.txt):</p>
  <pre class="lang-make prettyprint-override"><code>cmake_minimum_required(VERSION 2.8.12)
  project(pyglobal)
  
  find_package(pybind11 PATHS ${PYBIND11_DIR} REQUIRED)
  
  pybind11_add_module(pyglobal pyglobal.cpp global.c)
  </code></pre>
  <p>But when I start a python3 shell and type</p>
  <pre class="lang-py prettyprint-override"><code>import pyglobal
  </code></pre>
  <p>I get the following error message:</p>
  <pre class="lang-py prettyprint-override"><code>&gt; Python 3.5.2 (default, Nov 23 2017, 16:37:01) 
  [GCC 5.4.0 20160609] on linux
  Type &quot;help&quot;, &quot;copyright&quot;, &quot;credits&quot; or &quot;license&quot; for more information.
  &gt;&gt;&gt; import pyglobal
  Traceback (most recent call last):
    File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt;
  ImportError: AttributeError: array
  </code></pre>
  <p>What am I doing wrong here?</p>