mysql c++ connector error: linker command failed with exit code 1

mysql c++ connector error: linker command failed with exit code 1

错误是:

Undefined symbols for architecture x86_64:
  "_mysql_init", referenced from:
      _main in dao-8fe5b4.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

当我尝试使用以下命令编译 cpp 文件时。

g++ -o test_install   -I/usr/local/include -I/usr/local/include/cppconn -L/usr/local/lib dao.cc

dao.cc 文件是:

#include <iostream>
#include <cstdio>
#include <mysql/mysql.h>

using namespace std;

int
main() {
    MYSQL mysql;
    mysql_init(&mysql);
    return 0;
}

当我不调用mysql_init函数时,没有报错。所以"include"程序是正确的。

头文件在/usr/local/inclulde和/usr/local/cppconn中,在/usr/local/lib中,lib文件是:

libmysqlclient.18.dylib        libmysqlcppconn-static.a
libmysqlclient.a               libmysqlcppconn.7.1.1.6.dylib
libmysqlclient.dylib           libmysqlcppconn.7.dylib
libmysqlclient_r.18.dylib      libmysqlcppconn.dylib
libmysqlclient_r.a             libmysqld.a
libmysqlclient_r.dylib         libmysqlservices.a

但是为什么不能link功能呢?

您实际上 link MySQL 库。在命令行中添加选项 -lmysqlclient last.