C++ 错误:对“sqlite3_open”的未定义引用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9389344/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error: undefined reference to `sqlite3_open'
提问by thameera
I'm trying to get started with the C++ API for SQLite.
我正在尝试开始使用 SQLite 的 C++ API。
#include <iostream>
#include <sqlite3.h>
using namespace std;
int main()
{
sqlite3 *db;
if (sqlite3_open("ex1.db", &db) == SQLITE_OK)
cout << "Opened db successfully\n";
else
cout << "Failed to open db\n";
return 0;
}
Compiling this using the command "g++ main.cpp" gives the following error:
使用命令“g++ main.cpp”编译它会出现以下错误:
/tmp/ccu8sv4b.o: In function `main':
main.cpp:(.text+0x64): undefined reference to `sqlite3_open'
collect2: ld returned 1 exit status
What could have gone wrong? Hasn't sqlite3 properly installed in the server I'm compiling this in?
可能出了什么问题?我正在编译的服务器中没有正确安装 sqlite3 吗?
回答by casablanca
You need to link the sqlite3 library along with your program:
您需要将 sqlite3 库与您的程序一起链接:
g++ main.cpp -lsqlite3
回答by fileoffset
You need to adjust your linker flags to link in the sqlite3
library. Libraries are usually installed in /usr/lib
or /usr/lib64
您需要调整链接器标志以在sqlite3
库中链接。库通常安装在/usr/lib
或/usr/lib64
Alternatively, you can copy the sqlite3.c
file to your project directory and compile it as part of the g++
command:
或者,您可以将sqlite3.c
文件复制到您的项目目录并将其编译为g++
命令的一部分:
g++ main.cpp sqlite3.c
回答by fgomez
First step:Install all library sqlite3 with the command:
第一步:使用以下命令安装所有库 sqlite3:
sudo apt-get install libsqlite3-dev
With that you can use #include <sqlite3.h>
in a programm of C
or C++
.
有了它,您可以#include <sqlite3.h>
在C
或的程序中使用C++
。
Second step:To compile the program by console:
第二步:通过控制台编译程序:
C++:
C++:
g++ program.cpp -o executable -lsqlite3
./executable
C:
C:
gcc program.c -o executable -lsqlite3
./executable
回答by user11586466
Either link your program to lib g++ yourProgram.c -lsqlite3 in command line or in Open IDE -> project -> properties -> locate lib file for sqlite3 .
在命令行或 Open IDE -> project -> properties -> locate lib file for sqlite3 中将您的程序链接到 lib g++ yourProgram.c -lsqlite3 。