Installation

Lindera C is a header, a library and a pkg-config file. A release has them in an archive for each system (Pre-built archives). From the sources, cargo-c builds the crate lindera-c and installs them under the names a C program expects (Build and install).

Pre-built archives

From the release after 7.3.0, each release has an archive of the library for each of these targets, lindera-c-<target>-<version>.zip:

TargetSystem
x86_64-unknown-linux-gnu, aarch64-unknown-linux-gnuLinux (glibc)
aarch64-apple-darwin, x86_64-apple-darwinmacOS
x86_64-pc-windows-msvc, aarch64-pc-windows-msvcWindows (MSVC)

There is no archive for Linux with musl. From the sources, cargo cinstall with --target <target> --library-type staticlib builds the static library (a shared library is not built for musl). A program that links it also needs an unwinder (lindera.pc asks for -lunwind), which the musl C toolchain has to provide.

An archive holds one directory, lindera-c-<target>-<version>, with the files cargo cinstall installs (see Build and install), LICENSE and README.md. The archives for Linux and macOS also have lib/pkgconfig/lindera-static.pc, the pkg-config file of the static library (see Linking the static library). The library is built with the default features (train and dependency) and no embedded dictionary.

# Example: the library for Linux on x86_64
curl -LO https://github.com/lindera/lindera/releases/download/v<version>/lindera-c-x86_64-unknown-linux-gnu-<version>.zip
unzip lindera-c-x86_64-unknown-linux-gnu-<version>.zip
export PKG_CONFIG_PATH="$PWD/lindera-c-x86_64-unknown-linux-gnu-<version>/lib/pkgconfig"

The directory can be anywhere: lindera.pc gives the paths of the directory it is in. The rest of this page applies with that directory in place of dist (see Compiling a program).

  • macOS. The install name of liblindera.dylib is @rpath/liblindera.dylib: a program that links it needs a run path to the directory of the library (-Wl,-rpath,<directory>/lib).
  • Windows. A program links the import library lib/lindera.dll.lib, and finds bin/lindera.dll through PATH.

Prerequisites

To build the library from the sources:

  • Rust toolchain 1.89 or later -- Install via rustup
  • cargo-c 0.10.28 or later -- cargo install --locked cargo-c
  • A C compiler, to compile a program with the library

Build and install

From the root of the Lindera repository:

cargo cinstall --release -p lindera-c --target-dir target/capi --prefix "$PWD/dist" --libdir lib

--prefix is where the files go. --libdir lib keeps the libraries in lib/: without it, cargo-c follows the system, and on Debian and Ubuntu puts them in lib/x86_64-linux-gnu/. --target-dir target/capi gives the build a directory of its own, apart from the other builds of the workspace.

On Linux, the command installs these files:

dist/include/lindera.h
dist/lib/liblindera.a
dist/lib/liblindera.so
dist/lib/pkgconfig/lindera.pc

On macOS the shared library is lib/liblindera.dylib. On Windows (MSVC) it is bin/lindera.dll, with its import library lib/lindera.dll.lib, and the static library is lib/lindera.lib.

The library is built with the default features (train and dependency) and no embedded dictionary; see Obtaining Dictionaries. To enable other feature flags, pass --features:

cargo cinstall --release -p lindera-c --features embed-ipadic --target-dir target/capi --prefix "$PWD/dist" --libdir lib

Compiling a program

With pkg-config, which reads lindera.pc:

export PKG_CONFIG_PATH="$PWD/dist/lib/pkgconfig"
cc -o tokenize lindera-c/examples/tokenize.c $(pkg-config --cflags --libs lindera)
LD_LIBRARY_PATH="$PWD/dist/lib" ./tokenize /path/to/lindera-ipadic

lindera-c/examples/tokenize.c is the program of the Quick Start. The last line tells the loader where the shared library is, as dist/ is not a directory it searches.

Linking the static library

A program that links liblindera.a also links what the Rust standard library needs. pkg-config --libs --static lindera prints the list, from the Libs.private of lindera.pc; on Linux:

