【问题标题】:VSCode c++ mysql.h undefined reference to 'mysql_init@4'VSCode c++ mysql.h 未定义引用'mysql_init@4'
【发布时间】:2020-03-26 21:11:31
【问题描述】:

我正在尝试让 C++ 在 VSCode 中与 mysql.h 一起使用。我已将所有文件(.h、libmysql.lib、...)与我的“main.cpp”文件放在同一个文件夹中,并尝试使用

执行程序
g++ main.cpp -I"D:\Projects\New project"

我不断收到以下错误:

c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\user\AppData\Local
\Temp\ccYwlVSu.o:main.cpp:(.text+0x43): undefined reference to `mysql_init@4'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\user\AppData\Local          
\Temp\ccYwlVSu.o:main.cpp:(.text+0x8c): undefined reference to `mysql_real_connect@32'
collect2.exe: error: ld returned 1 exit status

我已经读到我需要正确链接我的所有库,我认为我正在使用“-I”命令执行此操作。我错过了什么?

main.cpp

#include <iostream>
#include <windows.h>
#include <mysql.h>

int main(){
    MYSQL* conn;
    conn = mysql_init(0);
    conn = mysql_real_connect(conn, "localhost", "root", "", "airlines_db", 0, NULL, 0);

    if(conn){
        std::cout << "Connected" << std::endl;
    } else {
        std::cout << "Not connected" << std::endl;
    }
}

tasks.json

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "shell",
            "label": "echo",
            "command": "g++",
            "args": [
                "-g", "main.cpp"
            ],
            "group": {
                "kind" : "build",
                "isDefault": true
            }
        }
    ]
}

c_cpp_properties.json

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "compilerPath": "C:\\MinGW\\bin\\gcc.exe",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "clang-x86",
            "browse": {
                "path": [
                    "C:\\MinGW\\lib\\gcc\\mingw32\\9.2.0\\include\\c++"
                ]
            }
        }
    ],
    "version": 4
}

【问题讨论】:

  • 编译命令中可能缺少mysql库。
  • 我没有看到任何链接到 mysql 库的证据。
  • 我用 -I 命令指向我的文件夹。这还不够吗?

标签: c++ mysql visual-studio-code g++ mingw


【解决方案1】:

我想通了 - 我的命令中缺少一个 -lmysql。我已经添加了,现在一切正常!

【讨论】:

    猜你喜欢
    • 2012-06-13
    • 2022-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多