0

I am having trouble in including a header only library. In my main Application when I include the header file it works fine, now when I need to reference some of the classes from it in a different file then I get multiple definition errors. I checked the header only file has #pragma once and #ifndef checks added

Here is the code (webview.h) which is a header only library included here.

Window.h

#pragma once

#include <memory>
#include <filesystem>
#include <webview.h>

namespace HL {

    struct Props {
        int width;
        int height;
        std::string title;
        Props(const std::string& title = "My Test Application",
                    uint32_t width = 1600,
                    uint32_t height = 900)
            : title(title), width(width), height(height)
        {
        }
    };
    class Window {

        public:
            Window(const Props& props) 
            {
                init(props);
            }

            static Window* create(const Props props)
            {                
                return new Window(props);
            }

            ~Window() 
            {
                terminate();
            }

            void terminate()
            {
                mWebview.terminate();
            }

            void init(const Props& props )
            {
                mProps.width = props.width;
                mProps.height = props.height;
                mProps.title = props.title;

                mWebview = webview::webview(true, nullptr);
                mWebview.set_title(mProps.title);
                mWebview.set_size(1080, 800, WEBVIEW_HINT_NONE);
                std::string uiFile = std::filesystem::current_path().u8string() + std::string("/ui/build/index.html");
                mWebview.navigate(std::string("file://") + uiFile);

                mWebview.run();

            }
        
        private:
            webview::webview mWebview;
            int mWidth = 1080;
            int mHeight = 800;
            std::string mTitle = "";
            Props mProps;
    };
};

Application.h

#pragma once
#include <Focus.h>
#include <string>
#include <memory>
#include "Window.h"


namespace HL 
{
    class Application : public Focus::Application
    {
        private:
            Window *mWindow;
        public:
            Application(Focus::Sqlite* db): BaseApplication(db) 
            {
                FOCUS_LOG_INFO("Initializing DB");
                db->init();  
            }

            void run() 
            {
                startClients();

                mWindow = Window::create(Props("Test Application"));
                
            }

            Window& getWindow()
            {
                return *mWindow;
            }

            void loadPlugins()
            {

            }
    };

}

EventManager.h Here is when I include Window.h which has webview.h include I get the linker errors

#pragma once
#include <memory>
#include <sqlite3.h>
#include "Event.h"
#include <string>
#include <json.hpp>
#include <sstream>
#include "Window.h"

using json = nlohmann::json;

namespace HL 
{
    class EventManager
    {
        private:
            Window mWindow;
            sqlite3* mDB;

        public:
            EventManager(Window& window, sqlite3* db) : mWindow(window), mDB(db) {}
            void bindAll();
            void persist(Event& event);
    
    };
};

And the errors

/usr/bin/ld: CMakeFiles/hl.dir/src/main.cpp.o: in function `webview_create':
/home/naveed/Projects/hl/desktop/vendor/webview/webview.h:1305: multiple definition of `webview_create'; CMakeFiles/hl.dir/src/EventManager.cpp.o:/home/naveed/Projects/hl/desktop/vendor/webview/webview.h:1305: first defined here
/usr/bin/ld: CMakeFiles/hl.dir/src/main.cpp.o: in function `webview_destroy':

kaylum
  • 13,833
  • 2
  • 22
  • 31
Aftab Naveed
  • 3,652
  • 3
  • 26
  • 40
  • Does this answer your question? [Multiple definition and header-only libraries](https://stackoverflow.com/questions/8201944/multiple-definition-and-header-only-libraries) I know this is a C question but should apply directly. – Allan Wind Sep 20 '21 at 03:55
  • *"Here is the code (webview.h) which is a header only library included here."* -- Sorry, I do not see the code of `webview.h` "Here". – JaMiT Sep 20 '21 at 05:02
  • 1
    At a guess `webview_create` is missing an `inline` specifier – Alan Birtles Sep 20 '21 at 06:44
  • @AlanBirtles that's correct. I had to modify the webview.h file to add that and it worked. – Aftab Naveed Sep 20 '21 at 07:20

0 Answers0