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

cmd/go: "go list" option to display accumulated cgo LDFLAGS for -buildmode=c-archive/c-shared #11258

Open
shazow opened this issue Jun 17, 2015 · 7 comments
Labels
GoCommand cmd/go help wanted NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@shazow
Copy link
Contributor

shazow commented Jun 17, 2015

Trying to build a simple Go library that spins up an http.Server from a call in C, but it fails to compile the C against the produced library object. Similar examples without involving http work just fine.

$ go version
go version devel +a2aaede Wed Jun 17 14:55:39 2015 +0000 darwin/amd64
$ ld -v
@(#)PROGRAM:ld  PROJECT:ld64-241.9
configured to support archs: armv6 armv7 armv7s arm64 i386 x86_64 x86_64h armv6m armv7m armv7em
LTO support using: LLVM version 3.5svn
$ gcc --version
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 6.0 (clang-600.0.57) (based on LLVM 3.5svn)
Target: x86_64-apple-darwin13.4.0
Thread model: posix

Error:

$ gcc -o gohttp-c examples/c/main.c gohttplib.a -lpthread
Undefined symbols for architecture x86_64:
  "_CFArrayGetCount", referenced from:
      _FetchPEMRoots in gohttplib.a(000003.o)
  "_CFArrayGetValueAtIndex", referenced from:
      _FetchPEMRoots in gohttplib.a(000003.o)
  "_CFDataAppendBytes", referenced from:
      _FetchPEMRoots in gohttplib.a(000003.o)
  "_CFDataCreateMutable", referenced from:
      _FetchPEMRoots in gohttplib.a(000003.o)
  "_CFDataGetBytePtr", referenced from:
      _FetchPEMRoots in gohttplib.a(000003.o)
      __cgo_6dbb806e9976_Cfunc_CFDataGetBytePtr in gohttplib.a(000003.o)
     (maybe you meant: __cgo_6dbb806e9976_Cfunc_CFDataGetBytePtr)
  "_CFDataGetLength", referenced from:
      _FetchPEMRoots in gohttplib.a(000003.o)
      __cgo_6dbb806e9976_Cfunc_CFDataGetLength in gohttplib.a(000003.o)
     (maybe you meant: __cgo_6dbb806e9976_Cfunc_CFDataGetLength)
  "_CFRelease", referenced from:
      _FetchPEMRoots in gohttplib.a(000003.o)
      __cgo_6dbb806e9976_Cfunc_CFRelease in gohttplib.a(000003.o)
     (maybe you meant: __cgo_6dbb806e9976_Cfunc_CFRelease)
  "_SecKeychainItemExport", referenced from:
      _FetchPEMRoots in gohttplib.a(000003.o)
  "_SecTrustCopyAnchorCertificates", referenced from:
      _FetchPEMRoots in gohttplib.a(000003.o)
  "_kCFAllocatorDefault", referenced from:
      _FetchPEMRoots in gohttplib.a(000003.o)
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [example-c] Error 1

More thorough description and full code examples are available in this StackOverflow question.

This might be related to the OSX/darwin arch as a commenter was able to get it working on Ubuntu.

@ianlancetaylor
Copy link
Contributor

Does it work if you add "-framework CoreFoundation -framework Security" to the gcc link line? Probably just before the -pthread option.

I took those options from the # cgo LDFLAGS line in crypto/x509/root_cgo_darwin.go. I don't see a way for us to add those lines to the final command line, as that is not under our control. I don't know if there is a way to avoid needing them at all. There may be Darwin-specific approaches that I am not aware of.

@ianlancetaylor ianlancetaylor added this to the Go1.6 milestone Jun 17, 2015
@shazow
Copy link
Contributor Author

shazow commented Jun 17, 2015

@ianlancetaylor Aha, this does the trick indeed:

gcc -o gohttp-c examples/c/main.c gohttplib.a -framework CoreFoundation -framework Security -lpthread

(I think you have a mispaste in your comment with "http://go/gocomments#TOC-ErrorStrings")

@ianlancetaylor
Copy link
Contributor

Fixed the mispaste, sorry about that.

I'm going to postpone this to 1.6. I don't know what to do about it, but maybe someone can think of something.

@rsc
Copy link
Contributor

rsc commented Nov 5, 2015

Sounds like this is a documentation issue. I don't know of any magic way to convince the linker to automatically link a library. And if Ian doesn't, there probably isn't one.

@rsc rsc modified the milestones: Go1.6Maybe, Go1.6 Dec 17, 2015
@rsc
Copy link
Contributor

rsc commented Jan 6, 2016

@ianlancetaylor, I think this is a documentation issue, but where do we put the note that the final link will need to include libraries used by the Go sources? Is there something other than your build modes doc?

@ianlancetaylor
Copy link
Contributor

I started to write docs in helpBuildmode in cmd/go/help.go, but it's too horrible to make people dig into the crypto/x509 sources to figure out what options to pass. I think what we need to do is have a way for cmd/go to print out the accumulated cgo LDFLAGS from the packages added to a c-archive or c-shared build. Then people can in principle capture that at the time they build the archive/shared library (probably using a Makefile), and pass it on their final linker command line.

So, postponing until 1.7.

@ianlancetaylor ianlancetaylor modified the milestones: Go1.7, Go1.6Maybe Jan 6, 2016
@ianlancetaylor ianlancetaylor changed the title cmd/link: buildmode=c-archive with net/http.Server fails to be linked against cmd/go: option to display accumulated cgo LDFLAGS for -buildmode=c-archive/c-shared Jan 6, 2016
@rsc rsc modified the milestones: Go1.8, Go1.7 May 17, 2016
@quentinmit quentinmit added the NeedsFix The path to resolution is known, but the work has not been done. label Oct 6, 2016
@rsc rsc modified the milestones: Go1.9, Go1.8 Nov 3, 2016
@rsc rsc modified the milestones: Go1.10, Go1.9 Jun 22, 2017
@rsc rsc modified the milestones: Go1.10, Go1.11 Dec 1, 2017
@gopherbot gopherbot modified the milestones: Go1.11, Unplanned May 23, 2018
@nickkeets
Copy link

As a follow up to this, there is a bigger related issue on iOS. The following functions exist on macOS but not on iOS:

SecTrustSettingsCopyCertificates
SecTrustSettingsCopyTrustSettings
SecCertificateCopyNormalizedSubjectContent
SecCertificateCopyNormalizedIssuerContent
SecItemExport

So adding -framework Security doesn't help there. This means that any code that imports net/http (or anything else that uses crypto/x509) can not be used on iOS.

@bcmills bcmills changed the title cmd/go: option to display accumulated cgo LDFLAGS for -buildmode=c-archive/c-shared cmd/go: "go list" option to display accumulated cgo LDFLAGS for -buildmode=c-archive/c-shared Sep 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
GoCommand cmd/go help wanted NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

7 participants