Skip to content

static library build #84

Closed
Closed

Description

@nickdesaulniers

I'm trying to build this as a static lib that I can link to, but must admit I'm out of my depth. Is it possible? I've added the usual cmake commands but am getting various errors. If I try to build the archive manually, I get that it's empty. If I compile src/json.hpp to an object file, it's only 200B in file size, which seems wrong, objdump doesn't print any symbols from it, and ar complains that the would be archive would be wrong. What am I missing here? I recognize the library is meant to be included as a header, and static linking doesn't save me from having to import the forward declarations anyways.

Activity

  1. vpetrigo commented on Jun 5, 2015

    @vpetrigo
    Contributor

    @nickdesaulniers You can not compile the json.hpp because it's just header file. You have to include it in you source files and then build your own library by using ar if you wish. It happens because header files contain only definition, templates or inline functions (if we talk about class definition), templates and so on which will be placed where you want to use them in your code.

    So, for example you have header foo.h and source file foo.cpp:

    // foo.h
    
    void foo();
    // foo.cpp
    #include <iostream>
    
    void foo() {
        std::cout << "Hello from foo!" << std::endl
    }

    So, you want to use foo() function in you project. To do that you need to include foo.h header where definition of your function is placed. After that you add foo.cpp to your project and it will be compiled to object file and then linked. But if you make some changes in foo.cpp it must be recompiled again. One day you decide that your function void foo() is ideal and you do not want to make any changes or you want to hide implementation from others. In that case you could build object file and archive it with ar for making library which you could statically linked to your project. Then you will need only header file for understanding which functions you are able to use from you library.

    I hope the process more or less clear.

  2. nickdesaulniers commented on Jun 5, 2015

    @nickdesaulniers
    Author

    ah, so that's why inline is everywhere. Thanks for explanation Vladamir, I appreciate it!

  3. nlohmann commented on Jun 7, 2015

    @nlohmann
    Owner

    Thanks @vpetrigo for the explanation. In fact, the class makes heavy use of templates to be applicable in many scenarios where we know little about the values we want to store to JSON, but can rely on the fact that they are some kind of STL containers.

  4. added a commit that references this issue on Nov 13, 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

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions