Привет, у меня есть базовый CmakeList.txt для создания кода C++, вызывающего функцию python.
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;
}
include_directories( ${PCL_INCLUDE_DIRS} )
добавляет путь включения, в котором находятся заголовки другой версии библиотеки Python. - person t.niese   schedule 01.02.2021include_directories
,add_definitions
, расширение переменных пакета (в отличие от использования импортированных целей) и т. д. вызывают проблемы. Эти функции полностью устарели, и им не место в новом коде CMake. - person Alex Reinking   schedule 01.02.2021FindPythonLibs
также устарел. Используйте современные CMake иFindPython
. - person Alex Reinking   schedule 01.02.2021