您的位置:首页 > 编程语言 > C语言/C++

C++调用python脚本

2017-08-21 09:50 387 查看
#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;
}

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文件和执行文件在同一个目录。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: