【问题标题】:cannot link to static mysqlclient library, though shared library works无法链接到静态 mysqlclient 库,尽管共享库有效
【发布时间】:2020-02-22 02:57:56
【问题描述】:

我有一个示例程序来熟悉 mysqlclient API。但是,当我静态编译并将其链接到 mysqlclient 库(.a 文件)时,链接器抱怨它找不到该文件,尽管它存在于我的路径中。链接到共享库(我的 Mac 上的.dylib 文件)有效。 请帮助我了解这种行为。非常感谢!

这是我的驱动程序client.c,它调用了mysqlclient 库。

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include <mysql.h>

int main(int argc, char **argv) 
{
  MYSQL *mysql = NULL;

  if (mysql_library_init(argc, argv, NULL)) {
    fprintf(stderr, "could not initialize MySQL client library\n");
    exit(1);
  }

  mysql = mysql_init(mysql);

  if (!mysql) {
    puts("Init faild, out of memory?");
    return EXIT_FAILURE;
  }

  if (!mysql_real_connect(mysql,       /* MYSQL structure to use */
              NULL,         /* server hostname or IP address */ 
              NULL,         /* mysql user */
              NULL,          /* password */
              NULL,               /* default database to use, NULL for none */
              0,           /* port number, 0 for default */
              NULL,        /* socket file or named pipe name */
              CLIENT_FOUND_ROWS /* connection flags */ )) {
    puts("Connect failed\n");
  } else {                
    const char *query = "SELECT VERSION()";
    if (mysql_real_query(mysql, query, strlen(query))) {
      printf("Query failed: %s\n", mysql_error(mysql));
    } else {
      puts("Query OK");
    }
  }

  mysql_close(mysql);

  mysql_library_end();

  return EXIT_SUCCESS;
}

这是我的编译方法

gcc -I /usr/local/Cellar/mysql/8.0.16/include/mysql client.c -L /usr/local/Cellar/mysql/8.0.16/lib/ -l mysqlclient.a
ld: library not found for -lmysqlclient.a
clang: error: linker command failed with exit code 1 (use -v to see invocation)

没有.a 的编译成功,因为它链接到共享库,而不是静态库。

最后,这是我的库文件:

ls /usr/local/Cellar/mysql/8.0.16/lib/libmysqlclient*
/usr/local/Cellar/mysql/8.0.16/lib/libmysqlclient.21.dylib /usr/local/Cellar/mysql/8.0.16/lib/libmysqlclient.a        /usr/local/Cellar/mysql/8.0.16/lib/libmysqlclient.dylib

【问题讨论】:

    标签: mysql c linker static-linking dynamic-linking


    【解决方案1】:

    这个论点:

     -l mysqlclient.a
    

    使链接器查找名为libmysqlclient.a.a 的文件。相反,你想要这样的东西:

    gcc -I /usr/local/Cellar/mysql/8.0.16/include/mysql client.c /usr/local/Cellar/mysql/8.0.16/lib/mysqlclient.a
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-27
      • 2018-04-09
      • 1970-01-01
      • 2015-10-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多