xref: /freebsd/contrib/libcbor/doc/source/getting_started.rst (revision 5d3e7166f6a0187fa3f8831b16a06bd9955c21ff)
110ff414cSEd MasteGetting started
210ff414cSEd Maste==========================
310ff414cSEd Maste
410ff414cSEd MastePre-built Linux packages are available in most mainstream distributions
510ff414cSEd Maste
610ff414cSEd Maste**Ubuntu, Debian, etc.**:
710ff414cSEd Maste
810ff414cSEd Maste.. code-block:: bash
910ff414cSEd Maste
1010ff414cSEd Maste    apt-get install libcbor-dev
1110ff414cSEd Maste
1210ff414cSEd Maste**Fedora, openSUSE, etc.**:
1310ff414cSEd Maste
1410ff414cSEd Maste.. code-block:: bash
1510ff414cSEd Maste
1610ff414cSEd Maste    yum install libcbor-devel
1710ff414cSEd Maste
1810ff414cSEd Maste
1910ff414cSEd Maste**OS X** users can use `Homebrew <http://brew.sh/>`_:
2010ff414cSEd Maste
2110ff414cSEd Maste.. code-block:: bash
2210ff414cSEd Maste
2310ff414cSEd Maste    brew install libcbor
2410ff414cSEd Maste
2510ff414cSEd MasteFor other platforms, you will need to compile it from source.
2610ff414cSEd Maste
2710ff414cSEd MasteBuilding & installing libcbor
2810ff414cSEd Maste------------------------------
2910ff414cSEd Maste
3010ff414cSEd MastePrerequisites:
3110ff414cSEd Maste - C99 compiler
3210ff414cSEd Maste - CMake_ 2.8 or newer (might also be called ``cmakesetup``, ``cmake-gui`` or ``ccmake`` depending on the installed version and system)
3310ff414cSEd Maste - C build system CMake can target (make, Apple Xcode, MinGW, ...)
3410ff414cSEd Maste
3510ff414cSEd Maste.. _CMake: http://cmake.org/
3610ff414cSEd Maste
3710ff414cSEd Maste**Configuration options**
3810ff414cSEd Maste
3910ff414cSEd MasteA handful of configuration flags can be passed to `cmake`. The following table lists libcbor compile-time directives and several important generic flags.
4010ff414cSEd Maste
4110ff414cSEd Maste========================  =======================================================   ======================  =====================================================================================================================
4210ff414cSEd MasteOption                    Meaning                                                   Default                 Possible values
4310ff414cSEd Maste------------------------  -------------------------------------------------------   ----------------------  ---------------------------------------------------------------------------------------------------------------------
4410ff414cSEd Maste``CMAKE_C_COMPILER``      C compiler to use                                         ``cc``                   ``gcc``, ``clang``, ``clang-3.5``, ...
4510ff414cSEd Maste``CMAKE_INSTALL_PREFIX``  Installation prefix                                       System-dependent         ``/usr/local/lib``, ...
4610ff414cSEd Maste``BUILD_SHARED_LIBS``     Build as a shared library                                 ``OFF``                  ``ON``, ``OFF``
4710ff414cSEd Maste``HUGE_FUZZ``             :doc:`Fuzz test </tests>` with 8GB of data                ``OFF``                  ``ON``, ``OFF``
4810ff414cSEd Maste``SANE_MALLOC``           Assume ``malloc`` will refuse unreasonable allocations    ``OFF``                  ``ON``, ``OFF``
4910ff414cSEd Maste``COVERAGE``              Generate test coverage instrumentation                    ``OFF``                  ``ON``, ``OFF``
5010ff414cSEd Maste``WITH_TESTS``            Build unit tests (see :doc:`development`)                 ``OFF``                  ``ON``, ``OFF``
5110ff414cSEd Maste========================  =======================================================   ======================  =====================================================================================================================
5210ff414cSEd Maste
5310ff414cSEd MasteThe following configuration options will also be defined as macros [#]_ in ``<cbor/common.h>`` and can therefore be used in client code:
5410ff414cSEd Maste
5510ff414cSEd Maste========================  =======================================================   ======================  =====================================================================================================================
5610ff414cSEd MasteOption                    Meaning                                                   Default                 Possible values
5710ff414cSEd Maste------------------------  -------------------------------------------------------   ----------------------  ---------------------------------------------------------------------------------------------------------------------
5810ff414cSEd Maste``CBOR_PRETTY_PRINTER``   Include a pretty-printing routine                         ``ON``                  ``ON``, ``OFF``
5910ff414cSEd Maste``CBOR_BUFFER_GROWTH``    Factor for buffer growth & shrinking                       ``2``                    Decimals > 1
6010ff414cSEd Maste========================  =======================================================   ======================  =====================================================================================================================
6110ff414cSEd Maste
6210ff414cSEd Maste.. [#] ``ON`` & ``OFF`` will be translated to ``1`` and ``0`` using `cmakedefine <https://cmake.org/cmake/help/v3.2/command/configure_file.html?highlight=cmakedefine>`_.
6310ff414cSEd Maste
6410ff414cSEd MasteIf you want to pass other custom configuration options, please refer to `<http://www.cmake.org/Wiki/CMake_Useful_Variables>`_.
6510ff414cSEd Maste
66*5d3e7166SEd Maste.. warning::
67*5d3e7166SEd Maste    ``CBOR_CUSTOM_ALLOC`` has been `removed <https://github.com/PJK/libcbor/pull/237>`_.
68*5d3e7166SEd Maste    Custom allocators (historically a controlled by a build flag) are always enabled.
69*5d3e7166SEd Maste
7010ff414cSEd Maste**Building using make**
7110ff414cSEd Maste
7210ff414cSEd MasteCMake will generate a Makefile and other configuration files for the build. As a rule of thumb, you should configure the
7310ff414cSEd Mastebuild *outside of the source tree* in order to keep different configurations isolated. If you are unsure where to
7410ff414cSEd Masteexecute the build, just use a temporary directory:
7510ff414cSEd Maste
7610ff414cSEd Maste.. code-block:: bash
7710ff414cSEd Maste
7810ff414cSEd Maste  cd $(mktemp -d /tmp/cbor_build.XXXX)
7910ff414cSEd Maste
8010ff414cSEd MasteNow, assuming you are in the directory where you want to build, build libcbor as a **static library**:
8110ff414cSEd Maste
8210ff414cSEd Maste.. code-block:: bash
8310ff414cSEd Maste
8410ff414cSEd Maste  cmake -DCMAKE_BUILD_TYPE=Release path_to_libcbor_dir
8510ff414cSEd Maste  make cbor
8610ff414cSEd Maste
8710ff414cSEd Maste... or as a **dynamic library**:
8810ff414cSEd Maste
8910ff414cSEd Maste.. code-block:: bash
9010ff414cSEd Maste
9110ff414cSEd Maste  cmake -DCMAKE_BUILD_TYPE=Release  -DBUILD_SHARED_LIBS=ON path_to_libcbor_dir
9210ff414cSEd Maste  make cbor
9310ff414cSEd Maste
9410ff414cSEd MasteTo install locally:
9510ff414cSEd Maste
9610ff414cSEd Maste.. code-block:: bash
9710ff414cSEd Maste
9810ff414cSEd Maste  make install
9910ff414cSEd Maste
10010ff414cSEd MasteRoot permissions are required on most systems when using the default installation prefix.
10110ff414cSEd Maste
10210ff414cSEd Maste
10310ff414cSEd Maste**Portability**
10410ff414cSEd Maste
10510ff414cSEd Mastelibcbor is highly portable and works on both little- and big-endian systems regardless of the operating system. After building
10610ff414cSEd Masteon an exotic platform, you might wish to verify the result by running the :doc:`test suite </tests>`. If you encounter any problems, please
10710ff414cSEd Mastereport them to the `issue tracker <https://github.com/PJK/libcbor/issues>`_.
10810ff414cSEd Maste
10910ff414cSEd Mastelibcbor is known to successfully work on ARM Android devices. Cross-compilation is possible with ``arm-linux-gnueabi-gcc``.
11010ff414cSEd Maste
11110ff414cSEd Maste
11210ff414cSEd MasteLinking with libcbor
11310ff414cSEd Maste---------------------
11410ff414cSEd Maste
11510ff414cSEd MasteIf you include and linker paths include the directories to which libcbor has been installed, compiling programs that uses libcbor requires
11610ff414cSEd Masteno extra considerations.
11710ff414cSEd Maste
11810ff414cSEd MasteYou can verify that everything has been set up properly by creating a file with the following contents
11910ff414cSEd Maste
12010ff414cSEd Maste.. code-block:: c
12110ff414cSEd Maste
12210ff414cSEd Maste    #include <cbor.h>
12310ff414cSEd Maste    #include <stdio.h>
12410ff414cSEd Maste
12510ff414cSEd Maste    int main(int argc, char * argv[])
12610ff414cSEd Maste    {
12710ff414cSEd Maste        printf("Hello from libcbor %s\n", CBOR_VERSION);
12810ff414cSEd Maste    }
12910ff414cSEd Maste
13010ff414cSEd Maste
13110ff414cSEd Masteand compiling it
13210ff414cSEd Maste
13310ff414cSEd Maste.. code-block:: bash
13410ff414cSEd Maste
13510ff414cSEd Maste    cc hello_cbor.c -lcbor -o hello_cbor
13610ff414cSEd Maste
13710ff414cSEd Maste
13810ff414cSEd Mastelibcbor also comes with `pkg-config <https://wiki.freedesktop.org/www/Software/pkg-config/>`_ support. If you install libcbor with a custom prefix, you can use pkg-config to resolve the headers and objects:
13910ff414cSEd Maste
14010ff414cSEd Maste.. code-block:: bash
14110ff414cSEd Maste
14210ff414cSEd Maste    cc $(pkg-config --cflags libcbor) hello_cbor.c $(pkg-config --libs libcbor) -o hello_cbor
14310ff414cSEd Maste
14410ff414cSEd Maste
14510ff414cSEd Maste**A note on linkage**
14610ff414cSEd Maste
14710ff414cSEd Mastelibcbor is primarily intended to be linked statically. The shared library versioning scheme generally follows `SemVer <https://semver.org/>`_, but is irregular for the 0.X.Y development branch for historical reasons. The following version identifiers are used as a part of the SONAME (Linux) or the dylib `"Compatibility version" <https://developer.apple.com/library/archive/documentation/DeveloperTools/Conceptual/DynamicLibraries/100-Articles/CreatingDynamicLibraries.html>`_ (OS X):
14810ff414cSEd Maste
14910ff414cSEd Maste  - 0.Y for the 0.Y.Z branch. Patches are backwards compatible, minor releases are generally not and require re-compilation of any dependent code.
15010ff414cSEd Maste  - X for the X.Y.Z stable versions starting 1.X.Y. All minor release of the major version are backwards compatible.
15110ff414cSEd Maste
15210ff414cSEd Maste.. warning:: Please note that releases up to and including v0.6.0 `may export misleading .so/.dylib version number <https://github.com/PJK/libcbor/issues/52>`_.
15310ff414cSEd Maste
15410ff414cSEd Maste
15510ff414cSEd MasteTroubleshooting
15610ff414cSEd Maste---------------------
15710ff414cSEd Maste
15810ff414cSEd Maste**cbor.h not found**: The headers directory is probably not in your include path. First, verify the installation
15910ff414cSEd Mastelocation by checking the installation log. If you used make, it will look something like
16010ff414cSEd Maste
16110ff414cSEd Maste.. code-block:: text
16210ff414cSEd Maste
16310ff414cSEd Maste    ...
16410ff414cSEd Maste    -- Installing: /usr/local/include/cbor
16510ff414cSEd Maste    -- Installing: /usr/local/include/cbor/callbacks.h
16610ff414cSEd Maste    -- Installing: /usr/local/include/cbor/encoding.h
16710ff414cSEd Maste    ...
16810ff414cSEd Maste
16910ff414cSEd MasteMake sure that ``CMAKE_INSTALL_PREFIX`` (if you provided it) was correct. Including the path path during compilation should suffice, e.g.:
17010ff414cSEd Maste
17110ff414cSEd Maste.. code-block:: bash
17210ff414cSEd Maste
17310ff414cSEd Maste    cc -I/usr/local/include hello_cbor.c -lcbor -o hello_cbor
17410ff414cSEd Maste
17510ff414cSEd Maste
17610ff414cSEd Maste**cannot find -lcbor during linking**: Most likely the same problem as before. Include the installation directory in the
17710ff414cSEd Mastelinker shared path using ``-R``, e.g.:
17810ff414cSEd Maste
17910ff414cSEd Maste.. code-block:: bash
18010ff414cSEd Maste
18110ff414cSEd Maste    cc -Wl,-rpath,/usr/local/lib -lcbor -o hello_cbor
18210ff414cSEd Maste
18310ff414cSEd Maste**shared library missing during execution**: Verify the linkage using ``ldd``, ``otool``, or similar and adjust the compilation directives accordingly:
18410ff414cSEd Maste
18510ff414cSEd Maste.. code-block:: text
18610ff414cSEd Maste
18710ff414cSEd Maste    ⇒  ldd hello_cbor
18810ff414cSEd Maste        linux-vdso.so.1 =>  (0x00007ffe85585000)
18910ff414cSEd Maste        libcbor.so => /usr/local/lib/libcbor.so (0x00007f9af69da000)
19010ff414cSEd Maste        libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f9af65eb000)
19110ff414cSEd Maste        /lib64/ld-linux-x86-64.so.2 (0x00007f9af6be9000)
19210ff414cSEd Maste
19310ff414cSEd Maste**compilation failed**: If your compiler supports C99 yet the compilation has failed, please report the issue to the `issue tracker <https://github.com/PJK/libcbor/issues>`_.
194