MySQL connection

Pages: 12
I'm writing a program to connect to a MySQL v5.0 database. I did a #include <mysql.h> and I get an error saying there's no such file or directory. Any ideas as to where I can find that header file and how to install it so it can be found by my compiler(Dev C++)?
You will need to download the mysql server from their website - which will
come with the header files and appropriate libraries.
Download a program called xamplite I think its called.
Well I downloaded MySQL v5.0 Community Server. That's been installed already but the header files it has associated with it don't automatically get put into the include folder under the compliler's location. And I looked through the MYSQL folder and couldn't find any header files to copy over to it.
Assuming you are talking about a windows installation:
If you had done it correctly- you would have found an include directory and a lib directory in C:\Program Files\MySQL\MySQL Server 5.0.
If they are not there, see how to add the developers components in my post below
Last edited on
There is still a way out - if you go to Control Panel - Add/ Remove Programs, and select MySQl Server 5.0 and click change.
The mysql installer will appear, press next , select Modify and click next.
It will display the mysql component parts and you should see Developer Components. listed.
Select each Developer Component to be Run from Hard Disk
Last edited on
OK. I finally got the mysql.h file included and it's in the proper place. At this point i'm trying to simply open a connection to the database. The code to do so is below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <conio.h>
#include <string>
#include <fstream>
#include <windows.h>
#include <C:\\Program Files\\MySQL\\MySQL Server 5.0\\include\\mysql.h>

using namespace std;

int main()
   {
   MYSQL* conn;
   
   conn = mysql_init(NULL);
   mysql_real_connect(conn,HOST,USERNAME,PASSWORD,DATABASE,0,NULL,0);   
   return 0;       
   }


My problem now is apparently a library file. The compiler gives me 3 linker errors:

1
2
3
[Linker error] undefined reference to `__cpu_features_init' 
[Linker error] undefined reference to `mysql_init@4' 
[Linker error] undefined reference to `mysql_real_connect@32'  


I've read a few other threads about these issues and i've tried some of the solutions, none of which have fixed my problem. Is it a library issue? If so, how do i fix it?

Using Windows XP and Dev-C++
Last edited on
You need to link with the mysqllib.lib.
The debug version of this library is in C:\Program Files\MySQL\MySQL Server 5.0\lib\debug directory.
The release verson is in C:\Program Files\MySQL\MySQL Server 5.0\lib\opt.

You also need to make sure that libmysql.dll is either in your
project working directory or in the windows system directory so that Windows can find it to load it when your program is run.
Ok now how do i link the libmysql.lib? I've never had to do any linking before and im' really not sure what steps to take.
well, it's really different for each IDE/compiler setup, but with DevC++ you go to 'Project', then in the drop down menu select 'Project Options' ( this does require that you set up your program as a project ). in the box that pops up select the 'Parameters' tab. Then, in the 'Linker' column, click the button that says 'Add Library or Object' and locate the library file. Then, when you compile your project, the linker will reference and link the code as necessary from that library.
Ok, I have the libraries link however now it's giving me many many errors all saying something along the lines of:

"variable '_iob' can't be auto-imported. Please read the documentation for Id's --enable auto import for details"

Not sure what that's supposed to mean or what to do about it.
To cut a long story short - can you post your code?
The code is posted above. It's the 7th post on this subject. And the error is posted above your comment.
I'm sorry to say, that I made a DEVC++ project, copied your code into it.
Went to Project Options and set up the include path and library path.
Told it to link with mysqllib.lb. Copied the libmysql.dll file from the mysql from
the C:\Program Files\MySQL\MySQL Server 5.0\lib\debug folder into my
devc++ project folder.

Of course I had to change the host, user, password, and database parameters
in your code to match my setup --- it compiled and ran with no problems at all.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <conio.h>
#include <string>
#include <fstream>
#include <windows.h>
#include <C:\\Program Files\\MySQL\\MySQL Server 5.0\\include\\mysql.h>

using namespace std;

int main()
   {
   MYSQL* conn;
   
   conn = mysql_init(NULL);
   if (mysql_real_connect(conn,"","root","admin123","mysql",0,NULL,0) !=0)
   {
      cout << "Succesfully  Connected to MySQL database xxxx" << endl;  
    } 
   
    mysql_close(conn);
    system("PAUSE");    
   return 0;       
   }
Last edited on
Ok, the only difference I can find between our code is the library we're linking to. I've checked in all the MySQL folders and cannot find a filed called mysqllib.lib. The only thing similar to that is libmysql.lib. Are these the same? or am I missing a file? And as for the DLL file, It just needs to be in the same location as the .cpp files, correct?
Oops - my mistake - it is libmysql.lib.

The dll file should be in a place where windows can find it when it launces the program. So the two best places are either the system32 directory
or in the working directory for the program (which in the case of DEVC++ is where
the cpp files are)
Ok below i've listed exactly how i've tried linking the program including all software versions and such. Let me know if anything seems wrong or abnormal.

First I copied the libmysql.dll file from the following location:
C:\Program Files\MySQL\MySQL Server 5.0\lib\debug
into both the system32 file and the file containing the .cpp files for this project.

Next, I linked the libmysql.lib to the rest of the project. I did this by going into the project options, and under parameters adding a library. This is the text that is now in the linker text area:
"../../../../../Program Files/MySQL/MySQL Server 5.0/lib/debug/libmysql.lib"

I'm currently running Dev-C++ v4.9.9.2
Windows XP
MySQL Server 5.0

When I compile the project I get 3 errors:


[Linker error] undefined reference to `__cpu_features_init'
[Linker error] undefined reference to `mysql_init@4'
[Linker error] undefined reference to `mysql_real_connect@32'