-L<prefix>/lib -llindera -lgcc_s -lutil -lrt -lpthread -lm -ldl -lc

With lindera.pc, a linker takes the shared library when the two are in the same directory, as they are after cargo cinstall. lindera-static.pc names the static library by its path, with that list. The archives of a release for Linux and macOS have it; from the sources, a script writes it beside lindera.pc:

scripts/write_lindera_static_pc.sh "$PWD/dist"
cc -o tokenize lindera-c/examples/tokenize.c $(pkg-config --cflags --libs lindera-static)
./tokenize /path/to/lindera-ipadic

Without the file, name the library:

cc -o tokenize lindera-c/examples/tokenize.c $(pkg-config --cflags lindera) \
    "$PWD/dist/lib/liblindera.a" -lgcc_s -lutil -lrt -lpthread -lm -ldl -lc

Either way, the program runs without liblindera.so.

Obtaining Dictionaries

The default build embeds no dictionary. A program loads one from a directory, or the library is built with one.

Download from GitHub Releases

Pre-built dictionaries are available on the GitHub Releases page. Download and extract the dictionary archive; the archive holds one directory, lindera-ipadic for IPADIC:

# Example: download and extract the IPADIC dictionary
curl -LO https://github.com/lindera/lindera/releases/download/<version>/lindera-ipadic-<version>.zip
unzip lindera-ipadic-<version>.zip -d /path/to

The dictionary is then /path/to/lindera-ipadic, which is what a program passes to lindera_tokenizer_builder_set_dictionary or to lindera_dictionary_load.

Embed a dictionary

A library built with an embed-* feature holds the dictionary, and a program names it as embedded://<name>, for example embedded://ipadic. The library grows by the size of the dictionary.

Feature Flags

FeatureDescriptionDefault
trainThe training of a dictionary. Without it the functions are there and fail with LINDERA_STATUS_RUNTIMEEnabled
dependencyThe token filter japanese_dependency (bunsetsu dependency analysis)Enabled
embed-ipadicEmbed the Japanese dictionary (IPADIC)Disabled
embed-ipadic-neologdEmbed the Japanese dictionary (IPADIC NEologd)Disabled
embed-unidicEmbed the Japanese dictionary (UniDic)Disabled
embed-unidic-cwjEmbed the Japanese dictionary (UniDic CWJ)Disabled
embed-unidic-csjEmbed the Japanese dictionary (UniDic CSJ)Disabled
embed-sudachidictEmbed the Japanese dictionary (SudachiDict)Disabled
embed-jumandicEmbed the Japanese dictionary (JUMAN)Disabled
embed-ko-dicEmbed the Korean dictionary (ko-dic)Disabled
embed-cc-cedictEmbed the Chinese dictionary (CC-CEDICT)Disabled
embed-jiebaEmbed the Chinese dictionary (Jieba)Disabled
embed-cjkEmbed IPADIC, ko-dic and JiebaDisabled
embed-dependency-ipadic, embed-dependency-unidic, embed-dependency-unidic-cwj, embed-dependency-unidic-csjEmbed the model of the token filter japanese_dependency for that dictionaryDisabled

lindera_has_feature tells a program which of them the library has:

if (lindera_has_feature((const uint8_t *)"embed-ipadic", 12)) {
    /* embedded://ipadic can be loaded */
}

Development

The Makefile has the targets of the crate:

make setup-lindera-c-tools   # cbindgen and cargo-c, at the versions the Makefile pins
make header-lindera-c        # make lindera-c/include/lindera.h again from the sources
make check-header-lindera-c  # fail when the header is stale, as CI does
make test-lindera-c          # the Rust unit tests, then the C tests (needs CMake 3.22 or later)
make lint-lindera-c          # clippy

The header is made by cbindgen from the sources and kept in the repository. After a function is added or changed, run make header-lindera-c and add the function to lindera-c/abi/symbols.txt; a unit test compares both with the sources.

The C tests (lindera-c/tests/c) are compiled against the library as cargo cinstall installs it, and run with the shared and with the static library. On Linux they run a second time with AddressSanitizer and UndefinedBehaviorSanitizer.