xref: /netbsd-src/external/mit/libcbor/dist/doc/source/getting_started.rst (revision 5dd36a3bc8bf2a9dec29ceb6349550414570c447)
1*5dd36a3bSchristosGetting started
2*5dd36a3bSchristos==========================
3*5dd36a3bSchristos
4*5dd36a3bSchristosPre-built Linux packages are distributed from `the libcbor website <http://libcbor.org/>`_.
5*5dd36a3bSchristos
6*5dd36a3bSchristosOS X users can use `Homebrew <http://brew.sh/>`_:
7*5dd36a3bSchristos
8*5dd36a3bSchristos.. code-block:: bash
9*5dd36a3bSchristos
10*5dd36a3bSchristos    brew tap pjk/libcbor
11*5dd36a3bSchristos    brew install libcbor
12*5dd36a3bSchristos
13*5dd36a3bSchristosFor other platforms, you will need to compile it from source.
14*5dd36a3bSchristos
15*5dd36a3bSchristosBuilding & installing libcbor
16*5dd36a3bSchristos------------------------------
17*5dd36a3bSchristos
18*5dd36a3bSchristosPrerequisites:
19*5dd36a3bSchristos - C99 compiler
20*5dd36a3bSchristos - CMake_ 2.8 or newer (might also be called ``cmakesetup``, ``cmake-gui`` or ``ccmake`` depending on the installed version and system)
21*5dd36a3bSchristos - C build system CMake can target (make, Apple Xcode, MinGW, ...)
22*5dd36a3bSchristos .. _CMake: http://cmake.org/
23*5dd36a3bSchristos
24*5dd36a3bSchristos.. note:: As of May 2015, not even the 2015 release candidate of Visual Studio supports C99. While CMake will be happy to generate a VS solution that you can play with, libcbor currently cannot be compiled using the MSVC toolchain. `ICC <https://software.intel.com/en-us/c-compilers>`_, GCC under `Cygwin <https://www.cygwin.com/>`_, and `MinGW's <http://www.mingw.org/>`_ GCC will all work. The MinGW build process is described below.
25*5dd36a3bSchristos
26*5dd36a3bSchristos**Configuration options**
27*5dd36a3bSchristos
28*5dd36a3bSchristosA handful of configuration flags can be passed to `cmake`. The following table lists libcbor compile-time directives and several important generic flags.
29*5dd36a3bSchristos
30*5dd36a3bSchristos========================  =======================================================   ======================  =====================================================================================================================
31*5dd36a3bSchristosOption                    Meaning                                                   Default                 Possible values
32*5dd36a3bSchristos------------------------  -------------------------------------------------------   ----------------------  ---------------------------------------------------------------------------------------------------------------------
33*5dd36a3bSchristos``CMAKE_C_COMPILER``      C compiler to use                                         ``cc``                   ``gcc``, ``clang``, ``clang-3.5``, ...
34*5dd36a3bSchristos``CMAKE_INSTALL_PREFIX``  Installation prefix                                       System-dependent         ``/usr/local/lib``, ...
35*5dd36a3bSchristos``HUGE_FUZZ``             :doc:`Fuzz test </tests>` with 8GB of data                ``OFF``                   ``ON``, ``OFF``
36*5dd36a3bSchristos``SANE_MALLOC``           Assume ``malloc`` will refuse unreasonable allocations    ``OFF``                   ``ON``, ``OFF``
37*5dd36a3bSchristos``COVERAGE``              Generate test coverage instrumentation                    ``OFF``                   ``ON``, ``OFF``
38*5dd36a3bSchristos``WITH_TESTS``            Build unit tests (see :doc:`development`)                 ``OFF``                   ``ON``, ``OFF``
39*5dd36a3bSchristos========================  =======================================================   ======================  =====================================================================================================================
40*5dd36a3bSchristos
41*5dd36a3bSchristosThe following configuration options will also be defined as macros[#]_ in ``<cbor/common.h>`` and can therefore be used in client code:
42*5dd36a3bSchristos
43*5dd36a3bSchristos========================  =======================================================   ======================  =====================================================================================================================
44*5dd36a3bSchristosOption                    Meaning                                                   Default                 Possible values
45*5dd36a3bSchristos------------------------  -------------------------------------------------------   ----------------------  ---------------------------------------------------------------------------------------------------------------------
46*5dd36a3bSchristos``CBOR_CUSTOM_ALLOC``     Enable custom allocator support                           ``OFF``                  ``ON``, ``OFF``
47*5dd36a3bSchristos``CBOR_PRETTY_PRINTER``   Include a pretty-printing routine                         ``ON``                  ``ON``, ``OFF``
48*5dd36a3bSchristos``CBOR_BUFFER_GROWTH``    Factor for buffer growth & shrinking                       ``2``                    Decimals > 1
49*5dd36a3bSchristos========================  =======================================================   ======================  =====================================================================================================================
50*5dd36a3bSchristos
51*5dd36a3bSchristos.. [#] ``ON`` & ``OFF`` will be translated to ``1`` and ``0`` using `cmakedefine <https://cmake.org/cmake/help/v3.2/command/configure_file.html?highlight=cmakedefine>`_.
52*5dd36a3bSchristos
53*5dd36a3bSchristosIf you want to pass other custom configuration options, please refer to `<http://www.cmake.org/Wiki/CMake_Useful_Variables>`_.
54*5dd36a3bSchristos
55*5dd36a3bSchristos**Building using make**
56*5dd36a3bSchristos
57*5dd36a3bSchristosCMake will generate a Makefile and other configuration files for the build. As a rule of thumb, you should configure the
58*5dd36a3bSchristosbuild *outside of the source tree* in order to keep different configurations isolated. If you are unsure where to
59*5dd36a3bSchristosexecute the build, just use a temporary directory:
60*5dd36a3bSchristos
61*5dd36a3bSchristos.. code-block:: bash
62*5dd36a3bSchristos
63*5dd36a3bSchristos  cd $(mktemp -d /tmp/cbor_build.XXXX)
64*5dd36a3bSchristos
65*5dd36a3bSchristosNow, assuming you are in the directory where you want to build, execute the following to configure the build and run make
66*5dd36a3bSchristos
67*5dd36a3bSchristos.. code-block:: bash
68*5dd36a3bSchristos
69*5dd36a3bSchristos  cmake -DCMAKE_BUILD_TYPE=Release path_to_libcbor_dir
70*5dd36a3bSchristos  make cbor cbor_shared
71*5dd36a3bSchristos
72*5dd36a3bSchristosBoth the shared (``libcbor.so``) and the static (``libcbor.a``) libraries should now be in the ``src`` subdirectory.
73*5dd36a3bSchristos
74*5dd36a3bSchristosIn order to install the libcbor headers and libraries, the usual
75*5dd36a3bSchristos
76*5dd36a3bSchristos.. code-block:: bash
77*5dd36a3bSchristos
78*5dd36a3bSchristos  make install
79*5dd36a3bSchristos
80*5dd36a3bSchristosis what your're looking for. Root permissions are required on most systems when using the default installation prefix.
81*5dd36a3bSchristos
82*5dd36a3bSchristos
83*5dd36a3bSchristos**Portability**
84*5dd36a3bSchristos
85*5dd36a3bSchristoslibcbor is highly portable and works on both little- and big-endian systems regardless of the operating system. After building
86*5dd36a3bSchristoson an exotic platform, you might wish to verify the result by running the :doc:`test suite </tests>`. If you encounter any problems, please
87*5dd36a3bSchristosreport them to the `issue tracker <https://github.com/PJK/libcbor/issues>`_.
88*5dd36a3bSchristos
89*5dd36a3bSchristoslibcbor is known to successfully work on ARM Android devices. Cross-compilation is possible with ``arm-linux-gnueabi-gcc``.
90*5dd36a3bSchristos
91*5dd36a3bSchristos
92*5dd36a3bSchristosLinking with libcbor
93*5dd36a3bSchristos---------------------
94*5dd36a3bSchristos
95*5dd36a3bSchristosIf you include and linker paths include the directories to which libcbor has been installed, compiling programs that uses libcbor requires
96*5dd36a3bSchristosno extra considerations.
97*5dd36a3bSchristos
98*5dd36a3bSchristosYou can verify that everything has been set up properly by creating a file with the following contents
99*5dd36a3bSchristos
100*5dd36a3bSchristos.. code-block:: c
101*5dd36a3bSchristos
102*5dd36a3bSchristos    #include <cbor.h>
103*5dd36a3bSchristos    #include <stdio.h>
104*5dd36a3bSchristos
105*5dd36a3bSchristos    int main(int argc, char * argv[])
106*5dd36a3bSchristos    {
107*5dd36a3bSchristos        printf("Hello from libcbor %s\n", CBOR_VERSION);
108*5dd36a3bSchristos    }
109*5dd36a3bSchristos
110*5dd36a3bSchristos
111*5dd36a3bSchristosand compiling it
112*5dd36a3bSchristos
113*5dd36a3bSchristos.. code-block:: bash
114*5dd36a3bSchristos
115*5dd36a3bSchristos    cc hello_cbor.c -lcbor -o hello_cbor
116*5dd36a3bSchristos
117*5dd36a3bSchristos
118*5dd36a3bSchristoslibcbor 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:
119*5dd36a3bSchristos
120*5dd36a3bSchristos.. code-block:: bash
121*5dd36a3bSchristos
122*5dd36a3bSchristos    cc $(pkg-config --cflags libcbor) hello_cbor.c $(pkg-config --libs libcbor) -o hello_cbor
123*5dd36a3bSchristos
124*5dd36a3bSchristos
125*5dd36a3bSchristosMinGW build instructions
126*5dd36a3bSchristos---------------------------
127*5dd36a3bSchristosPrerequisites:
128*5dd36a3bSchristos - MinGW
129*5dd36a3bSchristos - CMake GUI
130*5dd36a3bSchristos
131*5dd36a3bSchristosFirst of all, create a folder that will be used for the output. For this demonstration, we will use ``cbor_out``. Start CMake and select the source path and the destination folder.
132*5dd36a3bSchristos
133*5dd36a3bSchristos.. image:: img/win_1.png
134*5dd36a3bSchristos
135*5dd36a3bSchristosThen hit the 'Configure' button. You will be prompted to select the build system:
136*5dd36a3bSchristos
137*5dd36a3bSchristos.. image:: img/win_2.png
138*5dd36a3bSchristos
139*5dd36a3bSchristosChoose MinGW and confirm.
140*5dd36a3bSchristos
141*5dd36a3bSchristos.. note:: If you select Visual Studio at this point, a MSVC project will be generated for you. This is useful if you just want to browse through the source code.
142*5dd36a3bSchristos
143*5dd36a3bSchristosYou can then adjust the build options. The defaults will work just fine. Hit 'Generate' when you are done.
144*5dd36a3bSchristos
145*5dd36a3bSchristos.. image:: img/win_3.png
146*5dd36a3bSchristos
147*5dd36a3bSchristosYou can then adjust the build options. The defaults will work just fine. Hit 'Generate' when you are done.
148*5dd36a3bSchristos
149*5dd36a3bSchristosOpen the shell, navigate to the output directory, and run ``mingw32-make cbor cbor_shared``.
150*5dd36a3bSchristos
151*5dd36a3bSchristos.. image:: img/win_4.png
152*5dd36a3bSchristos
153*5dd36a3bSchristos*libcbor* will be built and your ``.dll`` should be ready at this point
154*5dd36a3bSchristos
155*5dd36a3bSchristos.. image:: img/win_5.png
156*5dd36a3bSchristos
157*5dd36a3bSchristosFeel free to also try building and running some of the examples, e.g. ``mingw32-make sort``
158*5dd36a3bSchristos
159*5dd36a3bSchristos.. image:: img/win_6.png
160*5dd36a3bSchristos
161*5dd36a3bSchristos
162*5dd36a3bSchristosTroubleshooting
163*5dd36a3bSchristos---------------------
164*5dd36a3bSchristos
165*5dd36a3bSchristos**cbor.h not found**: The headers directory is probably not in your include path. First, verify the installation
166*5dd36a3bSchristoslocation by checking the installation log. If you used make, it will look something like
167*5dd36a3bSchristos
168*5dd36a3bSchristos.. code-block:: text
169*5dd36a3bSchristos
170*5dd36a3bSchristos    ...
171*5dd36a3bSchristos    -- Installing: /usr/local/include/cbor
172*5dd36a3bSchristos    -- Installing: /usr/local/include/cbor/callbacks.h
173*5dd36a3bSchristos    -- Installing: /usr/local/include/cbor/encoding.h
174*5dd36a3bSchristos    ...
175*5dd36a3bSchristos
176*5dd36a3bSchristosMake sure that ``CMAKE_INSTALL_PREFIX`` (if you provided it) was correct. Including the path path during compilation should suffice, e.g.:
177*5dd36a3bSchristos
178*5dd36a3bSchristos.. code-block:: bash
179*5dd36a3bSchristos
180*5dd36a3bSchristos    cc -I/usr/local/include hello_cbor.c -lcbor -o hello_cbor
181*5dd36a3bSchristos
182*5dd36a3bSchristos
183*5dd36a3bSchristos**cannot find -lcbor during linking**: Most likely the same problem as before. Include the installation directory in the
184*5dd36a3bSchristoslinker shared path using ``-R``, e.g.:
185*5dd36a3bSchristos
186*5dd36a3bSchristos.. code-block:: bash
187*5dd36a3bSchristos
188*5dd36a3bSchristos    cc -Wl,-rpath,/usr/local/lib -lcbor -o hello_cbor
189*5dd36a3bSchristos
190*5dd36a3bSchristos**shared library missing during execution**: Verify the linkage using ``ldd``, ``otool``, or similar and adjust the compilation directives accordingly:
191*5dd36a3bSchristos
192*5dd36a3bSchristos.. code-block:: text
193*5dd36a3bSchristos
194*5dd36a3bSchristos    ⇒  ldd hello_cbor
195*5dd36a3bSchristos        linux-vdso.so.1 =>  (0x00007ffe85585000)
196*5dd36a3bSchristos        libcbor.so => /usr/local/lib/libcbor.so (0x00007f9af69da000)
197*5dd36a3bSchristos        libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f9af65eb000)
198*5dd36a3bSchristos        /lib64/ld-linux-x86-64.so.2 (0x00007f9af6be9000)
199*5dd36a3bSchristos
200*5dd36a3bSchristos**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>`_.
201