使用JsonCpp将数据返回到带有pybind11的python会在python调用中产生Symbol not foun



我正在尝试使用JsonCpp来解析一些数据,然后再将其返回到python(使用pybind11(。

我已经设法让make文件配合识别JsonCpp和编译,但到目前为止,在python中调用该方法时无法消除以下错误:

ImportError:dlopen(/PATH/REACTED/project.cpython-36m-darwin.so,2(:找不到符号:__ZN4Json5ValueC1ENS_9ValueTypeE

应为:平面命名空间引用自:/PATH/REACTED/project.cpython-36m-darwin.so

JsonCpp库中的任何内容似乎都有问题。

#include <pybind11/pybind11.h>
#include <pybind11/embed.h>
#include <json/json.h>
void callhome(pybind11::object site, std::string user_name)
{
pybind11::object page = site.attr("Pages").attr("__getitem__")("User:" + user_name + "/status");
std::string text = string(py::str(page.attr("text")()));
Json::Value root;
/* Json::Reader reader;
bool parsingSuccessful = reader.parse( text, root );
if ( !parsingSuccessful )
{
cout << "Error parsing the string" << endl;
}
const Json::Value mynames = root["run"];
for ( int index = 0; index < mynames.size(); ++index )
{
// py::print()
cout << mynames[index] << endl;
}*/
}
PYBIND11_MODULE(music_infobox, m) {
m.def("callhome",&callhome);
}

Python调用:

import mwclient,music_infobox,mwparserfromhell;
if __name__ == '__main__':
site = mwclient.Site(('https', 'en.wikipedia.org'), '/w/');
page = site.Pages['La Más Completa Colección (Marco Antonio Solís album)']
text = page.text()
music_infobox.save_edit(page,site,False,text,"DeprecatedFixerBot")

cmake:

cmake_minimum_required(VERSION 2.8.12)
project(music_infobox)
add_subdirectory(pybind11)
add_subdirectory(json)
#target_link_libraries(LibsModule -L/usr/local/Cellar/jsoncpp/1.8.4/include/json)
pybind11_add_module(music_infobox src/example.cpp src/example.h src/example.cpp src/utilities.h src/utilities.cpp)

任何想法都将不胜感激!

构建pybind11模块时,需要链接JsonCpp库。

出现错误的原因是这些符号应该在库中,但由于缺少链接而找不到。

我使用Matthieu Brucher的答案解决了这个问题,并将我的cmake更改为:

cmake_minimum_required(VERSION 2.8.12)
project(music_infobox)
add_subdirectory(pybind11)
pybind11_add_module(music_infobox src/example.cpp src/example.h src/example.cpp src/utilities.h src/utilities.cpp)
target_link_libraries(music_infobox PUBLIC ${JSONCPP_LIBRARIES})

最新更新