Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

static library build #84

Closed
nickdesaulniers opened this issue Jun 5, 2015 · 3 comments
Closed

static library build #84

nickdesaulniers opened this issue Jun 5, 2015 · 3 comments

Comments

@nickdesaulniers
Copy link

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.

@vpetrigo
Copy link
Contributor

vpetrigo commented Jun 5, 2015

@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.

@nickdesaulniers
Copy link
Author

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

@nlohmann
Copy link
Owner

nlohmann commented Jun 7, 2015

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants