【问题标题】:Calling Python from C++, how to get rid of the `-Wstrict-prototypes` warning?从 C++ 调用 Python,如何摆脱`-Wstrict-prototypes` 警告?
【发布时间】:2014-06-23 15:32:19
【问题描述】:

我正在尝试从 C++ 调用 python 函数。我写了一个简单的 main.cpp 文件和 helloworld.py 如下:

ma​​in.cpp:

int main(int argc, char* argv[])
{
    Py_Initialize();
    PyRun_SimpleString("import sys");
    PyRun_SimpleString("sys.path.append('./')");

    PyObject *pModule = PyImport_ImportModule( "helloworld" );

    PyObject *pFunc = PyObject_GetAttrString(pModule, "say_hello_world");

    PyEval_CallObject(pFunc, NULL);

    Py_Initialize();
}

helloworld.py:

def say_hello_world():
    print( "Hello World from Python" )

我用以下代码编译程序:

g++ `python-config --cflags` main.cpp `python-config --ldflags` -o main

因此,除了我收到以下警告之外,一切正常:

cc1plus:警告:命令行选项“-Wstrict-prototypes”对 C/ObjC 有效,但对 C++ 无效[默认启用]

这是什么原因?有没有办法摆脱它?

【问题讨论】:

  • 对不起,如果我搞砸了你的命令行。反引号应仅用于指示code - 我很难猜出你的意思,这就是我最终得到的。如果不准确,请修正。
  • @BartoszKP 这些反引号在命令行中是必需的。 python-config --cflagspython-config --ldflags 只是意味着添加 python 库的配置。例如,如果我们使用 OpenCV 库,我们将添加诸如 opencv-config --cflags 之类的内容。

标签: python c++ gcc gcc-warning


【解决方案1】:

编写一个脚本gccflags-filter 来过滤掉不适合给定语言的标志。例如

  python-config --cflags | gccflags-filter --lang=c++

标志列表可以从文档中获取。

如果您现在需要针对特定​​问题采取权宜之计,请考虑类似

 g++ `python-config --cflags | sed s/-Wstrict-prototypes//`

【讨论】:

  • ...请提交错误报告,以便人们修复该脚本。
猜你喜欢
  • 2010-10-23
  • 1970-01-01
  • 1970-01-01
  • 2011-12-27
  • 2016-02-04
  • 1970-01-01
  • 2017-01-25
  • 1970-01-01
相关资源
最近更新 更多