getml / reflect-cpp

A C++20 library for fast serialization, deserialization and validation using reflection. Supports JSON, BSON, CBOR, flexbuffers, msgpack, TOML, XML, YAML / msgpack.org[C++20]
https://getml.github.io/reflect-cpp/
MIT License
901 stars 76 forks source link

Link error #15

Closed madmongo1 closed 7 months ago

madmongo1 commented 9 months ago

Given this program:

#include <iostream>
#include <rfl.hpp>
#include <rfl/json.hpp>

struct foo
{
    std::string a;
    int         b;
};

int
main(int argc, char *argv[])
{
    auto x = foo { .a = "Hello, World!", .b = 100 };
    std::cout << rfl::json::write(x) << '\n';
}

How do I fix this error?

[build] /usr/bin/ld: CMakeFiles/scratch3.dir/scratch3/main.cpp.o: in function `auto rfl::internal::get_field_names<iron::foo>()':
[build] .../_deps/reflect-cpp-src/include/rfl/internal/get_field_names.hpp:126: undefined reference to `rfl::internal::fake_object<iron::foo>'
[build] collect2: error: ld returned 1 exit status

The program is linked with target reflectcpp_static.

Thank you.

liuzicheng1987 commented 9 months ago

@madmongo1 , thanks for the issue!

I am unable to reproduce it, though. I have tried this with GCC and clang and in both cases it compiles without issues and produces the following output:

{"a":"Hello, World!","b":100}

Two possible reasons I can think of:

1) Are you trying to compile this with MSVC? MSVC is not yet supported (as we state in the README). However, I will add support for MSVC this weekend.

2) I can see that there is an undefined reference to rfl::internal::fake_object<iron::foo>, but your minimal example does not contain anything called iron. One limitation is that the struct cannot be declared inside a function (which is my bad...I haven't documented this limitation yet). Is iron a function?

liuzicheng1987 commented 8 months ago

@madmongo1 , thanks to the input of @ecatmur, I was able to reproduce the issue and implement a fix (https://github.com/getml/reflect-cpp/commit/2e3872c651dc5c0a2c06fa4f0b094617c74fc8bd).

Can you confirm that this fixes your issue?

If no, can you provide the compiler used (including the version number) and the compiler flags used?