json-schema-validator
json-schema-validator copied to clipboard
undefined reference to `nlohmann::json_schema::json_validator::json_validator
Hello,
I'm trying to use json-schema-validator with Cmake for several days now but i'm stuck on undefined reference for the json_validator, at first i thought it was a linking issue.
This piece of cmake code
find_package(nlohmann_json_schema_validator REQUIRED)
if(TARGET nlohmann_json_schema_validator::validator)
message("nlohmann_json_schema_validator")
endif()
Finally convince that indeed the target name was nlohmann_json_schema_validator::validator and not just nlohmann_json_schema_validator as stated in the Readme.
But it did not solve my problem, the other strange thing that convinced my that it was no linking issue is that this line compile correctly :
nlohmann::json_schema::basic_error_handler handler;
So i guess the nlohmann_json_schema_validator is properly found and link (right ?).
But this one
nlohmann::json_schema::json_validator validator
Does not
I also tried with another ctor :
nlohmann::json_schema::json_validator validator(loader, nlohmann::json_schema::default_string_format_check);
With the same result :
undefined reference to `nlohmann::json_schema::json_validator::json_validator(std::function<void (nlohmann::json_uri const&, nlohmann::basic_json<std::map, std::vector, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, bool, long, unsigned long, double, std::allocator, nlohmann::adl_serializer, std::vector<unsigned char, std::allocator<unsigned char> > >&)>, std::function<void (std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)>, std::function<void (std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, nlohmann::basic_json<std::map, std::vector, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, bool, long, unsigned long, double, std::allocator, nlohmann::adl_serializer, std::vector<unsigned char, std::allocator<unsigned char> > > const&)>)'
collect2: error: ld returned 1 exit status
Any ideas ?
Yes, nlohmann_json_schema_validator::validator is correct and it will work with either find_package or add_subdirectory. Sorry I did not get to updating the README.
One thing I haven't implemented is a consistency check that the same nlohmann::json is used as the one used in the compilation. It is a long shot, but worth checking since all constructors seem to have a variable linked to nlohmann::json. Afaik nlohmann::json is still header-only right? So this could be relatively difficult to check after the installation since it wouldn't have any library that you could check the linkage with readelf.
As a workaround, try to consume the CMake project with FetchContent instead of find_package. This would rebuild the project and it should use the current nlohmann::json you have linked to.
I tried this :
include(FetchContent)
FetchContent_Declare(
nlohmann_json_schema_validator
GIT_REPOSITORY https://github.com/pboettch/json-schema-validator/
GIT_TAG 2.3.0
)
FetchContent_MakeAvailable(nlohmann_json_schema_validator)
target_link_libraries(some_lib nlohmann_json_schema_validator)
add_executable(some_exe some_src)
target_link_libraries(some_exe some_lib )
With the same result.
I don't if it is a hint but i'm able to to build the lib successfully, but not the exe.
Also i was not sure if i was suppose to link to vanilla nlohmann_json, so i tried adding (also tried with only find_package) :
find_package(nlohmann_json 3.2.0 REQUIRED)
target_link_libraries(${PROJECT_QGOV}_lib nlohmann_json::nlohmann_json)
I got at cmake step :
-- Found nlohmann_json: /usr/local/lib64/cmake/nlohmann_json/nlohmann_jsonConfig.cmake (found suitable version "3.7.3", minimum required is "3.2.0")
-- JsonValidator: Configured for Release
-- JsonValidator: Using nlohmann/json version: 3.7.3
Which is quite encouraging, yet compilation still fails with even more undefined references :
undefined reference to `nlohmann::json_schema::default_string_format_check(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
qPuGov.cpp:(.text+0x1ed3): undefined reference to `nlohmann::json_schema::json_validator::json_validator(std::function<void (nlohmann::json_uri const&, nlohmann::basic_json<std::map, std::vector, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, bool, long, unsigned long, double, std::allocator, nlohmann::adl_serializer, std::vector<unsigned char, std::allocator<unsigned char> > >&)>, std::function<void (std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)>, std::function<void (std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, nlohmann::basic_json<std::map, std::vector, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, bool, long, unsigned long, double, std::allocator, nlohmann::adl_serializer, std::vector<unsigned char, std::allocator<unsigned char> > > const&)>)'
qPuGov.cpp:(.text+0x2200): undefined reference to `nlohmann::json_schema::json_validator::~json_validator()'
qPuGov.cpp:(.text+0x22e9): undefined reference to `nlohmann::json_schema::json_validator::~json_validator()'
CMakeFiles/governor.dir/src/governor/qPuGov.cpp.o: In function `nlohmann::json_uri::url[abi:cxx11]() const':
qPuGov.cpp:(.text._ZNK8nlohmann8json_uri3urlB5cxx11Ev[_ZNK8nlohmann8json_uri3urlB5cxx11Ev]+0x1f): undefined reference to `nlohmann::json_uri::location[abi:cxx11]() const'
collect2: error: ld returned 1 exit status
Ok i found out that i had an old nlohmann_json (3.7.3) installed, somehow compiling the lastest version and doing make install did not overwritted the old one. I had to manually wipe it out.
Now i have this :
-- Found nlohmann_json: /usr/local/share/cmake/nlohmann_json/nlohmann_jsonConfig.cmake (found suitable version "3.11.3", minimum required is "3.2.0")
-- JsonValidator: Configured for Release
-- JsonValidator: Using nlohmann/json version: 3.11.3
And still an undefined reference but only one :
undefined reference to `nlohmann::json_schema::json_validator::json_validator(std::function<void (nlohmann::json_uri const&, nlohmann::basic_json<std::map, std::vector, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, bool, long, unsigned long, double, std::allocator, nlohmann::adl_serializer, std::vector<unsigned char, std::allocator<unsigned char> > >&)>, std::function<void (std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)>, std::function<void (std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, nlohmann::basic_json<std::map, std::vector, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, bool, long, unsigned long, double, std::allocator, nlohmann::adl_serializer, std::vector<unsigned char, std::allocator<unsigned char> > > const&)>)'
collect2: error: ld returned 1 exit status
Finally setup my desk I can have a look at it. Did you manually set an environment varialbe nlohmann_json_VERSION or did you add_subdirectory (or FetchContent) for nlohmann_json? I think there's something off in the CMake build I will look into refactoring and maybe we can try it again after that.
I tried fetch content for nlohmann_json and also installing it from the github repo, i had the same result with both. And i did not manually set an environment varialbe nlohmann_json_VERSION.
Could you share repo or dockerfile to reproduce locally as well? Any complexity level is ok with me, but of course if it can be minimized it would be preferred.
Sorry i can't share any reproducer, I went on some other project.
But i did nothing fancy, running on Rocky linux 8 -Builing and installing json validator repo. -Cmake project with
find_package(nlohmann_json_schema_validator REQUIRED)
target_link_libraries(some_lib nlohmann_json_schema_validator::validator)
-Try to build any file with
nlohmann::json_schema::json_validator validator