mozilla / cbindgen

A project for generating C bindings from Rust code
Mozilla Public License 2.0
2.27k stars 294 forks source link

Linking to multiple ffi crates fails #980

Closed seftontycho closed 6 days ago

seftontycho commented 6 days ago

I have two crates that use cbindgen to generate bindings for c++. When I try to build the C++ project with both crates on windows I get the following errors:

06:24:17   analogue_dropouts.lib(alloc-7bfd0a6d1a588dbd.alloc.a1e7c519547c97d-cgu.0.rcgu.o) : error LNK2005: __rdl_oom already defined in language_identification.lib(alloc-f7d2d3884e227169.alloc.e369825d162945b2-cgu.0.rcgu.o)
06:24:17   analogue_dropouts.lib(std-8ac42145e0282c2a.std.184ed18d6ec46d5f-cgu.0.rcgu.o) : error LNK2005: __rdl_alloc already defined in language_identification.lib(std-0953e6946d141dd5.std.1acf644841c643e8-cgu.0.rcgu.o)
06:24:17   analogue_dropouts.lib(std-8ac42145e0282c2a.std.184ed18d6ec46d5f-cgu.0.rcgu.o) : error LNK2005: __rdl_dealloc already defined in language_identification.lib(std-0953e6946d141dd5.std.1acf644841c643e8-cgu.0.rcgu.o)

It looks like some parts of alloc and std are present in both (which makes sense) causing a conflict. How can I get around this?

My cbindgen.toml looks like the following:

include_guard = "ANALOGUE_DROPOUTS_HPP"

autogen_warning = "/* This file is autogenerated by cbindgen. Don't modify this file manually. */"

namespace = "ffi"

pragma_once = true

[parse]
expand = ["analogue_dropouts"]

[export]
prefix = "analogue_dropouts_"
seftontycho commented 6 days ago

This is not an issue on linux but I assume that is because on windows to link to the dll you also need the .lib file whereas on linux that is not the case.

emilio commented 6 days ago

This is not a cbindgen issue, cbindgen only generates the headers... However it seems like you're trying to statically link multiple crates which is not really something that works, see https://github.com/rust-lang/rust/issues/33221 and so on.

A way to make it work could be something like this which compiles everything into a single staticlib.