Can't call a function from static C++ library in Rust

I have a C++ app that I want to wrap into Rust.

It has GYP-based build system and produces an executable.
Current plan is to to replace main() with exported run() function and call it from Rust executable instead.
mediasoup/worker at rust-worker · nazar-pc/mediasoup · GitHub (note: if you decide to build it, you'll provably need to set environment variable PYTHON=python3).

I have added static library build for that app and now can't seem to figure out how to call it.

CXX and CC do not really help here since they expect to build things and I already have a static library.


^ in helps somewhat but results in lots of complaints from linker like this:

  = note: /usr/bin/ld: /usr/bin/ld: DWARF error: could not find variable specification at offset 5081
          /usr/bin/ld: DWARF error: could not find variable specification at offset 67e4
          /usr/bin/ld: DWARF error: could not find variable specification at offset 6816
          /web/github/mediasoup/worker/out/Release/libmediasoup-worker.a(DepLibSRTP.o): in function `DepLibSRTP::ClassInit()':
          /web/github/mediasoup/worker/out/../src/DepLibSRTP.cpp:48: undefined reference to `srtp_get_version_string'
          /usr/bin/ld: /web/github/mediasoup/worker/out/../src/DepLibSRTP.cpp:50: undefined reference to `srtp_init'
          /usr/bin/ld: /web/github/mediasoup/worker/out/Release/libmediasoup-worker.a(DepLibSRTP.o): in function `DepLibSRTP::ClassDestroy()':
          /web/github/mediasoup/worker/out/../src/DepLibSRTP.cpp:60: undefined reference to `srtp_shutdown'

Is there really an accessible way to achieve this?

If you're pulling in a static library from a project, you also have to pull in every other library that it depends on and link it with your Rust program.

In this case it's missing srtp_shutdown. That may need srtp2_sys.

I guess I know too little about this. I though static library has things inside of it. Included all of the .a files that were build and received this lonely error now:

  = note: /usr/bin/ld: /web/github/mediasoup/worker/out/Release/libuv.a(sysinfo-loadavg.o): in function `uv_loadavg':
          /web/github/mediasoup/worker/out/../deps/libuv/libuv/src/unix/sysinfo-loadavg.c:28: multiple definition of `uv_loadavg'; /web/github/mediasoup/worker/out/Release/libuv.a(linux-core.o):/web/github/mediasoup/worker/out/../deps/libuv/libuv/src/unix/linux-core.c:1132: first defined here
          /usr/bin/ld: /web/github/mediasoup/worker/out/Release/libabseil.a(print_hash_of.o): in function `main':
          /web/github/mediasoup/worker/out/../deps/libwebrtc/deps/abseil-cpp/abseil-cpp/absl/hash/internal/ multiple definition of `main'; /web/github/mediasoup/target/debug/deps/mediasoup_sys-ad2c49156d6c0bb6.37125jqqwh13ju51.rcgu.o:37125jqqwh13ju51:(.text.main+0x0): first defined here
          collect2: error: ld returned 1 exit status

There is indeed the same function present in two files, but I don't understand how it builds with C++ compiler without errors then.
Both files are explicitly mentioned for inclusion here (first and last): mediasoup/uv.gyp at rust-worker · nazar-pc/mediasoup · GitHub

UPD: I was able to get rid of it by editing 2 cpp files, but I'm sure this is wrong...

Confusion continues.

It seems to build as an executable with src/ and even runs, but when I switch it to be a library, I suddenly get this:

error: could not find native static library `stdc++`, perhaps an -L flag is missing?

error: aborting due to previous error

Interestingly, When I add this it does build:


But that is specific to my particular machine, so I'd like to avoid it if possible. There must be a better way.

The cc crate has a method for this: cc::Build - Rust

But then I'd have to call .compile(), which I don't need (I don't think). And cc doesn't seem to have an API to specify just libraries to link and not build anything on its own.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.