gpt4 book ai didi

python - 从 C++ 调用 Python,如何摆脱 `-Wstrict-prototypes` 警告?

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

我正在尝试从 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: warning: command line option ‘-Wstrict-prototypes’ is valid for C/ObjC but not for C++ [enabled by default]

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

最佳答案

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

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

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

如果您现在需要针对您的特定问题采取权宜之计,请考虑像这样的方法

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

关于python - 从 C++ 调用 Python,如何摆脱 `-Wstrict-prototypes` 警告?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24369812/

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