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