LNK2019构建MySQL Codei N Visual Studio C++

发布于 2025-02-12 18:47:15 字数 4565 浏览 1 评论 0原文

我已经花了大约两天的时间试图使它起作用,但是我仍然在每种方法中都会遇到相同的错误。我是将MySQL连接到C ++的新手,因此我有点丢失,每次尝试编译它时,事情都会出现LNK2019错误。我正在使用Visual Studio 2022。

#include <iostream>
#include <mysql.h>
using namespace std;

// DATABASE STUFF
struct connection_details {
    const char* server, * user, * password, * database;
};

MYSQL* mysql_connection_setup(struct connection_details mysql_details) {
    MYSQL* connection = mysql_init(NULL);

    if (!(mysql_real_connect(connection, mysql_details.server, mysql_details.user, mysql_details.password, mysql_details.database, 0, NULL, 0))) {
        cout << "Connection Error: " << mysql_error(connection) << endl;
        exit(1);
    }

    return connection;
}

MYSQL_RES* mysql_execute_query(MYSQL* connection, const char* sql_query) {
    if (mysql_query(connection, sql_query)) {
        cout << "MYSQL Query Error: " << mysql_error(connection) << endl;
        exit(1);
    }

    return mysql_use_result(connection);
}

// MAIN FUNCTION
int main(int argc, char const* argv[]) {
    MYSQL* con;
    MYSQL_RES* res;
    MYSQL_ROW row;

    struct connection_details mysql_db;
    mysql_db.server = "localhost";
    mysql_db.user = "root";
    mysql_db.password = "tAblE4wTe3";
    mysql_db.database = "test";

    con = mysql_connection_setup(mysql_db);
    res = mysql_execute_query(con, "select * from table1");

    cout << "Displaying Database:\n" << endl;

    while ((row = mysql_fetch_row(res)) != NULL) {
        cout << row[0] << " | " << row[1] << " | " << row[2] << endl;
    }

    mysql_free_result(res);
    mysql_close(con);

    return 0;
}

它显示的错误:

1>------ Build started: Project: Test, Configuration: Debug x64 ------
1>main.obj : error LNK2019: unresolved external symbol mysql_error referenced in function "struct MYSQL * __cdecl mysql_connection_setup(struct connection_details)" (?mysql_connection_setup@@YAPEAUMYSQL@@Uconnection_details@@@Z)
1>main.obj : error LNK2019: unresolved external symbol mysql_init referenced in function "struct MYSQL * __cdecl mysql_connection_setup(struct connection_details)" (?mysql_connection_setup@@YAPEAUMYSQL@@Uconnection_details@@@Z)
1>main.obj : error LNK2019: unresolved external symbol mysql_real_connect referenced in function "struct MYSQL * __cdecl mysql_connection_setup(struct connection_details)" (?mysql_connection_setup@@YAPEAUMYSQL@@Uconnection_details@@@Z)
1>main.obj : error LNK2019: unresolved external symbol mysql_query referenced in function "struct MYSQL_RES * __cdecl mysql_execute_query(struct MYSQL *,char const *)" (?mysql_execute_query@@YAPEAUMYSQL_RES@@PEAUMYSQL@@PEBD@Z)
1>main.obj : error LNK2019: unresolved external symbol mysql_use_result referenced in function "struct MYSQL_RES * __cdecl mysql_execute_query(struct MYSQL *,char const *)" (?mysql_execute_query@@YAPEAUMYSQL_RES@@PEAUMYSQL@@PEBD@Z)
1>main.obj : error LNK2019: unresolved external symbol mysql_free_result referenced in function main
1>main.obj : error LNK2019: unresolved external symbol mysql_fetch_row referenced in function main
1>main.obj : error LNK2019: unresolved external symbol mysql_close referenced in function main
1>C:\Users\Administrator\Documents\Visual Studio 2022\C++ Projects\Test\x64\Debug\Test.exe : fatal error LNK1120: 8 unresolved externals
1>Done building project "Test.vcxproj" -- FAILED.

这些是Incluble/Library文件夹位置:

C:\Users\Administrator\Documents\Visual Studio 2022\mysql stuff\libbinlogevents
C:\Users\Administrator\Documents\Visual Studio 2022\mysql stuff\libbinlogstandalone
C:\Users\Administrator\Documents\Visual Studio 2022\mysql stuff\libchangestreams
C:\Users\Administrator\Documents\Visual Studio 2022\mysql stuff\libmysql
C:\Users\Administrator\Documents\Visual Studio 2022\mysql stuff\libservices
C:\Program Files\MySQL\Connector C++ 8.0\lib64\vs14

C:\Users\Administrator\Documents\Visual Studio 2022\mysql stuff\include
C:\Program Files\MySQL\Connector C++ 8.0\include

我尝试查找它,并且每个论坛,视频等。我尝试使用来解决的每个论坛,视频等不起作用。我不确定这是32位还是64位问题,尽管我不认为这是因为所有文件都指向64位文件夹。我认为这是我的代码中的东西,但我对此非常陌生,所以我无法真正指定它。任何帮助将不胜感激。

编辑:我设法解决了它,刚刚从头开始启动该项目,然后遵循Visual Studio docs this 视频。

I've spent about two days now trying to get this to work but I still keep getting the same error with every method I do. I'm new to connecting MySQL to C++ so I'm a bit lost and every time I try to compile it, the thing spits out LNK2019 error. I'm using Visual Studio 2022.

