使用 JsonCpp return 数据到 python 与 pybind11 在 python 调用中产生 Symbol not found 错误

Using JsonCpp to return data to python with pybind11 produces Symbol not found error in python call

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

我已经设法让make文件配合识别JsonCpp并编译,但是在调用python中的方法时一直无法消除以下错误:

ImportError: dlopen(/PATH/REDACTED/project.cpython-36m-darwin.so, 2): Symbol not found: __ZN4Json5ValueC1ENS_9ValueTypeE

Expected in: flat namespace Referenced from: /PATH/REDACTED/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 模块时,您需要link针对 JsonCpp 库。

这个错误是因为这个符号应该在库中,但是因为缺少link而找不到它。

我能够使用 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})