{"id":626,"date":"2024-10-17T02:00:49","date_gmt":"2024-10-17T02:00:49","guid":{"rendered":"https:\/\/carson.fenimorefamily.com\/?p=626"},"modified":"2024-10-17T02:00:50","modified_gmt":"2024-10-17T02:00:50","slug":"rust-cxx-crate-linking-to-external-c-shared-lib","status":"publish","type":"post","link":"https:\/\/carson.fenimorefamily.com\/?p=626","title":{"rendered":"rust cxx crate: linking to external c++ shared lib"},"content":{"rendered":"\n<p>I have a custom c++ shared library.  I also have a rust application that needs to use that library.  I had no problem making a build.rs using bindgen, however I wanted a little better c++ support so I turned to the cxx crate. All went well until link time as I was getting &#8220;unresolved symbols&#8221;&#8230; but the linker output showed the library WAS being linked in! What gives?!<\/p>\n\n\n\n<p>First, here was my source BEFORE the fix:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    \/\/ linking to c++ \"libcamlite\"\n    println!(\"cargo:rustc-link-lib=dylib=camlite\");\n    \/\/ .... code removed for brevity\n    cxx_build::bridge(\"src\/main.rs\")\n        .file(\"cxx\/wrap.cxx\")\n        .std(\"c++20\")\n        .debug(true)\n        .compile(\"libcamlite-rs\");\n<\/code><\/pre>\n\n\n\n<p>The solution? Behold!<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\n    cxx_build::bridge(\"src\/main.rs\")\n        .file(\"cxx\/wrap.cxx\")\n        .std(\"c++20\")\n        .debug(true)\n        .compile(\"libcamlite-rs\");\n    \/\/ .... code removed for brevity\n    \/\/ linking to c++ \"libcamlite\"\n    println!(\"cargo:rustc-link-lib=dylib=camlite\");\n<\/code><\/pre>\n\n\n\n<p>YES&#8230; I simply moved the cxx::build command above the link directive&#8230;! Seems like a bug, eh? And I don&#8217;t have time to care much more about it &#8211; but hope this helps someone.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I have a custom c++ shared library. I also have a rust application that needs to use that library. I had no problem making a build.rs using bindgen, however I wanted a little better c++ support so I turned to the cxx crate. All went well until link time as I was getting &#8220;unresolved symbols&#8221;&#8230; &hellip; <a href=\"https:\/\/carson.fenimorefamily.com\/?p=626\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">rust cxx crate: linking to external c++ shared lib<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[40],"tags":[],"class_list":["post-626","post","type-post","status-publish","format-standard","hentry","category-rust"],"_links":{"self":[{"href":"https:\/\/carson.fenimorefamily.com\/index.php?rest_route=\/wp\/v2\/posts\/626","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/carson.fenimorefamily.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/carson.fenimorefamily.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/carson.fenimorefamily.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/carson.fenimorefamily.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=626"}],"version-history":[{"count":1,"href":"https:\/\/carson.fenimorefamily.com\/index.php?rest_route=\/wp\/v2\/posts\/626\/revisions"}],"predecessor-version":[{"id":627,"href":"https:\/\/carson.fenimorefamily.com\/index.php?rest_route=\/wp\/v2\/posts\/626\/revisions\/627"}],"wp:attachment":[{"href":"https:\/\/carson.fenimorefamily.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=626"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/carson.fenimorefamily.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=626"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/carson.fenimorefamily.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=626"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}