Accessing MySQL from C

Derrick Ryalls freebsd at fbsdsolutions.com
Tue Apr 20 11:42:07 PDT 2004


I am trying to write a little program that will talk to my existing mysql db
server (4.1.1), but am having compile issues.

The code is:

#include <stdio.h>
#include <mysql.h>

#define HOST "localhost"
#define USERNAME "test"
#define PASSWORD "test"
#define DB "pollphp"

MYSQL *conn;

int main()
{
        conn = mysql_init(NULL);
}

And here are various compile attempts:

# gcc -o sqltest sqltest.c
sqltest.c:2: mysql.h: No such file or directory
# gcc -L/usr/local/include/mysql -o sqltest sqltest.c
sqltest.c:2: mysql.h: No such file or directory

If I change the above include to:
#include </usr/local/include/mysql/mysql.h>

I get this on compile:

# gcc -o sqltest sqltest.c
/tmp/ccrC5KFf.o: In function `main':
/tmp/ccrC5KFf.o(.text+0xc): undefined reference to `mysql_init'

And if I and the -l flag:
# gcc -o sqltest -l/usr/local/lib/mysql/libmysqlclient.so sqltest.c
/usr/libexec/elf/ld: cannot find -l/usr/local/lib/mysql/libmysqlclient.so
(I also tried libmysqlclient.a and libmysqlclient)

I have gotten as far as I can with the man pages for gcc, and google isn't
helping much.  Does anyone know how I can compile a program that talks to
mysql?

TIA

-Derrick



More information about the freebsd-questions mailing list