Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

golang - unable to use go library #41516

Closed
ManojKumarChauhan opened this issue Sep 21, 2020 · 6 comments
Closed

golang - unable to use go library #41516

ManojKumarChauhan opened this issue Sep 21, 2020 · 6 comments

Comments

@ManojKumarChauhan
Copy link

@ManojKumarChauhan ManojKumarChauhan commented Sep 21, 2020

What version of Go are you using (go version)?

$ go version
go version go1.15 linux/amd64

Does this issue reproduce with the latest release?

yes

What operating system and processor architecture are you using (go env)?

4.15.0-117-generic #118~16.04.1-Ubuntu SMP Sat Sep 5 23:35:06 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux

What did you do?

Created a golang package 'test' using following commands
go install -buildmode=shared std
go install -buildmode=shared -linkshared test

above commands will create libstd.so and libtest.so under
go/pkg/linux_amd64_dynlink folder

Created a C program to use libtest.so
int main(int argc, char **argv) {
void *handle= dlopen ("libtest.so", RTLD_LAZY);
if (!handle) {
fputs (dlerror(), stderr);
exit(1);
}
...
}

dlopen is failed with following error
libstd.so: undefined symbol: main.main

What did you expect to see?

dlopen should succeed

What did you see instead?

libstd.so: undefined symbol: main.main
This error is coming for any package. GOROOT and GOPATH are set correctly.

@AlexRouSg
Copy link
Contributor

@AlexRouSg AlexRouSg commented Sep 21, 2020

-buildmode=shared is to create a Go shared lib only useable in Go programs.
You want -buildmode=c-shared if you want to use it in C

See https://golang.org/cmd/go/#hdr-Build_modes

@ManojKumarChauhan
Copy link
Author

@ManojKumarChauhan ManojKumarChauhan commented Sep 21, 2020

Hi @AlexRouSg ,
Using -buildmode=c-shared, I cannot export structure.
I a looking to create a library in golang where the functions in library can take stratucture as an argument or return structure.
Please suggest if this can be done.

@AlexRouSg
Copy link
Contributor

@AlexRouSg AlexRouSg commented Sep 21, 2020

You should be able to use a C structure instead of a Go structure.

@ManojKumarChauhan
Copy link
Author

@ManojKumarChauhan ManojKumarChauhan commented Sep 21, 2020

does that mean If I have a go package which contains a go structure then convert this to C structure & create the lib using c-shared?

@AlexRouSg
Copy link
Contributor

@AlexRouSg AlexRouSg commented Sep 21, 2020

Yes.

As this is not a bug and the issue tracker is only for bugs, please see https://github.com/golang/go/wiki/Questions if you need further/more detailed help.

@ManojKumarChauhan
Copy link
Author

@ManojKumarChauhan ManojKumarChauhan commented Sep 21, 2020

Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.