{"id":21631921,"url":"https://github.com/xiangpenghao/cxx-cmake-example","last_synced_at":"2026-03-06T00:31:41.583Z","repository":{"id":40624606,"uuid":"303966319","full_name":"XiangpengHao/cxx-cmake-example","owner":"XiangpengHao","description":"An example repo to setup cxx (Rust FFI library) with the CMake build system.","archived":false,"fork":false,"pushed_at":"2024-12-05T15:03:52.000Z","size":20,"stargazers_count":98,"open_issues_count":7,"forks_count":21,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-10-12T06:08:12.860Z","etag":null,"topics":["cmake","cpp","cxx","ffi","rust"],"latest_commit_sha":null,"homepage":"","language":"CMake","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/XiangpengHao.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-10-14T09:30:37.000Z","updated_at":"2025-09-11T16:19:26.000Z","dependencies_parsed_at":"2024-12-29T11:00:18.914Z","dependency_job_id":null,"html_url":"https://github.com/XiangpengHao/cxx-cmake-example","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/XiangpengHao/cxx-cmake-example","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/XiangpengHao%2Fcxx-cmake-example","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/XiangpengHao%2Fcxx-cmake-example/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/XiangpengHao%2Fcxx-cmake-example/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/XiangpengHao%2Fcxx-cmake-example/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/XiangpengHao","download_url":"https://codeload.github.com/XiangpengHao/cxx-cmake-example/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/XiangpengHao%2Fcxx-cmake-example/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":30156285,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-05T22:39:40.138Z","status":"ssl_error","status_checked_at":"2026-03-05T22:39:24.771Z","response_time":93,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cmake","cpp","cxx","ffi","rust"],"created_at":"2024-11-25T02:15:46.246Z","updated_at":"2026-03-06T00:31:41.545Z","avatar_url":"https://github.com/XiangpengHao.png","language":"CMake","funding_links":[],"categories":[],"sub_categories":[],"readme":"# CXX with CMake build system\n![CXX CMake CI](https://github.com/XiangpengHao/cxx-cmake-example/workflows/CXX%20CMake%20CI/badge.svg)\n\n\nThis is an example repo to setup [cxx](https://github.com/dtolnay/cxx) with the cmake build system.\n\nThe official [demo](https://github.com/dtolnay/cxx/tree/master/demo) used `cargo` to orchestrate the two build systems and place the `main` function inside the rust project.\n\nIn a lot of other applications, however, we want to embed rust into a large cpp project where we don't have a chance to choose build systems.\nThis template repo shows how to use cmake with cxx.\n\n\nThe cmake files do the following things:\n1. Call `cargo build [--release]` to build the static library and the necessary c++ header/source files\n2. Create a library from the cxx generated source and link to the rust static library\n3. Link and include the libraray to the corresponding targets\n\n## Demo \n```bash\ndocker build . --no-cache --progress plain\n```\n\n## Cross-language LTO (linking time optimization)\n\n### Why?\nCalling rust function from c++ (and vice versa) is not zero-overhead because the LLVM optimizer by default will not optimize across shared libraries, let alone across different languages.\n\n[LTO](https://llvm.org/docs/LinkTimeOptimization.html) (linking time optimization) allows the optimizers to perform optimization during linking time (instead of compile time), thus enables cross library optimization.\n\n### How?\n```bash\ncmake -DENABLE_LTO=ON -DCMAKE_BUILD_TYPE=Release ..\nmake -j\n./main\n```\n\nThe `-DENABLE_LTO=ON` will compile and link both libraries with proper parameters.\nNote that cross language LTO between rust and c/c++ is only possible with clang toolchain, \nmeaning that you need to have a very recent clang/lld installed.\n\n## Example output\n\n### With LTO\n```\nPoints {\n    x: [\n        1,\n        2,\n        3,\n    ],\n    y: [\n        4,\n        5,\n        6,\n    ],\n}\n\"cpp expert!\"\nCalling rust function, time elapsed: 100 ns.\nCalling c++ function, time elapsed: 100 ns.\n```\n\n#### Without LTO\n```\nPoints {\n    x: [\n        1,\n        2,\n        3,\n    ],\n    y: [\n        4,\n        5,\n        6,\n    ],\n}\n\"cpp expert!\"\nCalling rust function, time elapsed: 1176600 ns.\nCalling c++ function, time elapsed: 100 ns.\n```\n\n\n## Credits\nThe cmake files are largely inspired by [Using unsafe for Fun and Profit](https://github.com/Michael-F-Bryan/rust-ffi-guide).\n\nI learned a lot about cross language LTO from this post: [Closing the gap: cross-language LTO between Rust and C/C++](https://blog.llvm.org/2019/09/closing-gap-cross-language-lto-between.html)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxiangpenghao%2Fcxx-cmake-example","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fxiangpenghao%2Fcxx-cmake-example","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxiangpenghao%2Fcxx-cmake-example/lists"}