Troubles with C's MySQL API

Belaran

Registered
Hi !

I tried to compil a simple programs with the mysql.h include and both gcc and cc gives me this :

main.C:10:19: mysql.h: No such file or directory
main.C:15: error: syntax error before `*' token
main.C:16: error: `MYSQL' was not declared in this scope
main.C:16: error: `db' was not declared in this scope
main.C:16: error: variable or field `db_disconnect' declared void


How should i include the mysql folder to the inclusion path ? ( if this how to do this)
Is there some gcc or cc options i should use ?

Belaran
On 400Mhz PowerPC G3
Mac OS X 10.3.2
 
Belaran said:
Hi !

I tried to compil a simple programs with the mysql.h include and both gcc and cc gives me this :

main.C:10:19: mysql.h: No such file or directory
main.C:15: error: syntax error before `*' token
main.C:16: error: `MYSQL' was not declared in this scope
main.C:16: error: `db' was not declared in this scope
main.C:16: error: variable or field `db_disconnect' declared void


How should i include the mysql folder to the inclusion path ? ( if this how to do this)
Is there some gcc or cc options i should use ?

Belaran
On 400Mhz PowerPC G3
Mac OS X 10.3.2

Do "man gcc" and pay particular attention to the -I option.

Wade
 
Ok
i finally compil my source file but not with -lmysql nor -lmysqlclient because the two link were not recognized. I add to my file "usr/local/mysql/include/mysql.h". But i know i got this

[Belaran:~/Desktop/projet/sources] belaran% gcc main.c
ld: warning prebinding disabled because of undefined symbols
ld: Undefined symbols:
_mysql_init
_mysql_options
_mysql_real_connect
[Belaran:~/Desktop/projet/sources] belaran% grep "mysql_init" /usr/local/mysql/include/mysql.h
MYSQL * STDCALL mysql_init(MYSQL *mysql);
[Belaran:~/Desktop/projet/sources] belaran% grep "mysql_options" /usr/local/mysql/include/mysql.h
struct st_mysql_options {
struct st_mysql_options options;
int STDCALL mysql_options(MYSQL *mysql,enum mysql_option option,
[Belaran:~/Desktop/projet/sources] belaran% grep "mysql_real_connect" /usr/local/mysql/include/mysql.h
MYSQL * STDCALL mysql_real_connect(MYSQL *mysql, const char *host,


... So the unknown items are present in the header !!!
But the objects may not be , so i tried to see what was inside the mysql*.a to find them :


ar -P libmysqlclient.a
ar: illegal option -- P
usage: ar -d [-TLsv] archive file ...
ar -m [-TLsv] archive file ...
ar -m [-abiTLsv] position archive file ...
ar -p [-TLsv] archive [file ...]
ar -q [-cTLsv] archive file ...
ar -r [-cuTLsv] archive file ...
ar -r [-abciuTLsv] position archive file ...
ar -t [-TLsv] archive [file ...]
ar -x [-ouTLsv] archive [file ...]

So where are my API ? Why ar isn't fonctionning correctly ????
 
Belaran said:
Ok
i finally compil my source file but not with -lmysql nor -lmysqlclient because the two link were not recognized. I add to my file "usr/local/mysql/include/mysql.h".

You're getting confused between libraries and headers. Adding the mysql.h header will allow your program to compile, but it doesn't do anything for the linking.

But i know i got this

[Belaran:~/Desktop/projet/sources] belaran% gcc main.c
ld: warning prebinding disabled because of undefined symbols
ld: Undefined symbols:
_mysql_init
_mysql_options
_mysql_real_connect

Exactly. See the "ld: Undefined symbols:"? That's the linker. It's telling you you have not linked with the library that contains those symbols.

... So the unknown items are present in the header !!!

No, the declaration is present in the header. The code is present in the library.

ar -P libmysqlclient.a
ar: illegal option -- P
usage: ar -d [-TLsv] archive file ...
ar -m [-TLsv] archive file ...
ar -m [-abiTLsv] position archive file ...
ar -p [-TLsv] archive [file ...]
ar -q [-cTLsv] archive file ...
ar -r [-cuTLsv] archive file ...
ar -r [-abciuTLsv] position archive file ...
ar -t [-TLsv] archive [file ...]
ar -x [-ouTLsv] archive [file ...]

So where are my API ? Why ar isn't fonctionning correctly ????

ar is functioning correctly.

Where do you see a -P (uppercase) option there? The only -p option is lowercase and that's why ar is complaining.

But ar has nothing to do with examining the symbols in a library anyhow. For that, you want to use the nm command.

I'm willing to bet the symbols you need are in libmysql.a, so you need to add -lmysql to your gcc command. If libmysql.a exists in a non-standard location, you'll also need to add -L/path/to/libmysql.a to tell gcc where to find the library.

Wade
 
okay

so i tried to check if i had my "missing symbols" inside the .a :

nm libmysqlclient.a | grep -e mysql_init
00003020 T _mysql_init
nm: no name list
...

So it appears that i got the missing symbols in this .a so i tried compiled this way :

belaran% gcc -L/usr/local/mysql/lib/libmysqlclient.a -L/usr/local/mysql/include/mysql.h main.c
ld: warning prebinding disabled because of undefined symbols
ld: Undefined symbols:
_mysql_init
_mysql_options
_mysql_real_connect

I must be doing something wrong here , because gcc can't file item figuring inside the ar.

PS : to simplify the compilation i had "/usr/local/mysql/include/mysql.h" into a header included in main.C
 
Code:
belaran% gcc -L/usr/local/mysql/lib/libmysqlclient.a -L/usr/local/mysql/include/mysql.h main.c
ld: warning prebinding disabled because of undefined symbols
ld: Undefined symbols:
_mysql_init
_mysql_options
_mysql_real_connect

You're still not understanding the usage of the different flags.

-l tells what library to link with (the lib and .a or .dylib part of the name are automatically added, so if the lib is libmysql.a, you specify -lmysql)
-L tells where to search for that library
-I tells where to search for headers

What you want is:

gcc -I/usr/local/mysql/include -lmysqlclient -L/usr/local/mysql/lib main.c


PS : to simplify the compilation i had "/usr/local/mysql/include/mysql.h" into a header included in main.C

This is unnecessary if the flags are specified correctly.

Wade
 
Back
Top