[英]Error: undefined reference to `sqlite3_open'
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 吗?
您需要将 sqlite3 库与您的程序一起链接:
g++ main.cpp -lsqlite3
You need to adjust your linker flags to link in the sqlite3
library.您需要调整链接器标志以在sqlite3
库中进行链接。 Libraries are usually installed in /usr/lib
or /usr/lib64
库通常安装在/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
as per: http://sqlite.org/cvstrac/wiki?p=HowToCompile根据: http : //sqlite.org/cvstrac/wiki?p=HowToCompile
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++
.有了它,您可以在C
或C++
程序中使用#include <sqlite3.h>
。
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
在命令行或 Open IDE -> project -> properties -> locate lib file for sqlite3 中将您的程序链接到 lib g++ yourProgram.c -lsqlite3 。
Devcpp
如果你使用Devcpp
1. you just need to add the sqlite3.dll file in the project folder. 1.只需要在项目文件夹中添加sqlite3.dll文件即可。
2. Goto Compiler option in Tools >> 2.工具中的 Goto Compiler 选项 >>
3. just write sqlite3.dll
next to >> Add the following commands when calling compiler 3.只需在>>旁边写sqlite3.dll
调用编译器时添加以下命令
NOTE : you need to install MinGW (compiler)注意:您需要安装 MinGW(编译器)
g++ file.cpp -o output.exe sqlite3.dll
You need to define sqlite3.dll
in linker in project properties您需要在项目属性的链接器中定义sqlite3.dll
HAVE A NICE DAY
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.