LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-28-2011, 09:30 PM   #1
chymeira
Member
 
Registered: Dec 2008
Location: CH/IL
Distribution: Slackware 13.1 Fedora 12
Posts: 73

Rep: Reputation: 16
C MySql


Hello everyone,

Im trying to write a program that interacts with MySql in C . Im using slackware 13.1 64 bit and Ive installed MySql , getting help from this page : http://www.basicconfig.com/linuxserv...ysql_slackware .

This is the program I have ( I know they arent any vars here , i just thought it wasnt necessary )
Code:
#include <stdio.h>
#include <mysql/my_global.h>
#include <mysql/my_sys.h>
#include <mysql/mysql.h>

int main(int argc, char** argv)
{
  MY_INIT(argv[0]);

  if(mysql_library_init(0, NULL, NULL))  {
    fprintf(stderr, "mysql_library_init() failed\n");
    exit(1);
  }

  conn = mysql_init(NULL);
  if(conn == NULL) {
    fprintf(stderr, "mysql_init() failed ( probably out of memory)\n");
    exit(1);
  }

  if(mysql_real_connect(conn, opt_host_name, opt_user_name, opt_password,
			opt_db_name, opt_port_num, opt_socket_name,
			opt_flags) == NULL) {
    fprintf(stderr, "mysql_real_connect() failed\n");
    mysql_close(conn);
    exit(1);
  }

  mysql_close(conn);
  mysql_library_end();
  exit(0);
}
This program is from the MySql website ( a tutorial they have up there ). When I run this program I get hundreds of lines of errors , nothing related to my program , but the headers, indicating that there are syntax errors and all kinds of other problems in them. So I was wondering if you could help me out with my problem .

Thanks in advance.
 
Old 03-28-2011, 10:08 PM   #2
nigerag
LQ Newbie
 
Registered: Feb 2008
Location: San Diego, CA
Distribution: Fedora 20, CentOS 6.5
Posts: 17

Rep: Reputation: 1
Look at the mysqld log file and see if it gives you any clue.
 
Old 03-28-2011, 10:20 PM   #3
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by chymeira View Post
When I run this program I get hundreds of lines of errors , nothing related to my program , but the headers, indicating that there are syntax errors and all kinds of other problems in them.
Syntax errors? These come when you compile the program; you may also get link errors when you attempt to build the executable. Yet above you claim you get these when you "run this program". Please elaborate a little further, because your statement makes no sense.

Please provide the GCC statement you use to compile (and link) the program. Also it would be helpful to provide the complete source listing, not just what you think is relevant.
 
Old 03-28-2011, 10:24 PM   #4
chymeira
Member
 
Registered: Dec 2008
Location: CH/IL
Distribution: Slackware 13.1 Fedora 12
Posts: 73

Original Poster
Rep: Reputation: 16
I mean when I compile , srry about that ..

This is the gcc statement :
gcc -g -o connect connect.c -lmysqlclient -lz

This is how the tutorial told me to do it , like I said I just started an hour ago.
Here are some of the errors I get ( this is like 15% )
Quote:
/usr/include/mysql/my_global.h:80:23: error: my_config.h: No such file or directory
/usr/include/mysql/my_global.h:615:26: error: my_attribute.h: No such file or directory
/usr/include/mysql/my_global.h:651:21: error: my_dbug.h: No such file or directory
In file included from connect1.c:2:
/usr/include/mysql/my_global.h:690: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'size_socket'
/usr/include/mysql/my_global.h:815:2: error: #error "please add -DSTACK_DIRECTION=1 or -1 to your CPPFLAGS"
/usr/include/mysql/my_global.h:1000:2: error: #error Neither int or long is of 4 bytes width
/usr/include/mysql/my_global.h:1070: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'os_off_t'
/usr/include/mysql/my_global.h:1569: error: static declaration of 'rint' follows non-static declaration
/usr/include/mysql/my_global.h: In function 'rint':
/usr/include/mysql/my_global.h:1579: error: 'DBL_MANT_DIG' undeclared (first use in this function)
/usr/include/mysql/my_global.h:1579: error: (Each undeclared identifier is reported only once
/usr/include/mysql/my_global.h:1579: error: for each function it appears in.)
In file included from connect1.c:3:
/usr/include/mysql/my_sys.h:34:63: error: m_ctype.h: No such file or directory
/usr/include/mysql/my_sys.h:36:21: error: typelib.h: No such file or directory
In file included from connect1.c:3:
/usr/include/mysql/my_sys.h: At top level:
/usr/include/mysql/my_sys.h:227: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'my_thread_stack_size'
/usr/include/mysql/my_sys.h:235: error: expected '=', ',', ';', 'asm' or '__attribute__' before '*' token
/usr/include/mysql/my_sys.h:236: error: expected '=', ',', ';', 'asm' or '__attribute__' before '*' token
/usr/include/mysql/my_sys.h:237: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'compiled_charsets'
/usr/include/mysql/my_sys.h:240: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'my_file_opened'
The problem is I have all those files ( my_config.h .... etc )

