g++编译文件并调用python

写c++代码并编译

1报错找不到include 的python.h文件

找到python.h所在的目录
比如:C:\ProgramData\Anaconda3\include
编译时添加 -IC:\ProgramData\Anaconda3\include
g++找到了python.h

g++ -IC:\ProgramData\Anaconda3\include test.cpp
g++编译文件并调用python_第1张图片

2报错’::hypot’ has not been declared"error in g++”

g++编译文件并调用python_第2张图片
在stackoverflow中,找到答案
https://stackoverflow.com/questions/42276984/hypot-has-not-been-declared
修改代码

#include < cmath>
#include

g++编译文件并调用python_第3张图片
再修改pyconfig.h的line 234

#define hypot _hypot

改为

#define hypot hypot

g++编译文件并调用python_第4张图片
至此,如果test.cpp代码如下

#include “Python.h”
int main(int argc, char *argv[])
{
printf(“hello world!”);
return 0;
}

在这里插入图片描述

报错 undefined reference to “__imp_PyTuple_New”

或 undefined reference to “__imp_Py_Initialize()”

#include “Python.h”
int main(int argc, char argv[])
{
PyObject
t;
t = PyTuple_New(3);
printf(“hello world!”);
return 0;
}
在这里插入图片描述

解决办法是先找到gendef.exe,
比如C:\TDM-GCC-64\x86_64-w64-mingw32\bin
加入环境变量,我的电脑->propertise->advanced system setting->Enviroment Variables->User variables->Path->edit->
测试,gendef命令可用
g++编译文件并调用python_第5张图片

然后进入python37.dll所在的目录,生成def
g++编译文件并调用python_第6张图片
参考https://stackoverflow.com/questions/6731100/link-to-python-with-mingw
Try this…
1)Download gendef for your version of mingw (32 or 64 bit), and in msys shell…
2)Run gendef /c/windows/system32/python32.dll
3)Run dlltool -D python32.dll -d python32.def -l libpython32.a
4)Copy libpython32.a to your ./python32/libs directory.

此时,对于

#include
int main(int argc, char *argv[])
{
wchar_t program = Py_DecodeLocale(argv[0], NULL);
if (program == NULL) {
fprintf(stderr, “Fatal error: cannot decode argv[0]\n”);
exit(1);
}
Py_SetProgramName(program); /
optional but recommended */
Py_Initialize();
PyRun_SimpleString(“from time import time,ctime\n”
“print(‘Today is’, ctime(time()))\n”);
Py_Finalize();
PyMem_RawFree(program);
return 0;
}

在这里插入图片描述

参考官网指南
https://docs.python.org/3.5/c-api/index.html
https://docs.python.org/3.5/c-api/intro.html

Introduction

两种接口方式
1The first reason is to write extension modules for specific purposes; these are C modules that extend the Python interpreter. This is probably the most common use.
2The second reason is to use Python as a component in a larger application; this technique is generally referred to as embedding Python in an application.

it’s probably a good idea to become familiar with writing an extension before attempting to embed Python in a real application.

Include Files

#include “Python.h”

引用顺序
Note Since Python may define some pre-processor definitions which affect the standard headers on some systems, you must include Python.h before any standard headers are included.
前缀
All user visible names defined by Python.h (except those defined by the included standard headers) have one of the prefixes Py or _Py. Names beginning with _Py are for internal use by the Python implementation and should not be used by extension writers. Structure member names do not have a reserved prefix.
Important: user code should never define names that begin with Py or _Py. This confuses the reader, and jeopardizes the portability of the user code to future Python versions, which may define additional names beginning with one of these prefixes.
header files的存储位置
On Windows, the headers are installed in prefix/include, where prefix is the installation directory specified to the installer.

Objects, Types and Reference Counts

返回值
Most Python/C API functions have one or more arguments as well as a return value of type PyObject*.

This type is a pointer to an opaque data type representing an arbitrary Python object. Since all Python object types are treated the same way by the Python language in most situations (e.g., assignments, scope rules, and argument passing), it is only fitting that they should be represented by a single C type. Almost all Python objects live on the heap: you never declare an automatic or static variable of type PyObject, only pointer variables of type PyObject* can be declared. The sole exception are the type objects; since these must never be deallocated, they are typically static PyTypeObject objects.

Reference Counts

Exceptions

参考官网指南
https://docs.python.org/3.5/extending/embedding.html

1. Embedding Python in Another Application

首先,主程序要初始化 Python interpreter, call the function Py_Initialize().
其次,有不同的方式调用Python interpreter.
1)you can pass a string containing Python statements to PyRun_SimpleString()
2)pass a stdio file pointer and a file name (for identification in error messages only) to PyRun_SimpleFile().
3) call the lower-level operations and use Python objects.

1.1. Very High Level Embedding

你可能感兴趣的:(G++,python)