0

嗨'我有基本的 CmakeList.txt 来构建调用 python 函数的 C++ 代码。

cmake_minimum_required(VERSION 2.6)
# Create Project
project(demo)

    # Find Packages
    find_package(PythonLibs REQUIRED)
    # [Additional Include Directories]
    include_directories(${PYTHON_INCLUDE_DIRS})
    add_definitions(${PCL_DEFINITIONS} )
    add_executable(demo embeddedPython.cpp)
    target_link_libraries(demo ${PYTHON_LIBRARIES})

它工作得很好,但现在我想添加 PCL 支持,如下所示。

cmake_minimum_required(VERSION 2.6)
# Create Project
project(demo)

# Find Packages
find_package(PythonLibs REQUIRED)
find_package(PCL 1.2 REQUIRED)

# [Additional Include Directories]
include_directories(${PYTHON_INCLUDE_DIRS})
include_directories( ${PCL_INCLUDE_DIRS} )

add_definitions(${PCL_DEFINITIONS} )
add_executable(demo embeddedPython.cpp)
target_link_libraries(demo ${PCL_LIBRARIES} ${PYTHON_LIBRARIES})

我收到以下错误:

error: PyUnicode_DecodeFSDefault’ was not declared in this scope
     pName = PyUnicode_DecodeFSDefault(argv[1]);
             ^~~~~~~~~~~~~~~~~~~~~~~~~
 error: ‘Py_FinalizeEx’ was not declared in this scope
     if (Py_FinalizeEx() < 0) {
         ^~~~~~~~~~~~~

CMakeFiles/demo.dir/build.make:62: recipe for target 'CMakeFiles/demo.dir/embeddedPython.cpp.o' failed
make[2]: *** [CMakeFiles/demo.dir/embeddedPython.cpp.o] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/demo.dir/all' failed
make[1]: *** [CMakeFiles/demo.dir/all] Error 2
Makefile:83: recipe for target 'all' failed

我尝试在示例项目中只使用 PCL,没有问题。我的新 cmakelists.txt 中缺少什么?

编辑:

代码:

#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include <iostream>
#include <pcl/io/ply_io.h>
#include <pcl/point_types.h>

int
main(int argc, char *argv[])
{
    PyObject *pName, *pModule, *pFunc;
    PyObject *pArgs, *pValue;
    int i;

    if (argc < 3) {
        fprintf(stderr,"Usage: call pythonfile funcname [args]\n");
        return 1;
    }

    Py_Initialize();
    PyRun_SimpleString("import sys");
    PyRun_SimpleString("sys.path.append(\".\")");
    pName = PyUnicode_DecodeFSDefault(argv[1]);
    /* Error checking of pName left out */
    pModule = PyImport_Import(pName);
    Py_DECREF(pName);

    if (pModule != NULL) {
        pFunc = PyObject_GetAttrString(pModule, argv[2]);
        /* pFunc is a new reference */

        if (pFunc && PyCallable_Check(pFunc)) {
            pArgs = PyTuple_New(argc - 3);
            for (i = 0; i < argc - 3; ++i) {
                pValue = PyLong_FromLong(atoi(argv[i + 3]));
                if (!pValue) {
                    Py_DECREF(pArgs);
                    Py_DECREF(pModule);
                    fprintf(stderr, "Cannot convert argument\n");
                    return 1;
                }
                /* pValue reference stolen here: */
                PyTuple_SetItem(pArgs, i, pValue);
            }
            pValue = PyObject_CallObject(pFunc, pArgs);
            Py_DECREF(pArgs);
            if (pValue != NULL) {
                printf("Result of call: %ld\n", PyLong_AsLong(pValue));
                Py_DECREF(pValue);
            }
            else {
                Py_DECREF(pFunc);
                Py_DECREF(pModule);
                PyErr_Print();
                fprintf(stderr,"Call failed\n");
                return 1;
            }
        }
        else {
            if (PyErr_Occurred())
                PyErr_Print();
            fprintf(stderr, "Cannot find function \"%s\"\n", argv[2]);
        }
        Py_XDECREF(pFunc);
        Py_DECREF(pModule);
    }
    else {
        PyErr_Print();
        fprintf(stderr, "Failed to load \"%s\"\n", argv[1]);
        return 1;
    }
    if (Py_FinalizeEx() < 0) {
        return 120;
    }

    pcl::PointCloud<pcl::PointXYZ>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZ>);
     if (pcl::io::loadPLYFile<pcl::PointXYZ> ("try.ply", *cloud) == -1) //* load the file
    {
    PCL_ERROR ("Couldn't read file test_pcd.pcd \n");
    return (-1);
     }


    std::cout << "Loaded "
            << cloud->width * cloud->height
            << " data points from test_pcd.pcd with the following fields: "
            << std::endl;
     for (const auto& point: *cloud)
    std::cout << "    " << point.x
              << " "    << point.y
              << " "    << point.z << std::endl;

   
   
    return 0;
}
4

0 回答 0