recently I created code in c++ that I would like to use in Python, so I opted for PyBind11 as it seemed to be straight forward. As I never worked with this tool, I first wanted to understand and try out the basic example given in the documentation:
https://pybind11.readthedocs.io/en/latest/basics.html
// file test.cpp
#include <pybind11/pybind11.h>
int add(int i, int j) {
return i + j;
}
PYBIND11_MODULE(example, m) {
m.doc() = "pybind11 example plugin"; // optional module docstring
m.def("add", &add, "A function that adds two numbers");
}
I am working on a Windows 10 with Anaconda and I have cygwin and Visual Studio 2022 installed. I created a env and installed all required packages with pip.
First, I wanted to use the gcc compiler (cygwin) and found this in order to set the compiler flags: How can I build manually C++ extension with mingw-w64, Python and pybind11?
I tried to compile the code with the following command:
c++ -shared -std=c++23 -fPIC -IC:\Users\blindschleiche\Anaconda3\envs\feb2023\Include -IC:\Users\blindschleiche\Anaconda3\envs\feb2023\lib\site-packages\pybind11\include -Wall -LC:\Users\blindschleiche\anaconda3\Lib test.cpp -LC:\Users\blindschleiche\Anaconda3\pkgs\python-3.8.16-h6244533_2\libs -o test.pyd -lPython38
But I get a bunch of errors stemming from Python library files. Of course, I never changed these files on my own. And some of the errors seem to be "incorrect". Here is the full error message:
In file included from C:\Users\blindschleiche\Anaconda3\envs\feb2023\Include/Python.h:156,
from C:\Users\blindschleiche\Anaconda3\envs\feb2023\lib\site-packages\pybind11\include/pybind11/detail/../detail/common.h:266,
from C:\Users\blindschleiche\Anaconda3\envs\feb2023\lib\site-packages\pybind11\include/pybind11/detail/../attr.h:13,
from C:\Users\blindschleiche\Anaconda3\envs\feb2023\lib\site-packages\pybind11\include/pybind11/detail/class.h:12,
from C:\Users\blindschleiche\Anaconda3\envs\feb2023\lib\site-packages\pybind11\include/pybind11/pybind11.h:13,
from test.cpp:1:
C:\Users\blindschleiche\Anaconda3\envs\feb2023\Include/fileutils.h:79:5: error: '__int64' does not name a type; did you mean '__int64_t'?
79 | __int64 st_size;
| ^~~~~~~
| __int64_t
In file included from /usr/include/sys/stat.h:22,
from C:\Users\blindschleiche\Anaconda3\envs\feb2023\Include/pyport.h:245,
from C:\Users\blindschleiche\Anaconda3\envs\feb2023\Include/Python.h:63,
from C:\Users\blindschleiche\Anaconda3\envs\feb2023\lib\site-packages\pybind11\include/pybind11/detail/../detail/common.h:266,
from C:\Users\blindschleiche\Anaconda3\envs\feb2023\lib\site-packages\pybind11\include/pybind11/detail/../attr.h:13,
from C:\Users\blindschleiche\Anaconda3\envs\feb2023\lib\site-packages\pybind11\include/pybind11/detail/class.h:12,
from C:\Users\blindschleiche\Anaconda3\envs\feb2023\lib\site-packages\pybind11\include/pybind11/pybind11.h:13,
from test.cpp:1:
C:\Users\blindschleiche\Anaconda3\envs\feb2023\Include/fileutils.h:80:12: error: expected ';' at end of member declaration
80 | time_t st_atime;
| ^~~~~~~~
C:\Users\blindschleiche\Anaconda3\envs\feb2023\Include/fileutils.h:80:12: error: expected unqualified-id before '.' token
80 | time_t st_atime;
| ^~~~~~~~
C:\Users\blindschleiche\Anaconda3\envs\feb2023\Include/fileutils.h:82:12: error: expected ';' at end of member declaration
82 | time_t st_mtime;
| ^~~~~~~~
C:\Users\blindschleiche\Anaconda3\envs\feb2023\Include/fileutils.h:82:12: error: expected unqualified-id before '.' token
82 | time_t st_mtime;
| ^~~~~~~~
C:\Users\blindschleiche\Anaconda3\envs\feb2023\Include/fileutils.h:84:12: error: expected ';' at end of member declaration
84 | time_t st_ctime;
| ^~~~~~~~
C:\Users\blindschleiche\Anaconda3\envs\feb2023\Include/fileutils.h:84:12: error: expected unqualified-id before '.' token
84 | time_t st_ctime;
| ^~~~~~~~
In file included from test.cpp:1:
C:\Users\blindschleiche\Anaconda3\envs\feb2023\lib\site-packages\pybind11\include/pybind11/pybind11.h: In member function 'char* pybind11::cpp_function::strdup_guard::operator()(const char*)':
C:\Users\blindschleiche\Anaconda3\envs\feb2023\lib\site-packages\pybind11\include/pybind11/pybind11.h:76:36: error: 'strdup' was not declared in this scope; did you mean 'strcmp'?
76 | # define PYBIND11_COMPAT_STRDUP strdup
| ^~~~~~
C:\Users\blindschleiche\Anaconda3\envs\feb2023\lib\site-packages\pybind11\include/pybind11/pybind11.h:324:23: note: in expansion of macro 'PYBIND11_COMPAT_STRDUP'
324 | auto *t = PYBIND11_COMPAT_STRDUP(s);
| ^~~~~~~~~~~~~~~~~~~~~~
C:\Users\blindschleiche\Anaconda3\envs\feb2023\lib\site-packages\pybind11\include/pybind11/pybind11.h: In member function 'void pybind11::cpp_function::initialize_generic(pybind11::cpp_function::unique_function_record&&, const char*, const std::type_info* const*, pybind11::size_t)':
C:\Users\blindschleiche\Anaconda3\envs\feb2023\lib\site-packages\pybind11\include/pybind11/pybind11.h:76:36: error: 'strdup' was not declared in this scope; did you mean 'strcmp'?
76 | # define PYBIND11_COMPAT_STRDUP strdup
| ^~~~~~
C:\Users\blindschleiche\Anaconda3\envs\feb2023\lib\site-packages\pybind11\include/pybind11/pybind11.h:610:46: note: in expansion of macro 'PYBIND11_COMPAT_STRDUP'
610 | = signatures.empty() ? nullptr : PYBIND11_COMPAT_STRDUP(signatures.c_str());
| ^~~~~~~~~~~~~~~~~~~~~~
C:\Users\blindschleiche\Anaconda3\envs\feb2023\lib\site-packages\pybind11\include/pybind11/pybind11.h: In member function 'pybind11::class_<type_, options>& pybind11::class_<type_, options>::def_property_static(const char*, const pybind11::cpp_function&, const pybind11::cpp_function&, const Extra& ...)':
C:\Users\blindschleiche\Anaconda3\envs\feb2023\lib\site-packages\pybind11\include/pybind11/pybind11.h:76:36: error: there are no arguments to 'strdup' that depend on a template parameter, so a declaration of 'strdup' must be available [-fpermissiv
]
76 | # define PYBIND11_COMPAT_STRDUP strdup
| ^~~~~~
C:\Users\blindschleiche\Anaconda3\envs\feb2023\lib\site-packages\pybind11\include/pybind11/pybind11.h:1781:33: note: in expansion of macro 'PYBIND11_COMPAT_STRDUP'
1781 | rec_fget->doc = PYBIND11_COMPAT_STRDUP(rec_fget->doc);
| ^~~~~~~~~~~~~~~~~~~~~~
C:\Users\blindschleiche\Anaconda3\envs\feb2023\lib\site-packages\pybind11\include/pybind11/pybind11.h:76:36: note: (if you use
-fpermissive', G++ will accept your code, but allowing the use of an undeclared name is deprecated)
76 | # define PYBIND11_COMPAT_STRDUP strdup
| ^~~~~~
C:\Users\blindschleiche\Anaconda3\envs\feb2023\lib\site-packages\pybind11\include/pybind11/pybind11.h:1781:33: note: in expansion of macro 'PYBIND11_COMPAT_STRDUP'
1781 | rec_fget->doc = PYBIND11_COMPAT_STRDUP(rec_fget->doc);
| ^~~~~~~~~~~~~~~~~~~~~~
C:\Users\blindschleiche\Anaconda3\envs\feb2023\lib\site-packages\pybind11\include/pybind11/pybind11.h:76:36: error: there are no arguments to 'strdup' that depend on a template parameter, so a declaration of 'strdup' must be available [-fpermissiv
]
76 | # define PYBIND11_COMPAT_STRDUP strdup
| ^~~~~~
C:\Users\blindschleiche\Anaconda3\envs\feb2023\lib\site-packages\pybind11\include/pybind11/pybind11.h:1789:33: note: in expansion of macro 'PYBIND11_COMPAT_STRDUP'
1789 | rec_fset->doc = PYBIND11_COMPAT_STRDUP(rec_fset->doc);
|
Just for example, focus on the errors mentioned for "fileutils.h":
C:\Users\blindschleiche\Anaconda3\envs\feb2023\Include/fileutils.h:80:12: error: expected ';' at end of member declaration
80 | time_t st_atime;
| ^~~~~~~~
C:\Users\blindschleiche\Anaconda3\envs\feb2023\Include/fileutils.h:80:12: error: expected unqualified-id before '.' token
80 | time_t st_atime;
| ^~~~~~~~
C:\Users\blindschleiche\Anaconda3\envs\feb2023\Include/fileutils.h:82:12: error: expected ';' at end of member declaration
82 | time_t st_mtime;
| ^~~~~~~~
C:\Users\blindschleiche\Anaconda3\envs\feb2023\Include/fileutils.h:82:12: error: expected unqualified-id before '.' token
82 | time_t st_mtime;
| ^~~~~~~~
C:\Users\blindschleiche\Anaconda3\envs\feb2023\Include/fileutils.h:84:12: error: expected ';' at end of member declaration
84 | time_t st_ctime;
| ^~~~~~~~
C:\Users\blindschleiche\Anaconda3\envs\feb2023\Include/fileutils.h:84:12: error: expected unqualified-id before '.' token
84 | time_t st_ctime;
| ^~~~~~~~
and compare the errors with the actual code within the file:
// fileutils.h, line 70 onwards
#ifdef MS_WINDOWS
struct _Py_stat_struct {
unsigned long st_dev;
uint64_t st_ino;
unsigned short st_mode;
int st_nlink;
int st_uid;
int st_gid;
unsigned long st_rdev;
__int64 st_size;
time_t st_atime;
int st_atime_nsec;
time_t st_mtime;
int st_mtime_nsec;
time_t st_ctime;
int st_ctime_nsec;
unsigned long st_file_attributes;
unsigned long st_reparse_tag;
};
#else
# define _Py_stat_struct stat
#endif
There is neither a semicolon missing, nor there is a .
token.
So I do not understand what are the reasons for this error or whether someone already encountered such problems. I tried to find information about this issue, but most questions relating this topic are about a "missing python.h-file" and not some compilation errors.
Has someone an idea how to resolve this errors? Or does someone have experience with using PyBind11 on a windows system?
__int64
in _Py_stat_struct
is a microsoft-specific sized integer type. Hence the gcc compiler cannot decode it because it isn't aware of such type. To properly compile the files you should use the Microsoft compiler.