This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
                                
                                    (32个答案)
                                
                        
                                4年前关闭。
            
                    
错误是:

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函数时,没有错误。因此,“包含”过程是正确的。

头文件位于/ 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


但是为什么它不能链接到功能呢?

最佳答案

您实际上并没有链接到MySQL库。在命令行上最后添加选项-lmysqlclient

关于c++ - mysql c++连接器错误:链接器命令失败,退出代码为1 ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33431226/

10-11 15:58