Loki-Astari / ThorsMongo

C++ MongoDB API and BSON/JSON Serialization library
GNU General Public License v3.0
316 stars 71 forks source link

Linker fails with header-only build #81

Closed SteveS-Serrano closed 1 year ago

SteveS-Serrano commented 1 year ago

Describe the bug Header only build fails linking with undefined references to (apparently all) ThorsAnvil::Serialize::JsonPrinter:: and loguru:: functions.

I have installed the header-only branch of the repo:

git clone --single-branch --branch header-only https://github.com/Loki-Astari/ThorsSerializer.git

the latest tagged version of the magic_enum package

magic_enum-0.8.2.tar.gz

I put both these sources in subdirectories and created a CMakeLists.txt file to build them both. Compilation succeeds ok. But linking fails when I try to compile the trivial example as a google test case.

Other than include ThorSerialize/JsonThor.h, what else should I be doing? There aren't any other instructions for the header only build that I can find. Any help greatly appreciated.

CMakeLists.txt for Thors...

# target
set(top ../../../../)
include(${top}/cmake/preamble.cmake)

# library
set(target json-serdes)
add_library( ${target} INTERFACE )

target_include_directories(${target} INTERFACE
    "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>"
    "$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>"
 )

target_compile_features(${target} INTERFACE cxx_std_17)

target_link_libraries(${target} INTERFACE magic_enum)

install(TARGETS ${target}
    RUNTIME DESTINATION "${CMAKE_INSTALL_SBINDIR}"
    ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}"
    LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}")

google test file (.cpp)

#include "gtest/gtest.h"

#include <cstdio>
#include <iostream>
#include <memory>
#include <string>
#include <vector>

#include "ThorSerialize/JsonThor.h"

/////////////////////////////////////////////////////////////////////
// GoogleTest Class Definition
class EsiJsonSerdesGTest : public ::testing::Test {

protected:

    EsiJsonSerdesGTest() {
    }

    virtual ~EsiJsonSerdesGTest() {
        // Release all spdlog resources, and drop all loggers in the registry.
        // This is optional (only mandatory if using windows + async log).
    }

     // If the constructor and destructor are not enough for setting up
     // and cleaning up each test, you can define the following methods:

     virtual void SetUp() override {
       // Code here will be called immediately after the constructor (right
       // before each test).  You can add asserts here if needed
     }

     virtual void TearDown() override {
       // Code here will be called immediately after each test (right
       // before the destructor).  You can add asserts here if needed.
     }

};

TEST_F(EsiJsonSerdesGTest, Trivial)
{
    std::vector<int>    data;
    using ThorsAnvil::Serialize::jsonImporter;
    using ThorsAnvil::Serialize::jsonExporter;

    std::cin >> jsonImporter(data);
    std::cout << jsonExporter(data) << "\n";

    //EXPECT_TRUE(exists(log_file));

}

**Expected behavior**
> A clear and concise description of what you expected to happen.

Environment:

Loki-Astari commented 1 year ago

I will have a look this weekend. I am in the processes of tidying up a few things and make the header only version neater.

SteveS-Serrano commented 1 year ago

@Loki-Astari - I'd really like to use this package, but I'm stuck until it can build in our environment. Did you have a chance to look at the issue yet? Can you tell me how I might go about working around it just so I can move forward? Should I drop back to an older version? I saw an old post on stack overflow about the same issue - was that ever resolved?

Loki-Astari commented 1 year ago

Are you building on Windows?

If you are then I am trying to get a few build issues sorted before I can rebuild the header only version.

But if you only Mac or Linux then I can switch focus and get the header only version working first. Then move back to solving build issues on Windows.

SteveS-Serrano commented 1 year ago

Linux, Ubuntu 22.04

From: Loki @.> Sent: Tuesday, May 16, 2023 3:49 PM To: @.> Cc: @.>; @.> Subject: Re: [Loki-Astari/ThorsSerializer] Linker fails with header-only build (Issue #81)

Are you building on Windows?

— Reply to this email directly, view it on GitHubhttps://github.com/Loki-Astari/ThorsSerializer/issues/81#issuecomment-1550455738, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AQCFUJR3PV57QRVDJBYOJUDXGP77RANCNFSM6AAAAAAX6YNLNU. You are receiving this because you authored the thread.Message ID: @.***>

Loki-Astari commented 1 year ago

You do know that it is available pre-built on linix via brew.

https://docs.brew.sh/Homebrew-on-Linux

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" brew install libyaml brew install magic_enum brew install thors-serializer

Then you can simply link against it in your CMake file:

I am still working on the header only version. I'll update this as I get closer (this week).

SteveS-Serrano commented 1 year ago

I know about the pre-built version. Our project constraints require a header-only version for json serdes.

Loki-Astari commented 1 year ago

OK. Fixed the Windows build issues last night. Working on the Header Only version now.

Ssscy commented 1 year ago

you need add HEADER_ONLY macro

SteveS-Serrano commented 1 year ago

you need add HEADER_ONLY macro

Where? Can you give me an example?

SteveS-Serrano commented 1 year ago

@Ssscy 👍

you need add HEADER_ONLY macro

Thanks!!

I defined -DHEADER_ONLY=1 on the command line and this seems to have resolved the problem. This information should get added to doc/building.md

Loki-Astari commented 1 year ago

Update so the header-only version now builds out of the box. The current build tests: .github/workflows/build.yaml also validate the header-only version works out of the box. Latest build: https://github.com/Loki-Astari/ThorsSerializer/actions/runs/5458805284/jobs/9934301849