douye2488 2014-04-26 18:29
浏览 112
已采纳

如何使用sqlite3.h和cgo打开新的sqlite3数据库?

I am trying to use sqlite3 through C with cgo (I know that go has a sqlite3 wrapper, but want to try this way). This program gives me an error message

(Undefined symbols for architecture x86_64:
"_sqlite3_open", referenced from:
  __cgo_1d4838eae1de_Cfunc_sqlite3_open in cGo.cgo2.o
 (maybe you meant: __cgo_1d4838eae1de_Cfunc_sqlite3_open)
 ld: symbol(s) not found for architecture x86_64
 clang: error: linker command failed with exit code 1 (use -v to see invocation)

, and I do not understand why - could someone help me with understanding how to open a new database stream?

// cGo
package main

/*
#include <stdio.h>
#include <stdlib.h>
#include <sqlite3.h>
*/
import  "C"
//import "fmt"


func main() {
var t* C.sqlite3
C.sqlite3_open("test.db", t)

}

  • 写回答

1条回答 默认 最新

  • dtkvlj5386 2014-04-27 03:33
    关注

    "Undefined symbol" means that the build process isn't linking your code with the sqlite library on your system.

    The existing go-sqlite library gets linked to SQLite by placing in the package directory a C file which #include's the whole code of SQLite (lib/sqlite3.c in the same package). It also provides some compiler flags (CFLAGS) and C wrapper functions in sqlite3.go.

    The handy thing about that approach is that it means SQLite ends up linked right into your binary, so users of it don't have to separately install SQLite before they can run your program. Programs built on the gc toolchain (as opposed to gccgo) that use only pure-Go libraries are this way by default, so it's kind of "Go-y" to do it that way.

    Another approach is to use the cgo pragma #cgo LDFLAGS: -lsqlite3 in the code before your extern "C" declaration; then users need a compatible sqlite3 library installed to use your binary, but you don't need the source in your repository and it might be easier. Note that with this approach folks who want to build your package still need SQLite headers on their system, e.g., from an libsqlite3-dev package their Linux distribution might provide.

    Hope this has value as general help for cgo and library integration. I do think just using go-sqlite3 or another existing wrapper when available is likely to work out better.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