zoukankan      html  css  js  c++  java
  • undefined reference to `mysql_init'解决办法

    写了一个很简单的测试数据库连接的程序conn.c如下:
    #include <stdio.h>
    #include <stdlib.h>
    #include "/usr/local/mysql/include/mysql/mysql.h"
    #include <string.h>
    int main(int argc, char *argv[])
    {
    MYSQL my_connection;
    mysql_init(&my_connection);
    if (mysql_real_connect(&my_connection, "localhost", "root", "","mysql",0,NULL,CLIENT_FOUND_ROWS))
    {
        printf("Connection success\n"); 
        mysql_close(&my_connection);
    }
    else
    {
        fprintf(stderr, "Connection failed\n");
        if (mysql_errno(&my_connection))
        {
            fprintf(stderr, "Connection error %d: %s\n",mysql_errno(&my_connection),mysql_error(&my_connection));
            }
    }
        return EXIT_SUCCESS;
    }
    gcc编译:gcc -o conn conn.c出现如下错误:
    /tmp/ccY0JTdh.o(.text+0x1e): In function `main':
    : undefined reference to `mysql_init'
    /tmp/ccY0JTdh.o(.text+0x47): In function `main':
    : undefined reference to `mysql_real_connect'
    /tmp/ccY0JTdh.o(.text+0x6d): In function `main':
    : undefined reference to `mysql_close'
    /tmp/ccY0JTdh.o(.text+0x97): In function `main':
    : undefined reference to `mysql_errno'
    /tmp/ccY0JTdh.o(.text+0xad): In function `main':
    : undefined reference to `mysql_error'
    /tmp/ccY0JTdh.o(.text+0xc0): In function `main':
    : undefined reference to `mysql_errno'
    collect2: ld returned 1 exit status
    出现该错误的原因是因为编译器找不到mysql_init,mysql_close等的具体实现.虽然我们包括了正确的头文件,但是 我们在编译的时候还是要连接确定的库.对于一些常用的函数的实现,gcc编译器会自动去连接一些常用库,这样我们就没有必要自己去指定了,如:printf函数.在本程序中要通过-L选项包含库文件的路径:
    gcc -o conn conn.c -L /usr/local/mysql/lib/*.a -lz
    通过,
    用*.a将库全部包含进来拉,其实只要包含mysqlclient.a就可以. -lz什么意思我也不清楚了

    ================

    加 -lmysqlclient

  • 相关阅读:
    运营商公网
    任务管理器 的 服务与进程
    QQ通信原理及QQ是怎么穿透内网进行通信的?
    windows Telnet 客户端常用命令介绍
    redis优化
    shell反射
    USB安装centos6系统(centos7需要换软件)
    rocketmq双主模式
    golang数据类型与转换
    golang介绍
  • 原文地址:https://www.cnblogs.com/rooney/p/3003138.html
Copyright © 2011-2022 走看看