为什么在尝试创建数据库时出现未定义的引用错误?

发布于 2024-11-26 22:36:51 字数 1244 浏览 1 评论 0原文

我正在尝试在 Qt 中创建 SQLite 数据库:

#include <QtCore/QCoreApplication>
#include <QtSql/QSqlDatabase>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName("C:\\Users\\Tanner\\Desktop\\db.sqlite");

    return a.exec();
}

输出:

C:\Users\Tanner\qt\sqltest-build-desktop..\sqltest\main.cpp:8:错误: 对“_imp___ZN12QSqlDatabase17defaultConnectionE”的未定义引用

C:\Users\Tanner\qt\sqltest-build-desktop..\sqltest\main.cpp:8:错误: 对“imp___ZN12QSqlDatabase11addDatabaseERK7QStringS2”的未定义引用

C:\Users\Tanner\qt\sqltest-build-desktop..\sqltest\main.cpp:8:错误: 对“_imp___ZN12QSqlDatabaseD1Ev”的未定义引用

C:\Users\Tanner\qt\sqltest-build-desktop..\sqltest\main.cpp:9:错误: 对“_imp___ZN12QSqlDatabase15setDatabaseNameERK7QString”的未定义引用

C:\Users\Tanner\qt\sqltest-build-desktop..\sqltest\main.cpp:11: 错误:未定义对“_imp___ZN12QSqlDatabaseD1Ev”的引用

C:\Users\Tanner\qt\sqltest-build-desktop..\sqltest\main.cpp:11: 错误:未定义对“_imp___ZN12QSqlDatabaseD1Ev”的引用

:-1: 错误:collect2: ld 返回 1 退出状态

我在文件路径中添加了额外的斜杠,因为编译器一直抱怨字符转义。这可能有什么关系吗?如果是这样,我怎样才能进入路径而不让它认为我试图逃避下一个角色?

I'm trying to create an SQLite database in Qt:

#include <QtCore/QCoreApplication>
#include <QtSql/QSqlDatabase>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName("C:\\Users\\Tanner\\Desktop\\db.sqlite");

    return a.exec();
}

The output:

C:\Users\Tanner\qt\sqltest-build-desktop..\sqltest\main.cpp:8: error:
undefined reference to `_imp___ZN12QSqlDatabase17defaultConnectionE'

C:\Users\Tanner\qt\sqltest-build-desktop..\sqltest\main.cpp:8: error:
undefined reference to `imp___ZN12QSqlDatabase11addDatabaseERK7QStringS2'

C:\Users\Tanner\qt\sqltest-build-desktop..\sqltest\main.cpp:8: error:
undefined reference to `_imp___ZN12QSqlDatabaseD1Ev'

C:\Users\Tanner\qt\sqltest-build-desktop..\sqltest\main.cpp:9: error:
undefined reference to `_imp___ZN12QSqlDatabase15setDatabaseNameERK7QString'

C:\Users\Tanner\qt\sqltest-build-desktop..\sqltest\main.cpp:11:
error: undefined reference to `_imp___ZN12QSqlDatabaseD1Ev'

C:\Users\Tanner\qt\sqltest-build-desktop..\sqltest\main.cpp:11:
error: undefined reference to `_imp___ZN12QSqlDatabaseD1Ev'

:-1: error: collect2: ld returned 1 exit status

I added extra slashes to the filepath because the compiler kept complaining about character escaping. Could that have anything to do with it? If so, how can I enter the path without it thinking I'm trying to escape the next character?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

知你几分 2024-12-03 22:36:51

我在 .pro 文件中缺少 QT += sql

I was missing QT += sql in the .pro file.

怪我鬧 2024-12-03 22:36:51

您看到的错误不是编译器错误,而是链接器错误。该错误本身与 SQLite 无关,而是与 Qt 有关。您很可能不会链接到 QtSql4.lib (4 可能是不同的版本号)。这样做很可能会让您看到的错误消失。

作为您的评论/问题的答案,您(在您的应用程序级别)根本不必担心 SQLite。由于您使用 Qt 的 SQL 功能,因此这将由 Qt 处理(并“隐藏”)。 Qt 使用所谓的数据库驱动程序,它们本质上是根据什么加载的插件您已在代码中指定。所以你的情况是一个 SQLite 驱动程序。

如果安装 Qt 时尚未创建此驱动程序,您可能需要自己构建它。您可以按照我上面提供的链接中的说明进行操作。

PS您已正确处理“字符转义”问题。这不是您当前问题的原因。

The errors you're seeing are not compiler errors, but linker errors. The error itself is not related to SQLite, but to Qt. You most likely don't link against QtSql4.lib (the 4 might be a different version number). Doing that will most likely make the errors you're seeing go away.

As an answer to your comment/question, you don't (on your application level) have to worry about SQLite at all. Since you use Qt's SQL functionality, this will be taken care of (and "hidden") by Qt. Qt uses so called database drivers which are essentially plugins it loads based on what you have specified in your code. So an SQLite driver in your case.

If this driver has not been created when you installed Qt, you might have to build it yourself. You can follow the instructions you can find in the link I have provided above.

P.s. you have correctly handled the "character escaping" issue. This is not the cause of your current problems.

握住你手 2024-12-03 22:36:51

在 C++ 中,即使在字符串中,反斜杠也已作为前缀。如果我将 \ 更改为 / 它仍然可以找到我的文件。对于较大的文件路径,这可能会有所帮助,因为您将使用更少的字符。

In C++ the backslash is already prefixed even in a string. If I change \ to / it can still find my files. For larger file paths this might help because you'll use less characters.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文