gpt4 book ai didi

C++ QT Creator 5.0 SQLite3_Open 未定义引用

转载 作者:行者123 更新时间:2023-11-28 00:44:35 25 4
gpt4 key购买 nike

我是尝试链接到 sqlite 数据库的 C++ 广告的新手。我正在学习教程,但收到 sqlite3_open 错误未定义引用。我做错了什么?

#include "database.h"
#include<stdio.h>
#include<sqlite3.h>
#include<stdlib.h>

database::database()
{
}

int database::test()
{
int retval;

// A prepered statement for fetching tables
sqlite3_stmt *stmt;

// Create a handle for database connection, create a pointer to sqlite3
sqlite3 *handle;

// try to create the database. If it doesnt exist, it would be created
// pass a pointer to the pointer to sqlite3, in short sqlite3**
retval = sqlite3_open("CC.sqlite",&handle);
// If connection failed, handle returns NULL
if(retval)
{
printf("Database connection failed\n");
return -1;
}
printf("Connection successful\n");
return 1;
}

.pro文件

#-------------------------------------------------
#
# Project created by QtCreator 2013-05-31T09:22:09
#
#-------------------------------------------------

QT += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = CC_Herd_Manager
TEMPLATE = app


SOURCES += main.cpp\
mainwindow.cpp \
animal.cpp \
email.cpp \
owner.cpp \
phone.cpp \
equipment.cpp \
../CC_Cpp/cc_number.cpp \
../CC_Cpp/cc_date.cpp \
../CC_Cpp/cc_timestamp.cpp \
../CC_Cpp/cc_address.cpp \
database.cpp

HEADERS += mainwindow.h \
animal.h \
email.h \
owner.h \
phone.h \
equipment.h\
../CC_Cpp/cc_number.h \
../Cpp/CC_Cpp/cc_date.h \
../Cpp/CC_Cpp/cc_timestamp.h \
../Cpp/CC_Cpp/cc_address.h \
database.h

FORMS += mainwindow.ui

OTHER_FILES += \
DB_Install

错误

/home/mongo/Cpp/CC_Herd_Manager/database.o:-1: In function `database::test()':
/home/mongo/Cpp/CC_Herd_Manager/database.cpp:23: error: undefined reference to `sqlite3_open'
:-1: error: collect2: error: ld returned 1 exit status

最佳答案

如果您想再次链接到您系统的 sqlite 版本(这可能是最好的),您必须将以下行添加到您的 .pro 文件中:

LIBS += -lsqlite3

应该就是这样。

您还可以下载 sqlite 源代码(他们称之为“合并”)并将 .c 文件添加到您的项目中。如果这样做,您也应该使用下载的头文件。但是因为你在代码中使用了系统的头文件,我假设你想使用我推荐的系统的库版本。

关于C++ QT Creator 5.0 SQLite3_Open 未定义引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16964598/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com