Skip to content

Linker error after string_view commit #1595

Open
@vai-terokinnunen

Description

@vai-terokinnunen

Describe the bug
Since commit 60ccc1f ("feat: support std::string_view in Value API"), simple example fails to linker error.

To Reproduce
Steps to reproduce the behavior:

  1. Install latest jsoncpp (using Ubuntu:24.04 Docker image in this test)
git clone https://github.com/open-source-parsers/jsoncpp.git
cmake -B build-jsoncpp jsoncpp
cmake --build build-jsoncpp --target install
  1. Example project:

CMakeLists.txt

project("Example")
add_executable(example main.cpp)
target_link_libraries(example jsoncpp)

main.cpp

#include <iostream>
#include <json/json.h>
int main()
{
    Json::Value root;
    root["key"] = "value";
    std::cout << root.isMember("key") << '\n';
    root.removeMember("key");
    return 0;
}
  1. Try build cmake -B build-example example && cmake --build build-example
  2. Build fails with linker error
: && /usr/bin/c++  -rdynamic CMakeFiles/example.dir/main.cpp.o -o example  -ljsoncpp && :
/usr/bin/ld: CMakeFiles/example.dir/main.cpp.o: in function `main':
main.cpp:(.text+0x6c): undefined reference to `Json::Value::operator[](std::basic_string_view<char, std::char_traits<char> >)'
/usr/bin/ld: main.cpp:(.text+0xb7): undefined reference to `Json::Value::isMember(std::basic_string_view<char, std::char_traits<char> >) const'
/usr/bin/ld: main.cpp:(.text+0x105): undefined reference to `Json::Value::removeMember(std::basic_string_view<char, std::char_traits<char> >)'
collect2: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.

Expected behavior

  • Build successful. Works before commit 60ccc1f

Desktop (please complete the following information):

  • OS: Ubuntu 24.04
  • Meson version 1.3.2 (repeats with both meson and cmake builds)
  • Ninja version 1.11.1

Additional context

  • Extracting "key" to std::string, std::string_view or char* constant does not resolve the issue.

Activity

jmjatlanta

jmjatlanta commented on Feb 19, 2025

@jmjatlanta

I am getting the same problem on MacOS. I can work around it by removing the check in json/value.h to always set JSONCPP_HAS_STRING_VIEW to 1

or

A better fix is to modify CMakeLists.txt to set CMAKE_CXX_STANDARD to 17 (NOTE: setting it to 20 runs into problems with char8_t)

baylesj

baylesj commented on Mar 12, 2025

@baylesj
Contributor

Started messing around with this locally and was able to reproduce.

This looks to me like a library version mismatch. If JsonCpp is compiled with C++11, but the example project is compiled with C++17, then this error reproduces for me locally. If the example project is compiled with C++11 as well, I don't receive this error.

Being compatible with different versions of C++ is a big value add for JsonCpp, at least historically. I am reluctant to move to C++17 as a requirement in general.

This bug shouldn't impact most production use cases, AFAIK. I think what is probably the best answer at this point is to ensure that the headers that get installed to the system actually match the library that get installed to the system.

de1acr0ix

de1acr0ix commented on May 19, 2025

@de1acr0ix

As CMAKE_CXX_STANDARD is forced to be 11, JSONCPP_HAS_STRING_VIEW seems to be always undefined when building the library itself, because of the __cplusplus version check.

One possible approach may be omitting CMAKE_CXX_STANDARD, but using target_compile_features() to specify cxx_std_11 instead. As I see it is already there for lib_json there.

Nana5aki

Nana5aki commented on May 24, 2025

@Nana5aki

I had the same problem on Ubuntu 24.04, but I set neither CMAKE_CXX_STANDARD nor JSONCPP_HAS_STRING_VIEW to solve the problem

added a commit that references this issue on Jun 20, 2025
added a commit that references this issue on Jul 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Linker error after string_view commit · Issue #1595 · open-source-parsers/jsoncpp