英文:
PyBind11 - compilation errors from several library files
问题
最近,我在C++中创建了一些代码,我想要在Python中使用它,所以我选择了PyBind11,因为它似乎很直观。由于我从未使用过这个工具,我首先想要理解并尝试文档中提供的基本示例:
https://pybind11.readthedocs.io/en/latest/basics.html
我在Windows 10上使用Anaconda,并且已安装了cygwin和Visual Studio 2022。我创建了一个环境并使用pip安装了所有必需的包。
首先,我想使用gcc编译器(cygwin),并找到了以下设置编译器标志的方法:
https://stackoverflow.com/questions/60699002/how-can-i-build-manually-c-extension-with-mingw-w64-python-and-pybind11
我尝试使用以下命令编译代码:
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
但我收到了一堆源自Python库文件的错误。当然,我从未自行更改过这些文件。而且一些错误似乎是“不正确的”。以下是完整的错误消息:
在文件 C:\Users\blindschleiche\Anaconda3\envs\feb2023\Include/Python.h:156 中,
来自 C:\Users\blindschleiche\Anaconda3\envs\feb2023\lib\site-packages\pybind11\include/pybind11/detail/../detail/common.h:266 的引用,
来自 C:\Users\blindschleiche\Anaconda3\envs\feb2023\lib\site-packages\pybind11\include/pybind11/detail/../attr.h:13 的引用,
来自 C:\Users\blindschleiche\Anaconda3\envs\feb2023\lib\site-packages\pybind11\include/pybind11/detail/class.h:12 的引用,
来自 C:\Users\blindschleiche\Anaconda3\envs\feb2023\lib\site-packages\pybind11\include/pybind11/pybind11.h:13 的引用,
来自 test.cpp:1:
C:\Users\blindschleiche\Anaconda3\envs\feb2023\Include/fileutils.h:79:5: 错误: ' __int64' 不是类型名称;是否意味着 ' __int64_t'?
79 | __int64 st_size;
| ^~~~~~~
| __int64_t
来自 /usr/include/sys/stat.h:22,
来自 C:\Users\blindschleiche\Anaconda3\envs\feb2023\Include/pyport.h:245,
来自 C:\Users\blindschleiche\Anaconda3\envs\feb2023\Include/Python.h:63,
来自 C:\Users\blindschleiche\Anaconda3\envs\feb2023\lib\site-packages\pybind11\include/pybind11/detail/../detail/common.h:266 的引用,
来自 C:\Users\blindschleiche\Anaconda3\envs\feb2023\lib\site-packages\pybind11\include/pybind11/detail/../attr.h:13 的引用,
来自 C:\Users\blindschleiche\Anaconda3\envs\feb2023\lib\site-packages\pybind11\include/pybind11/detail/class.h:12 的引用,
来自 C:\Users\blindschleiche\Anaconda3\envs\feb2023\lib\site-packages\pybind11\include/pybind11/pybind11.h:13 的引用,
来自 test.cpp:1:
C:\Users\blindschleiche\Anaconda3\envs\feb2023\Include/fileutils.h:80:12: 错误: 预期的是 ';' 在成员声明的末尾
80 | time_t st_atime;
| ^~~~~~~~
C:\Users\blindschleiche\Anaconda3\envs\feb2023\Include/fileutils.h:80:12: 错误: 预期未限定的标识符之前的 '.'
80 | time_t st_atime;
| ^~~~~~~~
C:\Users\blindschleiche\Anaconda3\envs\feb2023\Include/fileutils.h:82:12: 错误: 预期的是 ';' 在成员声明的末尾
82 | time_t st_mtime;
| ^~~~~~~~
C:\Users\blindschleiche\Anaconda3\envs\feb2023\Include/fileutils.h:82:12: 错误: 预期未限定的标识符之前的 '.'
82 | time_t st_mtime;
| ^~~~~~~~
C:\Users\blindschleiche\Anaconda3\envs\feb2023\Include/fileutils.h:84:12: 错误: 预期的是 ';' 在成员声明的末尾
84 | time_t st_ctime;
| ^~~~~~~~
C:\Users\blindschleiche\Anaconda3\envs\feb2023\Include/fileutils.h:84:12: 错误: 预期未限定的标识符之前的 '.'
84 | time_t st_ctime;
| ^~~~~~~~
在 test.cpp:1 中包含的引用:
C:\Users\blindschleiche\Anaconda3\envs\feb2023\lib\site-packages\pybind11\include/pybind11/pybind11.h: 在成员函数 '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: 错误: '
<details>
<summary>英文:</summary>
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:
https://stackoverflow.com/questions/60699002/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?
</details>
# 答案1
**得分**: 2
`__int64` 在 `_Py_stat_struct` 中是一种 [微软特定的](https://learn.microsoft.com/en-us/cpp/cpp/int8-int16-int32-int64?view=msvc-170) 大小的整数类型。因此,gcc 编译器无法解码它,因为它不了解这种类型。为了正确编译这些文件,您应该使用微软编译器。
<details>
<summary>英文:</summary>
`__int64` in `_Py_stat_struct` is a [microsoft-specific](https://learn.microsoft.com/en-us/cpp/cpp/int8-int16-int32-int64?view=msvc-170) 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.
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论