C API for MySQL

This is a discussion on C API for MySQL within the C Programming forums, part of the General Programming Boards category; Help me please, I am having a problem getting the C API to work with in C. I have tried ...

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    184

    C API for MySQL

    Help me please,

    I am having a problem getting the C API to work with in C. I have tried it with DevC++ as a C program, I have tried it in DJGPP, I have tried it in MingW. First off, to my understanding, C library files are named as follows: libmysqlclient.a. Well in the C API that comes with MYSQL it is named mysqlclient.lib. It says it was compiled with VC5.0. Secondly, when I include my_global.h, I get a bunch of errors. One is that my_config.h cannot be found. When I look for it, it is no where in the include folder. I have gone through the MYSQL manual and followed all of the examples, but keep having problems. I have a code file as follows that will not compile and link:

    Code:
    #include <my_global.h>
    #include <mysql.h>
    
    int main(int argc, char *argv[])
    {
       return 0;
    }
    That will not even compile. If I take the my_global.h out, it will compile. But then I cannot get it to link. I add the -lmysqlclient but it says it cannot find the library. It is name as follows mysqlclient.lib. I don't know what to do. I have fought this thing for days. Any suggestions?????

    Thanks,
    Kendal

  2. #2
    Registered User sean345's Avatar
    Join Date
    Mar 2002
    Posts
    346
    > #include <my_global.h>
    This says to include my_global.h file and tells the compiler to look in the include folder for your compiler. If this file is in the same folder as your source code then it should look like this:
    Code:
    #include "my_global.h"
    > mysqlclient.lib
    I have never used this before so I am not exactly sure. You have to have this library to use it. If your compiler does not come with it then you have to download it.

    Hope that helps

    - Sean
    If cities were built like software is built, the first woodpecker to come along would level civilization.
    Black Frog Studios

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    184
    my_global.h and mysqlclient.lib came with mysql when I downloaded and installed it. The C API was included with mysql. The problem is getting any program that uses the C API to compile and link with a C compiler. I am using the gcc compiler. If I include my_global.h, I get compiler errors along the lines of missing header files, redefinition, syntax errors with the headers files. If I remove my_global.h, I get undefined reference linker errors. I am pretty sure that that has to do with linking the mysqlclient.lib. The mysql\lib\opt directory has the following files:

    libmySQL.dll
    libmySQL.lib
    mysqlclient.lib
    zlib.lib

    I add the include directory and the lib\opt directory in to the compile and link commands, but still get the errors. Any suggestions?????

    Thanks again,
    Kendal

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    385
    What is the exact command you are using to compile it?
    Wandering aimlessly through C.....

    http://dbrink.phpwebhosting.com

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    184
    I have tried many variations, here is one:

    cpp test.c -o test.exe -I"c:\mysql\include" -L"C:\mysql\lib\opt"
    -lmysqlclient

    The main problem with this command is the linking of the library. The path is correct, the compiler says it cannot find the library. I have a list of the libraries above. I have tried many variations of the library, libmysqlclient, mysqlclient.lib, libmysqlclient.lib, just about everything I can think of. Thanks for taking the time to look at this post.

    Thanks,
    Kendal

  6. #6
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544

  7. #7
    Registered User
    Join Date
    Feb 2003
    Posts
    184
    I have read through those pages and followed the instructions. I have tried downloading the client lib for mingw, i have tried using pexport and the dlltool. I have tried all of that and I cannot get anything to work. It still cannot find global.h. If I remove the myglobal include line, I still get linker errors. Here are some of them:

    Code:
    Compiler: Default compiler
    Executing  gcc.exe...
    gcc.exe "F:\Dev-Cpp\Code\MYTest1.c" -o "F:\Dev-Cpp\Code\MYTest1.exe"   -fmessage-length=0  -I"F:\Dev-Cpp\include"  -I"F:\MySQL\include"   -L"F:\Dev-Cpp\lib" -L"F:\MySQL\lib\opt" -L"F:\MySQL\lib\opt\New" -lmysql
    In file included from F:/MySQL/include/mysql.h:57,
                     from F:/Dev-Cpp/Code/MYTest1.c:1:
    F:/MySQL/include/mysql_com.h:116: parse error before "SOCKET"
    F:/MySQL/include/mysql_com.h:116: warning: no semicolon at end of struct or union
    F:/MySQL/include/mysql_com.h:135: parse error before '}' token
    F:/MySQL/include/mysql_com.h:135: warning: data definition has no type or storage class
    F:/MySQL/include/mysql_com.h:167: parse error before '*' token
    F:/MySQL/include/mysql_com.h:168: parse error before '*' token
    F:/MySQL/include/mysql_com.h:169: parse error before '*' token
    F:/MySQL/include/mysql_com.h:170: parse error before '*' token
    F:/MySQL/include/mysql_com.h:171: parse error before '*' token
    F:/MySQL/include/mysql_com.h:172: parse error before '*' token
    F:/MySQL/include/mysql_com.h:173: parse error before '*' token
    F:/MySQL/include/mysql_com.h:175: parse error before '*' token
    F:/MySQL/include/mysql_com.h:176: parse error before '*' token
    F:/MySQL/include/mysql_com.h:180: parse error before "s"
    
    In file included from F:/Dev-Cpp/Code/MYTest1.c:1:
    F:/MySQL/include/mysql.h:165: parse error before "NET"
    F:/MySQL/include/mysql.h:165: warning: no semicolon at end of struct or union
    F:/MySQL/include/mysql.h:202: parse error before '}' token
    F:/MySQL/include/mysql.h:202: warning: data definition has no type or storage class
    F:/MySQL/include/mysql.h:211: parse error before "MYSQL"
    F:/MySQL/include/mysql.h:211: warning: no semicolon at end of struct or union
    F:/MySQL/include/mysql.h:217: parse error before '}' token
    F:/MySQL/include/mysql.h:217: warning: data definition has no type or storage class
    F:/MySQL/include/mysql.h:232: parse error before "NET"
    That is coming directly from my Dev-C++ compile log. I just cannot get anything to work. Any suggestions

    Thanks,
    Kendal

  8. #8
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    They are compiler errors.
    It seems that your compiler is having trouble with this structure:
    Code:
    00113 typedef struct st_net {
    00114   Vio* vio;
    00115   my_socket fd;                                 /* For Perl DBI/dbd */
    00116   int fcntl;
    00117   unsigned char *buff,*buff_end,*write_pos,*read_pos;
    00118   char last_error[MYSQL_ERRMSG_SIZE];
    00119   unsigned int last_errno,max_packet,timeout,pkt_nr;
    00120   unsigned char error;
    00121   my_bool return_errno,compress;
    00122   my_bool no_send_ok; /* needed if we are doing several
    00123    queries in one command ( as in LOAD TABLE ... FROM MASTER ),
    00124    and do not want to confuse the client with OK at the wrong time
    00125                       */
    00126   unsigned long remain_in_buf,length, buf_length, where_b;
    00127   unsigned int *return_status;
    00128   unsigned char reading_or_writing;
    00129   char save_char;
    00130 } NET;
    However, this was taken from the web and is probably different to your version so please paste your version from mysql_com.h 116.

    My guess would be that the SOCKET (alias my_socket above) type is not defined. Try including <winsock.h> above all your other headers.

    All the other errors probably relate to the NET type not being declared.

  9. #9
    Registered User
    Join Date
    Feb 2003
    Posts
    184
    You were right. I added the winsock.h and that corrected the compiler problems. I could have sworn that I tried that before and it didn't work. Anyway, after that I tried compiling a complete sample and got some linker errors. So I went to one of the links you previously posted and downloaded the .a client library for mysql. When I added winsock.h and used the downloaded client library, the program compiles and links. So a combination of both makes it work for the time being. If I run into any more problems with mysql, I will post back on this post.

    Thanks a million,
    Kendal

  10. #10
    Registered User
    Join Date
    Dec 2006
    Posts
    8
    Try this
    gcc -o outputname infile.c `mysql_config --cflags --libs`
    Hope this will help

  11. #11
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    You can help us by reading rule #3
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. platform specific API or C standard API
    By George2 in forum C Programming
    Replies: 1
    Last Post: 11-12-2007, 12:32 AM
  2. OpenSSL and Win32 SSL API :: SSL/TLS
    By kuphryn in forum Networking/Device Communication
    Replies: 0
    Last Post: 03-10-2004, 06:46 PM
  3. FILES in WinAPI
    By Garfield in forum Windows Programming
    Replies: 46
    Last Post: 10-02-2003, 06:51 PM
  4. OLE Clipboard :: Win32 API vs. MFC
    By kuphryn in forum Windows Programming
    Replies: 3
    Last Post: 08-11-2002, 05:57 PM
  5. pthread api vs win32 thread api
    By Unregistered in forum Windows Programming
    Replies: 1
    Last Post: 11-20-2001, 07:55 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21