You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
@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:
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.
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.
Activity
vpetrigo commentedon Jun 5, 2015
@nickdesaulniers You can not compile the
json.hppbecause it's just header file. You have to include it in you source files and then build your own library by usingarif 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.hand source filefoo.cpp:So, you want to use
foo()function in you project. To do that you need to includefoo.hheader where definition of your function is placed. After that you addfoo.cppto your project and it will be compiled to object file and then linked. But if you make some changes infoo.cppit must be recompiled again. One day you decide that your functionvoid 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 witharfor 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 commentedon Jun 5, 2015
ah, so that's why
inlineis everywhere. Thanks for explanation Vladamir, I appreciate it!nlohmann commentedon 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.
generate list of misbehaviours (nlohmann#84)