how to open a new sqlite3 database using sqlite3.h and cgo?

huangapple go评论89阅读模式
英文:

how to open a new sqlite3 database using sqlite3.h and cgo?

问题

我正在尝试通过C和cgo使用sqlite3(我知道Go有一个sqlite3的封装,但想尝试这种方式)。这个程序给我一个错误信息:

(Undefined symbols for architecture x86_64:
"_sqlite3_open",引用自:
__cgo_1d4838eae1de_Cfunc_sqlite3_open in cGo.cgo2.o
(也许你的意思是:__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)

我不明白为什么会出现这个错误-有人可以帮我理解如何打开一个新的数据库流吗?

// 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)
}

英文:

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:
&quot;_sqlite3_open&quot;, 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 &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;sqlite3.h&gt;
*/
import	&quot;C&quot;
//import &quot;fmt&quot;


func main() {
var t* C.sqlite3
C.sqlite3_open(&quot;test.db&quot;, t)

}

答案1

得分: 3

"未定义的符号"意味着构建过程没有将您的代码与系统上的sqlite库链接起来。

现有的go-sqlite库通过在包目录中放置一个C文件(https://github.com/mxk/go-sqlite/blob/master/sqlite3/sqlite3.c),该文件#include整个SQLite代码(同一包中的lib/sqlite3.c)来将其链接到SQLite。它还提供了一些编译器标志(CFLAGS)和C包装函数(在sqlite3.go中)。

这种方法的好处是,这意味着SQLite被链接到您的二进制文件中,因此使用它的用户在运行程序之前不需要单独安装SQLite。使用仅使用纯Go库构建的gc工具链(而不是gccgo)的程序默认情况下是这样的,所以这种方式在Go中是比较常见的。

另一种方法是在您的extern "C"声明之前在代码中使用cgo的#cgo LDFLAGS: -lsqlite3指示;然后用户需要安装兼容的sqlite3库才能使用您的二进制文件,但您不需要在存储库中包含源代码,这可能更容易。请注意,使用此方法构建您的软件包的人仍然需要在系统上安装SQLite头文件,例如,他们的Linux发行版可能提供一个libsqlite3-dev软件包。

希望这对于cgo和库集成有一定的帮助。我认为,当有现成的包装器可用时,只需使用go-sqlite3或其他现有的包装器可能会更好。

英文:

"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 &quot;C&quot; 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.

huangapple
  • 本文由 发表于 2014年4月27日 02:29:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/23314934.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定