英文:
How to compile with gcc on Mac OS using mariadb-connector-c
问题
我已成功在Mac OS Monterey上使用Homebrew安装了mariadb和mariadb-connector-c,但无法弄清如何使用`gcc`编译一个简单的C源文件。如果我尝试:
gcc -o test main.c
main.c:1:10: fatal error: 'mysql.h'文件未找到
尝试一些不同的方法,如果我根据MySQL的使用自己的连接器-c的指令[这里](https://dev.mysql.com/doc/c-api/8.0/en/c-api-building-clients.html),我尝试了:
gcc -o main main.c /usr/local/bin/mariadb_config
ld: 无法为架构x86_64链接到主可执行文件'/usr/local/bin/mariadb_config'
我尝试的另一件事是根据这里的答案[这里](https://stackoverflow.com/a/17265849/19847712)来适应mariadb:
gcc -o test main.c $(mariadb_config --libs)
main.c:1:10: fatal error: 'mysql.h'文件未找到
如何在Mac上编译使用mariadb-connector-c的C程序?
FYI,这是`main.c`:
```c
#include <mysql.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
MYSQL *con = mysql_init(NULL);
if (con == NULL)
{
fprintf(stderr, "%s\n", mysql_error(con));
exit(1);
}
if (mysql_real_connect(con, "localhost", "(...)", "(...)", NULL, 0, NULL, 0) == NULL)
{
fprintf(stderr, "%s\n", mysql_error(con));
mysql_close(con);
exit(1);
}
if (mysql_query(con, "CREATE DATABASE testdb"))
{
fprintf(stderr, "%s\n", mysql_error(con));
mysql_close(con);
exit(1);
}
mysql_close(con);
printf("MySQL客户端版本:%s\n", mysql_get_client_info());
exit(0);
}
<details>
<summary>英文:</summary>
I have successfully installed mariadb and mariadb-connector-c on Mac OS Monterey using Homebrew but cannot figure out how to compile a simple c source file with `gcc`. If I try:
gcc -o test main.c
main.c:1:10: fatal error: 'mysql.h' file not found
Trying something different, if I adapt the MySQL instructions for using its own connector-c [here](https://dev.mysql.com/doc/c-api/8.0/en/c-api-building-clients.html), I've tried:
gcc -o main main.c /usr/local/bin/mariadb_config
ld: can't link with a main executable file '/usr/local/bin/mariadb_config' for architecture x86_64
Another thing I've tried is adapting the answer [here](https://stackoverflow.com/a/17265849/19847712) for mariadb:
gcc -o test main.c $(mariadb_config --libs)
main.c:1:10: fatal error: 'mysql.h' file not found
How can I compile a c program that uses mariadb-connector-c on Mac?
FYI, here is `main.c`:
#include <mysql.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
MYSQL *con = mysql_init(NULL);
if (con == NULL)
{
fprintf(stderr, "%s\n", mysql_error(con));
exit(1);
}
if (mysql_real_connect(con, "localhost", "(...)", "(...)", NULL, 0, NULL, 0) == NULL)
{
fprintf(stderr, "%s\n", mysql_error(con));
mysql_close(con);
exit(1);
}
if (mysql_query(con, "CREATE DATABASE testdb"))
{
fprintf(stderr, "%s\n", mysql_error(con));
mysql_close(con);
exit(1);
}
mysql_close(con);
printf("MySQL client version: %s\n", mysql_get_client_info());
exit(0);
}
</details>
# 答案1
**得分**: 1
gcc需要知道在哪里找到MariaDB Connector/C的包含文件(在大多数情况下是mysql.h)以及MariaDB C/C库的位置和名称。
您可以使用`-I/include_path` `-L/library_path` `-lmariadb`来指定它们,或者您可以使用mariadb_config:
```bash
gcc -o test main.c $(mariadb_config --include --libs)
来源:https://mariadb.com/docs/skysql-previous-release/connect/programming-languages/c/development/
英文:
gcc needs to know where to find both MariaDB Connector/C include files (in most cases mysql.h) and the location and names of the MariaDB C/C libraries.
You can either specify them with -I/include_path
-L/library_path
-lmariadb
or you can use mariadb_config:
gcc -o test main.c $(mariadb_config --include --libs)
Source: https://mariadb.com/docs/skysql-previous-release/connect/programming-languages/c/development/
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论