Thank you

Last edited by chymeira; 03-28-2011 at 10:40 PM.
 
Old 03-29-2011, 12:36 AM   #5
nigerag
LQ Newbie
 
Registered: Feb 2008
Location: San Diego, CA
Distribution: Fedora 20, CentOS 6.5
Posts: 17

Rep: Reputation: 1
Your compiler can not find the header files. Make sure you install the mysql source. In my case (I'm running Fedora) I had to install mysql-devel package.

Last edited by nigerag; 03-29-2011 at 12:38 AM.
 
Old 03-29-2011, 12:39 AM   #6
nigerag
LQ Newbie
 
Registered: Feb 2008
Location: San Diego, CA
Distribution: Fedora 20, CentOS 6.5
Posts: 17

Rep: Reputation: 1
My apologies. I've just noticed you mentioned you had these files.
 
Old 03-29-2011, 03:59 AM   #7
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Chymeira,

I'm almost certain that there is something wrong with the installation of your MySQL development package. I have this package (perhaps a meta-package) installed on my Ubuntu 10.04 system, and I had no trouble compiling/linking the following program (note, it is not important that it wont run successfully; the point it to compile it):
Code:
#include <mysql/mysql.h>

#include <stddef.h>   /* for NULL */

int main(void)
{
   MYSQL* conn = NULL;

   char hostname[20];
   char username[20];
   char password[20];
   char database[20];
   char socketname[20];
   unsigned int  port = 9000;
   unsigned long flag = 0;

   mysql_library_init(0, NULL, NULL);

   conn = mysql_init(NULL);

   mysql_real_connect(conn, hostname, username, password, database, port, socketname, flag);

   mysql_close(conn);
   mysql_library_end();

   return 0;
}
To build:
Code:
gcc -Wall mysql.c -lmysqlclient
I would recommend that you re-verify the installation of the MySQL development package. Did you use your system's package manager to install it, or did you install it manually from a tar-ball containing the source code?

EDIT:

Doh! It turns out the errors are from including the following header files:
Code:
#include <mysql/my_global.h>
#include <mysql/my_sys.h>
I guess the moral of the story is to not include them, if they are unnecessary. The other alternative, and probably the best solution, is to always compile with the path to the MySQL header files specified:
Code:
gcc -Wall -I/usr/include/mysql mysql.c -lmysqlclient

Last edited by dwhitney67; 03-29-2011 at 04:04 AM.
 
Old 03-29-2011, 10:40 AM   #8
chymeira
Member
 
Registered: Dec 2008
Location: CH/IL
Distribution: Slackware 13.1 Fedora 12
Posts: 73

Original Poster
Rep: Reputation: 16
Thank you very much for all your replies .
What I did is compile like the following ( ty for that dwhitney ) :

gcc -Wall -I /usr/include/mysql connect1.c -o connect1 -lmysqlclient

and it works fine now.
 
Old 03-29-2011, 11:09 AM   #9
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
It should be noted that by using the -I option, you no longer are required to specify the header files using their respective directory; for example, the following should be sufficient when using -I/usr/include/mysql:
Code:
#include <mysql.h>

...
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Can MySQL log on via SSH/bash? mysql:x:27:101:MySQL Server:/var/lib/mysql:/bin/bash Ujjain Linux - Newbie 2 04-24-2009 02:21 PM
yum install php-mysql fails with mysql 5.1 - "Error: mysql conflicts with MySQL" rebelde Linux - Software 2 03-13-2009 10:32 AM
mysql error Can't connect to local MySQL server through socket '/var/lib/mysql/mysql. SpellChainz Linux - Newbie 1 06-23-2007 03:35 PM
mysql error Can't connect to local MySQL server through socket '/var/lib/mysql/mysql. Dannux Linux - Software 3 03-24-2006 08:44 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:52 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration