#include "Python.h"
#include <iostream>
using namespace std;
int main()
{
Py_Initialize();
PyRun_SimpleString("import sys");
PyRun_SimpleString("sys.path.append('./')");
PyObject * pModule = NULL;
PyObject * pFunc = NULL;
pModule =PyImport_ImportModule("pp");
pFunc= PyObject_GetAttrString(pModule, "pp");
//cout<<pFunc<<endl;
PyEval_CallObject(pFunc, NULL);
Py_Finalize();
return 0;
#include <iostream>
using namespace std;
int main()
{
Py_Initialize();
PyRun_SimpleString("import sys");
PyRun_SimpleString("sys.path.append('./')");
PyObject * pModule = NULL;
PyObject * pFunc = NULL;
pModule =PyImport_ImportModule("pp");
pFunc= PyObject_GetAttrString(pModule, "pp");
//cout<<pFunc<<endl;
PyEval_CallObject(pFunc, NULL);
Py_Finalize();
return 0;
}
CmakeLists.txt:
cmake_minimum_required(VERSION 2.6)
project(c_to_python)
find_package(PythonLibs REQUIRED)
include_directories("/usr/include/python2.7")
#target_link_libraries("/usr/lib/python2.7/config-x86_64-linux-gnu/")
#include_directories(${PYTHON_INCLUDE_DIRS})
message("ctopython")
message(${PYTHON_INCLUDE_DIRS})
add_executable(c_to_python main.cpp)
#target_link_libraries(c_to_python "/usr/lib/python2.7/site-packages/")
target_link_libraries(c_to_python "/usr/lib/x86_64-linux-gnu/libpython2.7.so")
#target_link_libraries(c_to_python ${PYTHON_LIBRARIES})
message(${PYTHON_LIBRARIES})
需python文件和执行文件在同一个目录。