Help us understand the problem. What is going on with this article?

cgoで作ったStatic LibraryをMacで使おうとしたら困った話

More than 1 year has passed since last update.

Go Code

まず初めに、GoでStatic Libraryを作り

example.go
package main

import (
  "C"
  "io"
  "io/ioutil"
  "net/http"
)

//export GetUrl
func GetUrl(url string) (int, *C.char) {
  response, err := http.Get(url)
  if nil != err {
    return 0, nil
  }

  defer func() {
    io.Copy(ioutil.Discard, response.Body)
    response.Body.Close()
  }()

  body, err := ioutil.ReadAll(response.Body)
  if nil != err {
    return 0, nil
  }

  return len(body), C.CString(string(body))
}

func main() {}

Go Code build

build_static
# build to static library.
$ go build -buildmode=c-archive -o ./libtest.a ./test.go 

Main Code

それを使ってMacで実行ファイルを作ろうとしたんじゃ。

main.cpp
#include <iostream>
#include "libtest.h"

int main() {
  const std::string URL{"http://www.yahoo.co.jp"};
  auto data = GetUrl({URL.data(), static_cast <int32_t> (URL.length())});
  if (0 < data.r0) {
    std::cout << std::string(data.r1, data.r0) << std::endl;
    free(data.r1);
  }
}

Main Code build

build_command
# build to executable.
$ clang++ -O2 -std=c++1z -I./ -L./ main.cpp -ltest

Build Result

でも、そんなSymbol知らねーよとclang様にお叱りを受け

build_error
Undefined symbols for architecture x86_64:
  "_CFArrayGetCount", referenced from:
      _FetchPEMRoots_MountainLion in libtest.a(000003.o)
      _FetchPEMRoots in libtest.a(000003.o)
  "_CFArrayGetValueAtIndex", referenced from:
      _FetchPEMRoots_MountainLion in libtest.a(000003.o)
      _FetchPEMRoots in libtest.a(000003.o)
  "_CFDataAppendBytes", referenced from:
      _FetchPEMRoots_MountainLion in libtest.a(000003.o)
      _FetchPEMRoots in libtest.a(000003.o)
  "_CFDataCreateMutable", referenced from:
      _FetchPEMRoots_MountainLion in libtest.a(000003.o)
      _FetchPEMRoots in libtest.a(000003.o)
  "_CFDataGetBytePtr", referenced from:
      _FetchPEMRoots_MountainLion in libtest.a(000003.o)
      _FetchPEMRoots in libtest.a(000003.o)
      __cgo_62033c69288a_Cfunc_CFDataGetBytePtr in libtest.a(000003.o)
     (maybe you meant: __cgo_62033c69288a_Cfunc_CFDataGetBytePtr)
  "_CFDataGetLength", referenced from:
      _FetchPEMRoots_MountainLion in libtest.a(000003.o)
      _FetchPEMRoots in libtest.a(000003.o)
      __cgo_62033c69288a_Cfunc_CFDataGetLength in libtest.a(000003.o)
     (maybe you meant: __cgo_62033c69288a_Cfunc_CFDataGetLength)
  "_CFDictionaryGetValueIfPresent", referenced from:
      _FetchPEMRoots in libtest.a(000003.o)
  "_CFEqual", referenced from:
      _FetchPEMRoots in libtest.a(000003.o)
  "_CFNumberGetValue", referenced from:
      _FetchPEMRoots in libtest.a(000003.o)
  "_CFRelease", referenced from:
      _FetchPEMRoots_MountainLion in libtest.a(000003.o)
      _FetchPEMRoots in libtest.a(000003.o)
      __cgo_62033c69288a_Cfunc_CFRelease in libtest.a(000003.o)
     (maybe you meant: __cgo_62033c69288a_Cfunc_CFRelease)
  "_CFStringCreateWithCString", referenced from:
      _FetchPEMRoots in libtest.a(000003.o)
  "_SecCertificateCopyNormalizedIssuerContent", referenced from:
      _FetchPEMRoots in libtest.a(000003.o)
  "_SecCertificateCopyNormalizedSubjectContent", referenced from:
      _FetchPEMRoots in libtest.a(000003.o)
  "_SecKeychainItemExport", referenced from:
      _FetchPEMRoots_MountainLion in libtest.a(000003.o)
      _FetchPEMRoots in libtest.a(000003.o)
  "_SecTrustCopyAnchorCertificates", referenced from:
      _FetchPEMRoots_MountainLion in libtest.a(000003.o)
  "_SecTrustSettingsCopyCertificates", referenced from:
      _FetchPEMRoots in libtest.a(000003.o)
  "_SecTrustSettingsCopyTrustSettings", referenced from:
      _FetchPEMRoots in libtest.a(000003.o)
  "_kCFAllocatorDefault", referenced from:
      _FetchPEMRoots_MountainLion in libtest.a(000003.o)
      _FetchPEMRoots in libtest.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)

解決

途方に暮れていたら、いんたーねっと様から啓示を受け
パラメーター(framework)を追加したら解決しましたとさ。

build_success
$ clang++ -O2 -std=c++1z -I./ -L./ main.cpp  -ltest -framework CoreFoundation -framework Security
$ ./a.out
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta http-equiv="content-style-type" content="text/css">
<meta http-equiv="content-script-type" content="text/javascript">
<meta name="description" content="日本最大級のポータルサイト。検索、オークション、ニュース、メール、コミュニティ、ショッピング、など80以上のサービスを展開。あなたの生活をより豊かにする「ライフ・エンジン」を目指していきます。">
<meta name="robots" content="noodp">


~~~ 中略 ~~~


<td align="center">Copyright (C) 2017 Yahoo Japan Corporation. All Rights Reserved.</td>
</tr>
</table>
</td>
</tr>
</table>
</center>
</body>
</html>

<!-- p06.f13.top.kks.yahoo.co.jp Tue Jul 25 12:36:20 JST 2017 -->

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away