Getting started in C and C++
cadkit-capi exposes cadkit through a small C ABI (cadkit.h) and a header-only C++17
wrapper (cadkit.hpp). The header is the contract: functions are only added in minor
versions, and existing signatures, enum values and struct layouts change only with a major
version.
Get the library
Section titled “Get the library”Each GitHub release has a
cadkit-capi-<target> archive for x86_64-unknown-linux-gnu, aarch64-unknown-linux-gnu
(both built against glibc 2.28), aarch64-apple-darwin, x86_64-apple-darwin and
x86_64-pc-windows-msvc. It contains include/ (cadkit.h, cadkit.hpp), lib/ with the
shared and the static library, the license files and the API reference. Checksums are listed in
SHA256SUMS on the release.
Read a drawing and export SVG
Section titled “Read a drawing and export SVG”#include <stdio.h>#include "cadkit.h"
int main(void) { cadkit_document *doc = NULL; cadkit_status status = cadkit_read_file("plan.dxf", NULL, &doc); if (status != CADKIT_OK) { fprintf(stderr, "%s: %s\n", cadkit_status_string(status), cadkit_last_error_message()); return 1; } cadkit_info info; if (cadkit_document_info(doc, &info) == CADKIT_OK) printf("%llu models, %llu layers, %llu entities\n", (unsigned long long)info.model_count, (unsigned long long)info.layer_count, (unsigned long long)info.entity_count);
cadkit_svg_options options; cadkit_svg_options_init(&options); options.width_px = 1200; char *svg = NULL; if (cadkit_document_to_svg(doc, &options, &svg) == CADKIT_OK) { FILE *out = fopen("plan.svg", "wb"); if (out) { fputs(svg, out); fclose(out); } cadkit_string_free(svg); } cadkit_document_free(doc); return 0;}gcc -I cadkit-capi/include quickstart.c -L cadkit-capi/lib -lcadkit_capi -o quickstartLD_LIBRARY_PATH=cadkit-capi/lib ./quickstart # or link with an rpathPrefer the shared library: the static one also needs the platform libraries Rust’s standard
library uses. With MSVC, link cadkit_capi.dll.lib and ship cadkit_capi.dll next to the
executable.
The conventions in brief: every fallible function returns a cadkit_status and writes its
result through an out-pointer; cadkit_last_error_message() explains the last failure on the
calling thread; strings are UTF-8. Release documents with cadkit_document_free, strings with
cadkit_string_free and byte buffers with cadkit_bytes_free. No Rust panic crosses the
boundary. The full rules are in the C and C++ API reference.
#include <fstream>#include <iostream>#include "cadkit.hpp"
int main() { try { auto doc = cadkit::Document::read_file("plan.dxf"); std::cout << doc.info().entity_count << " entities\n"; cadkit::SvgOptions svg; svg.width_px = 1200; std::ofstream("plan.svg") << doc.to_svg(svg); std::ofstream("plan-r2000.dxf") << doc.to_dxf(CADKIT_DXF_R2000); } catch (const cadkit::Error& e) { std::cerr << e.what() << " (status " << static_cast<int>(e.status()) << ")\n"; return 1; }}cadkit::Document is move-only and frees its handle in the destructor; failures throw
cadkit::Error, which derives from std::runtime_error.
With a prebuilt archive, point CADKIT_DIR at the unpacked folder:
cmake_minimum_required(VERSION 3.18)project(quickstart LANGUAGES C)
# CADKIT_DIR: an unpacked cadkit-capi-<target> archive from GitHub Releases (include/ and lib/).set(CADKIT_DIR "" CACHE PATH "Unpacked cadkit-capi archive")# On Windows, name the import library: NAMES cadkit_capi.dll finds cadkit_capi.dll.lib.find_library(CADKIT_LIBRARY NAMES cadkit_capi PATHS "${CADKIT_DIR}/lib" NO_DEFAULT_PATH REQUIRED)
add_executable(quickstart quickstart.c)target_include_directories(quickstart PRIVATE "${CADKIT_DIR}/include")target_link_libraries(quickstart PRIVATE "${CADKIT_LIBRARY}")cmake -S . -B build -DCADKIT_DIR="$PWD/cadkit-capi"cmake --build buildBuilding from a checkout instead, cargo build -p cadkit-capi --release produces the
libraries in target/release/, and cmake --install of crates/cadkit-capi installs a CMake
package (find_package(cadkit CONFIG REQUIRED), target cadkit::cadkit) and a pkg-config file.
See Install for CMake and pkg-config consumers.
Next steps
Section titled “Next steps”- Convert DXF to SVG, inspect a drawing and handle untrusted files
- The complete C and C++ API reference