1Getting started 2========================== 3 4Pre-built Linux packages are available in most mainstream distributions 5 6**Ubuntu, Debian, etc.**: 7 8.. code-block:: bash 9 10 apt-get install libcbor-dev 11 12**Fedora, openSUSE, etc.**: 13 14.. code-block:: bash 15 16 yum install libcbor-devel 17 18 19**OS X** users can use `Homebrew <http://brew.sh/>`_: 20 21.. code-block:: bash 22 23 brew install libcbor 24 25For other platforms, you will need to compile it from source. 26 27Building & installing libcbor 28------------------------------ 29 30Prerequisites: 31 - C99 compiler 32 - CMake_ 2.8 or newer (might also be called ``cmakesetup``, ``cmake-gui`` or ``ccmake`` depending on the installed version and system) 33 - C build system CMake can target (make, Apple Xcode, MinGW, ...) 34 35.. _CMake: http://cmake.org/ 36 37**Configuration options** 38 39A handful of configuration flags can be passed to `cmake`. The following table lists libcbor compile-time directives and several important generic flags. 40 41.. list-table:: 42 :header-rows: 1 43 44 * - Option 45 - Meaning 46 - Default 47 - Possible values 48 * - ``CMAKE_C_COMPILER`` 49 - C compiler to use 50 - ``cc`` 51 - ``gcc``, ``clang``, ``clang-3.5``, ... 52 * - ``CMAKE_INSTALL_PREFIX`` 53 - Installation prefix 54 - System-dependent 55 - ``/usr/local/lib``, ... 56 * - ``CMAKE_INTERPROCEDURAL_OPTIMIZATION`` 57 - Enable LTO (if supported) 58 - System-dependent 59 - ``ON``, ``OFF`` 60 * - ``BUILD_SHARED_LIBS`` 61 - Build as a shared library 62 - ``OFF`` 63 - ``ON``, ``OFF`` 64 * - ``HUGE_FUZZ`` 65 - :doc:`Fuzz test </tests>` with 8GB of data 66 - ``OFF`` 67 - ``ON``, ``OFF`` 68 * - ``SANE_MALLOC`` 69 - Assume ``malloc`` will refuse unreasonable allocations 70 - ``OFF`` 71 - ``ON``, ``OFF`` 72 * - ``COVERAGE`` 73 - Generate test coverage instrumentation 74 - ``OFF`` 75 - ``ON``, ``OFF`` 76 * - ``WITH_TESTS`` 77 - Build unit tests (see :doc:`development`) 78 - ``OFF`` 79 - ``ON``, ``OFF`` 80 81 82The following configuration options will also be defined as macros [#]_ in ``<cbor/common.h>`` and can therefore be used in client code: 83 84.. list-table:: 85 :header-rows: 1 86 87 * - Option 88 - Meaning 89 - Default 90 - Possible values 91 * - ``CBOR_PRETTY_PRINTER`` 92 - Include a pretty-printing routine 93 - ``ON`` 94 - ``ON``, ``OFF`` 95 * - ``CBOR_BUFFER_GROWTH`` 96 - Factor for buffer growth & shrinking 97 - ``2`` 98 - Decimals > 1 99 100 101.. [#] ``ON`` & ``OFF`` will be translated to ``1`` and ``0`` using `cmakedefine <https://cmake.org/cmake/help/v3.2/command/configure_file.html?highlight=cmakedefine>`_. 102 103If you want to pass other custom configuration options, please refer to `<http://www.cmake.org/Wiki/CMake_Useful_Variables>`_. 104 105.. note:: 106 When ``CMAKE_INTERPROCEDURAL_OPTIMIZATION`` is enabled, the generated static library (`libcbor.a`) should be used with an LTO-enabled linker downstream. On LLVM toolchains without bitcode embedding (`-fembed-bitcode`), the archive will contain LLVM IR only and linking without LTO `will not work <https://github.com/PJK/libcbor/issues/372>`_. 107 108.. warning:: 109 ``CBOR_CUSTOM_ALLOC`` has been `removed <https://github.com/PJK/libcbor/pull/237>`_. 110 Custom allocators (historically a controlled by a build flag) are always enabled. 111 112**Building using make** 113 114CMake will generate a Makefile and other configuration files for the build. As a rule of thumb, you should configure the 115build *outside of the source tree* in order to keep different configurations isolated. If you are unsure where to 116execute the build, just use a temporary directory: 117 118.. code-block:: bash 119 120 cd $(mktemp -d /tmp/cbor_build.XXXX) 121 122Now, assuming you are in the directory where you want to build, build libcbor as a **static library**: 123 124.. code-block:: bash 125 126 cmake -DCMAKE_BUILD_TYPE=Release path_to_libcbor_dir 127 make cbor 128 129... or as a **dynamic library**: 130 131.. code-block:: bash 132 133 cmake -DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS=ON path_to_libcbor_dir 134 make cbor 135 136To install locally: 137 138.. code-block:: bash 139 140 make install 141 142Root permissions are required on most systems when using the default installation prefix. 143 144 145**Portability** 146 147libcbor is highly portable and works on both little- and big-endian systems regardless of the operating system. After building 148on an exotic platform, you might wish to verify the result by running the :doc:`test suite </tests>`. If you encounter any problems, please 149report them to the `issue tracker <https://github.com/PJK/libcbor/issues>`_. 150 151libcbor is known to successfully work on ARM Android devices. Cross-compilation is possible with ``arm-linux-gnueabi-gcc``. 152 153 154Linking with libcbor 155--------------------- 156 157If you include and linker paths include the directories to which libcbor has been installed, compiling programs that uses libcbor requires 158no extra considerations. 159 160You can verify that everything has been set up properly by creating a file with the following contents 161 162.. code-block:: c 163 164 #include <cbor.h> 165 #include <stdio.h> 166 167 int main(int argc, char * argv[]) 168 { 169 printf("Hello from libcbor %s\n", CBOR_VERSION); 170 } 171 172 173and compiling it 174 175.. code-block:: bash 176 177 cc hello_cbor.c -lcbor -o hello_cbor 178 179 180libcbor 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: 181 182.. code-block:: bash 183 184 cc $(pkg-config --cflags libcbor) hello_cbor.c $(pkg-config --libs libcbor) -o hello_cbor 185 186 187**A note on linkage** 188 189libcbor 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): 190 191 - 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. 192 - X for the X.Y.Z stable versions starting 1.X.Y. All minor release of the major version are backwards compatible. 193 194.. 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>`_. 195 196 197Troubleshooting 198--------------------- 199 200**cbor.h not found**: The headers directory is probably not in your include path. First, verify the installation 201location by checking the installation log. If you used make, it will look something like 202 203.. code-block:: text 204 205 ... 206 -- Installing: /usr/local/include/cbor 207 -- Installing: /usr/local/include/cbor/callbacks.h 208 -- Installing: /usr/local/include/cbor/encoding.h 209 ... 210 211Make sure that ``CMAKE_INSTALL_PREFIX`` (if you provided it) was correct. Including the path path during compilation should suffice, e.g.: 212 213.. code-block:: bash 214 215 cc -I/usr/local/include hello_cbor.c -lcbor -o hello_cbor 216 217 218**cannot find -lcbor during linking**: Most likely the same problem as before. Include the installation directory in the 219linker shared path using ``-R``, e.g.: 220 221.. code-block:: bash 222 223 cc -Wl,-rpath,/usr/local/lib -lcbor -o hello_cbor 224 225**shared library missing during execution**: Verify the linkage using ``ldd``, ``otool``, or similar and adjust the compilation directives accordingly: 226 227.. code-block:: text 228 229 ⇒ ldd hello_cbor 230 linux-vdso.so.1 => (0x00007ffe85585000) 231 libcbor.so => /usr/local/lib/libcbor.so (0x00007f9af69da000) 232 libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f9af65eb000) 233 /lib64/ld-linux-x86-64.so.2 (0x00007f9af6be9000) 234 235**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>`_. 236