gpt4 book ai didi

c++ - 如何在 OSX 上为 Python 编译 C++ 扩展(而不是 C)?

转载 作者:太空狗 更新时间:2023-10-29 20:29:41 25 4
gpt4 key购买 nike

我正在使用 distutils,并且有一个模块 spammodule.c 可以完美地编译、链接和工作。

但是如果我将该模块重命名为 .cpp 并重新运行 distutils 构建/安装序列,该模块会构建但在 python 中导入时会给我一个错误:

ImportError: 动态模块没有定义初始化函数 (initspam)

CPP 中的新模块与 C 中的新模块有不同的格式吗?

最佳答案

你需要将你的初始化函数包含在

#ifdef __cplusplus
extern "C" {
#endif

//initspam goes here

#ifdef __cplusplus
}
#endif

为了防止它的名字被破坏。

关于c++ - 如何在 OSX 上为 Python 编译 C++ 扩展(而不是 C)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9233181/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com