#include <iostream>
#include <mysql.h>
using namespace std;

// DATABASE STUFF
struct connection_details {
    const char* server, * user, * password, * database;
};

MYSQL* mysql_connection_setup(struct connection_details mysql_details) {
    MYSQL* connection = mysql_init(NULL);

    if (!(mysql_real_connect(connection, mysql_details.server, mysql_details.user, mysql_details.password, mysql_details.database, 0, NULL, 0))) {
        cout << "Connection Error: " << mysql_error(connection) << endl;
        exit(1);
    }

    return connection;
}

MYSQL_RES* mysql_execute_query(MYSQL* connection, const char* sql_query) {
    if (mysql_query(connection, sql_query)) {
        cout << "MYSQL Query Error: " << mysql_error(connection) << endl;
        exit(1);
    }

    return mysql_use_result(connection);
}

// MAIN FUNCTION
int main(int argc, char const* argv[]) {
    MYSQL* con;
    MYSQL_RES* res;
    MYSQL_ROW row;

    struct connection_details mysql_db;
    mysql_db.server = "localhost";
    mysql_db.user = "root";
    mysql_db.password = "tAblE4wTe3";
    mysql_db.database = "test";

    con = mysql_connection_setup(mysql_db);
    res = mysql_execute_query(con, "select * from table1");

    cout << "Displaying Database:\n" << endl;

    while ((row = mysql_fetch_row(res)) != NULL) {
        cout << row[0] << " | " << row[1] << " | " << row[2] << endl;
    }

    mysql_free_result(res);
    mysql_close(con);

    return 0;
}

Errors it shows:

1>------ Build started: Project: Test, Configuration: Debug x64 ------
1>main.obj : error LNK2019: unresolved external symbol mysql_error referenced in function "struct MYSQL * __cdecl mysql_connection_setup(struct connection_details)" (?mysql_connection_setup@@YAPEAUMYSQL@@Uconnection_details@@@Z)
1>main.obj : error LNK2019: unresolved external symbol mysql_init referenced in function "struct MYSQL * __cdecl mysql_connection_setup(struct connection_details)" (?mysql_connection_setup@@YAPEAUMYSQL@@Uconnection_details@@@Z)
1>main.obj : error LNK2019: unresolved external symbol mysql_real_connect referenced in function "struct MYSQL * __cdecl mysql_connection_setup(struct connection_details)" (?mysql_connection_setup@@YAPEAUMYSQL@@Uconnection_details@@@Z)
1>main.obj : error LNK2019: unresolved external symbol mysql_query referenced in function "struct MYSQL_RES * __cdecl mysql_execute_query(struct MYSQL *,char const *)" (?mysql_execute_query@@YAPEAUMYSQL_RES@@PEAUMYSQL@@PEBD@Z)
1>main.obj : error LNK2019: unresolved external symbol mysql_use_result referenced in function "struct MYSQL_RES * __cdecl mysql_execute_query(struct MYSQL *,char const *)" (?mysql_execute_query@@YAPEAUMYSQL_RES@@PEAUMYSQL@@PEBD@Z)
1>main.obj : error LNK2019: unresolved external symbol mysql_free_result referenced in function main
1>main.obj : error LNK2019: unresolved external symbol mysql_fetch_row referenced in function main
1>main.obj : error LNK2019: unresolved external symbol mysql_close referenced in function main
1>C:\Users\Administrator\Documents\Visual Studio 2022\C++ Projects\Test\x64\Debug\Test.exe : fatal error LNK1120: 8 unresolved externals
1>Done building project "Test.vcxproj" -- FAILED.

These are the include/library folder locations:

C:\Users\Administrator\Documents\Visual Studio 2022\mysql stuff\libbinlogevents
C:\Users\Administrator\Documents\Visual Studio 2022\mysql stuff\libbinlogstandalone
C:\Users\Administrator\Documents\Visual Studio 2022\mysql stuff\libchangestreams
C:\Users\Administrator\Documents\Visual Studio 2022\mysql stuff\libmysql
C:\Users\Administrator\Documents\Visual Studio 2022\mysql stuff\libservices
C:\Program Files\MySQL\Connector C++ 8.0\lib64\vs14

C:\Users\Administrator\Documents\Visual Studio 2022\mysql stuff\include
C:\Program Files\MySQL\Connector C++ 8.0\include

I've tried looking it up, and every forum, video, etc. that I try using to resolve doesn't work. I'm not sure if it's a 32-bit or 64-bit issue, though I don't think it is since all files point to 64-bit folders. I'm thinking it's something in my code but I'm very new to this so I can't really pinpoint it. Any help would be appreciated.

EDIT: I managed to solve it, just started the project from scratch and followed the visual studio docs and this video.

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

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

发布评论

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

评论(1

感受沵的脚步 2025-02-19 18:47:15

有相同的问题,当添加到链接器 - &gt时,请修复。输入 - &gt;其他依赖性:“ libmysql.lib;'然后,此'c:\ program文件\ mysql \ mysql Server 8.0 \ lib \ libmysql.lib'复制到.sln文件夹。

Have the same problem, fixed when add to Linker -> Input -> Additional Dependencies: 'libmysql.lib;' then this 'C:\Program Files\MySQL\MySQL Server 8.0\lib\libmysql.lib' copy to .sln folder.

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