Building dynamic library

Hi, i am trying to build a dynamic library to be able to call it from c++/c#

I am using the restic/builder:latest and invoking it using:
docker run --rm --volume “$PWD/restic-0.9.5:/restic” --volume “$PWD/output:/output” restic/builder

Now I changed the file: helpers/build-release-binaries/main.go to add the build mode: -buildmode=c-shared but am getting an error about missing gcc:

$sudo docker run --rm --volume "$PWD/restic-0.9.5:/restic" --volume "$PWD/output:/output" restic/builder
 building with 8 workers
# github.com/restic/restic/cmd/restic
loadinternal: cannot find runtime/cgo
/usr/local/go/pkg/tool/linux_amd64/link: running gcc failed: exec: "gcc": executable file not found in $PATH

I’m a newbie to golang, but from what i read about it, it seems that only build type “exe” is supported without external linker. So my question is: is it somehow possible to change the build environment to build a library out of restic?

From that error it looks like that gcc is not installed. Maybe you should create a new docker image from the existing one and install gcc in it ? I never did it, but that’s what I would try.

or the environment is not set correctly so the shell can’t find gcc - as the error message states.

Thank you both for your replies. Well it was and was not in missing gcc.

In the end I ended up building a simple vagrant box with the tools as mentioned in reproducible builds docu.
Once i got that, after some searching I managed to compile restic as a static library (with go 1.13) using:

GOOS=linux GOARCH=amd64 CGO_ENABLED=1 go build -buildmode=c-archive -mod=vendor -ldflags "-s -w" -tags selfupdate -o librestic_amd64.a ./cmd/restic

which produced:

root@ubuntu-bionic:/restic# ls -la librestic_amd64.*
-rw-r–r-- 1 root root 30207484 Sep 26 10:16 librestic_amd64.a
-rw-r–r-- 1 root root 1612 Sep 26 10:16 librestic_amd64.h

To be able to use restic functionality, I have created a simple exported function (before compiling restic as a static lib) in: cmd/restic/main.go
https://gist.github.com/ludovit-mikula/e149cbb14f68c5d2b38cc0b7b148c029

Here is a sample c++ app that utilizes this exported function:
https://gist.github.com/ludovit-mikula/2dd6fc57c689f1cb7412eacc45d27ea6

Compile it with: g++ -o test -I . -L . test.cpp -lrestic_amd64 -lpthread -m64
You can then run it like this:

vagrant@ubuntu-bionic:~/work/linux-example$ ./test version
restic 0.9.5-dev (compiled manually) compiled with go1.13 on linux/amd64
vagrant@ubuntu-bionic:~/work/linux-example$

It’s been a while i touched c/c++ and am a complete golang newbie so bear with me :slight_smile:
I’m trying now to build a windows .dll and use it from managed code (seems to be a bigger beast :smiley: )

Hi @umbro, I am also working on build a dynamic library of restic. I want to see your work but I can’t open the link of restic.patch and test.cpp. Can you post them again, thank you!

If it is interesting for you, one goal in rustic is to separate the CLI from a library which contains basically all functionality to work with a restic repo.

I have to admit that so far I didn’t do much to extract things into a library, but if you know Rust or want to learn it, feel free to work on it an propose a PR!

Hey @garyGlh … I have changed the the login, so it’s still there just the links are now:

and:

It’s been a while tho, so you might need to make some changes.

Thank you very much!

Thank you very much!