And the code being compiled is below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <conio.h>
#include <string>
#include <fstream>
#include <windows.h>
#include <C:\\Program Files\\MySQL\\MySQL Server 5.0\\include\\mysql.h>

using namespace std;
int main()
   {
   MYSQL* conn;
   
   conn = mysql_init(NULL);
   if(mysql_real_connect(conn,HOST,USERNAME,PASSWORD,DATABASE,0,NULL,0) != 0)
         cout << "Successfully connected to database!";
      else
         cout << "Was not able to connect to database";      
   return 0;
   }


The parameters for the mysql_real_connect() function have been declared as global constants using #define

I can't think of anything else you might need to know. So hopefully this'll shed some light on the subject.

Thank you very very much!
it is possible it is just a devcpp bug... if you notice all the slashes and dots in front of the library you linked, but the first actual folder listed, 'Program Files' was confirmed to only be in C:\ several lines earlier in your post. While using the 'Add' button normally works, I always check because i've had problems with this in the past. Try just writing out the full path manually, just change "../../../../../Program Files/MySQL/MySQL Server 5.0/lib/debug/libmysql.lib" to
"C:/Program Files/MySQL/MySQL Server 5.0/lib/debug/libmysql.lib"
and see if it recognizes the library then. if that doesn't do it, i'll have to look into it more. good luck.
Good idea, but it didn't work. I tried just changing it to C:\ and I also tried cahnging all the foward slashes to backslashes and that didn't work either.
yeah, devcpp uses just the forward slashes...
um, well, just in case, did you change the beginning to 'C:/', or did you use a backslash after the 'C:', because all the slashes need to be forward slashes, and make sure there are quotes around it, since it contains spaces.

beside that, i mean, to get those linker errors, either it is not able to find the library, or those functions aren't in that library. one other suggestion, is that while in theory you're supposed to be able to, i've never actually used a library with the '.lib' extension in devcpp. I would try going to the actual '.lib' file and manually changing it's extension to '.a', then add that to the parameters page. all the libraries included with the mingw install of DevCpp have that extension, and i've always renamed libraries that i use, so maybe try that and see if it works. best luck, let me know how it goes.
Pages: 12