Static linking of MySQL in C/C++ - c++

I am trying to develop an application that uses MySQL using C++. I downloaded the library from their website and I have attempted to compile the following code:
#include <iostream>
#include <windows.h>
#include <mysql.h>
using namespace std;
int main()
{
MYSQL *connection, mysql;
MYSQL_RES *result;
MYSQL_ROW row;
mysql_init(&mysql);
}
The line that has mysql_init(&mysql); gives me a compilation error
undefined reference to `mysql_init#4'
I am guessing this is due to a library error. I am linking mysqlclient.lib and libmysql.lib in that order. What do I need to do to make this compile without requiring a dll file? Is that possible? Thank you.
Note: I am using mingw32 on Windows 7 x64 to develop an application for Windows.

Maybe this link will help. It states that mysqlclient.lib is the static library and libmysql.lib is the dynamic library, so I don't think you should be linking both.
http://dev.mysql.com/doc/refman/5.0/en/building-clients.html

Related

c++ mysql connection bad_alloc using c++ connector

Trying to build a simple mysql connection, but getting a bad_alloc and I can't figure out how to solve this, even looking at similar posts
here is my code
#include <iostream>
#include <stdlib.h>
#include <sstream>
#include <memory>
#include <string>
#include <stdexcept>
#include "mysql_connection.h"
#include "cppconn\driver.h"
#include "cppconn\statement.h"
#include "cppconn\connection.h"
#include "cppconn\exception.h"
#include "cppconn\prepared_statement.h"
#include "cppconn\statement.h"
using namespace std;
using namespace sql;
int main()
{
Driver *driver;
Connection *conn;
Statement *stmt;
driver = get_driver_instance();
conn = driver->connect("127.0.0.1:3306", "root", "root"); // breaks here
stmt = conn->createStatement();
stmt->execute("USE mydb");
return 0;
}
I'm using mysql-connector-c++-1.1.7
mysqlcppconn.lib is used as a dependencies
mysqlcppconn.dll is located in the same dir as the .exe is.
Here is the error Exception thrown at 0x000007FEFD39A06D in MysqlConn.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x000000000014F5D0.
Thanks
I also had this error. In my case I am compiling using VS2015 in Windows.
First time I choose compile static version of the MySQL lib. Then later I decided to compile the dynamic version. This time the error bad_alloc at memory went off.
The solution is rolling back the CPPCONN_PUBLIC_FUNC= configuration.
Go to project Property Pages, under C++ > Preprocessor > Preprocessor Definitions and remove the item CPPCONN_PUBLIC_FUNC=".
I had the same error on linux.
The error was : I was using g++-4.8 to build the project
The issue lies on the version of the build tools (gcc,msvs,clang) used to build the project

Undefined symbols for architecture x86_64 - Compile error with Boost and Sockets.io (C++)

This is a problem I've been having for a solid week now; I am fairly new to C++ programming and am trying to compile the very basics of the socket.io C++ library in xcode 6.
This is the code I am using.
#include <iostream>
#include <string>
#include "sio_client.h"
int main(int argc, const char * argv[]) {
sio::client h;
h.connect("http://127.0.0.1:3000");
}
The build settings under target are:
Library search path: /usr/local/Cellar/boost/1.58.0/lib
User Header search path: /usr/local/Cellar/boost/1.58.0/include, "$(SRCROOT)/boost/socket.io-client-cpp/lib/websocketpp"and "$(SRCROOT)/boost/socket.io-client-cpp/lib/rapidjson/include"
I have installed Boost using brew install boost --c++11 and imported the libc++.dylib and tried the libc++.6.0.9.dylib as frameworks into the project.
And I am getting the following compiling error.
Note: following tutorial from - https://github.com/socketio/socket.io-client-cpp.
Can anybody PLEASE! Help me, I am dying here :P

Linker error with get_driver_instance() MySQL C++ VS2010

I have searched through all the questions on here trying to find a solution and it just isnt happening.
Error
1>MySQLTest.obj : error LNK2019: unresolved external symbol __imp__get_driver_instance referenced in function _main
What I have tried
I know this is a linker error. So I have modified the include and library directory options in VS2010 to point to the MySQL Server libraries and the Connection++ libraries
Libraries:
C:\Programming\boost_1_55_0\stage\lib
C:\Program Files\MySQL\Connector C++ 1.1.3\lib
C:\Program Files\MySQL\MySQL Server 5.6\lib
Includes:
C:\Programming\boost_1_55_0
C:\Program Files\MySQL\Connector C++ 1.1.3\include
C:\Program Files\MySQL\Connector C++ 1.1.3\include\cppconn
I have read in another thread that the get_driver_instance function is only available when dynamically loading the library. It said to define mysqlcppconn_EXPORTS.
get_driver_instance() is now only available in dynamic library builds
- static builds do not have this symbol. This was done to accommodate loading the
DLL with LoadLibrary or dlopen. If you do not use CMake
for building the source code you will need to define
mysqlcppconn_EXPORTS if you are loading dynamically and want to use
the get_driver_instance() entry point.
I defined this and it still does not work. There is another define (CPPCONN_PUBLIC_FUNC) that people said to use if I wanted to statically link. I tried this too and I still get that error. Preferably I would like to just statically link everything and use a different way of accessing the driver if possible. Is there another way other than get_driver_instance()?
Note: SQLString is working fine. So I am properly linking certain things. I just do not know why this is erring out with this one function.
I have the 64 bit version of the connector installed. Along with mysql Server and workbench.
Here is the code I am trying to run:
#include "stdafx.h"
//#define CPPCONN_PUBLIC_FUNC
/* Standard C++ headers */
#include <iostream>
#include <sstream>
#include <memory>
#include <string>
#include <stdexcept>
#include <Windows.h>
/* MySQL Connector/C++ specific headers */
#include <mysql_driver.h>
#include <mysql_connection.h>
#include <statement.h>
#include <prepared_statement.h>
#include <resultset.h>
#include <metadata.h>
#include <resultset_metadata.h>
#include <exception.h>
#include <warning.h>
using namespace std;
using namespace sql;
int main(int argc, _TCHAR* argv[])
{
sql::mysql::MySQL_Driver *driver;
Connection *con;
/* initiate url, user, password and database variables */
const sql::SQLString url = "tcp://127.0.0.1:3306";
const sql::SQLString user = "root";
const sql::SQLString password = "admin";
const sql::SQLString database = "store";
driver = sql::mysql::get_mysql_driver_instance();
/* create a database connection using the Driver */
con = driver->connect(url, user, password);
system("PAUSE");
return 0;
}
There ended up being a slight error in one of my assumptions. I assumed visual studio defaulted its build based on the system you are running. It was building a 32 bit version of the application(on a 64 bit machine), and I am using 64 bit libraries. If you do this the linker will not look at libraries compiled using a 64 bit target.
The machine type has to be changed to 64 bit in the following area to get my build to work properly:
Project -> "project_name" Properties -> Linker -> Advanced -> Target Machine set to 'x64'
There may also be another attribute that had to be changed that is related (making sure its building for 64 bit) but I cannot remember where it was. I remember it being very obvious to find though.
I solved this problem by downloading a C++ connector from MySQL website and a libmysql.dll 32bit from dllfiles.com
Note: MySQL Installer is 32 bit, but will install both 32 bit and 64 bit binaries depend on the platform of your computer. I'm using Win8 64bit and VS2010 32bit, my target platform is 32bit. But the connector provided with MySQL is 64bit, so it will cause unresolved symbol problem.
So you have to download an independent 32bit connector and use the lib in it. Also, you must download libmySQL.dll 32bit

Linking .lib of MySQL in C/C

So, after whole day of trial and effort, I still couldn't figure answer to this question.
I'm perfectly aware of
this thread and this one
but they don't quite give answer to the question.
So, the problem is when compiling program, let's say, like this one:
#include <iostream>
#include <windows.h>
#include <mysql.h>
using namespace std;
int main()
{
MYSQL *connection, mysql;
MYSQL_RES *result;
MYSQL_ROW row;
mysql_init(&mysql);
}
and I get linker error
undefined reference to `mysql_init#4'
I tried adding to input section libmesql.lib and mysqclient.lib, both simultaneously and separately. Tried copying .lib files to Visual Studio default folder, than attempted compiling it by setting path in Linker->General->Additional Library Directories. Tried #pragma comment as well - still to no avail.
So if anyone out there could explain what am I doing wrong (and possibly a way to solve this problem) it would be much appreciated.
p.s. And please, don't answer with links to MySQL documentation - I got them here.
Link your code with mysqlclient.lib.
Properties->Linker->General->Additional Libraries
Add the path to your library here.

C++ write and read from mysql database

I'm working on a project where we want to write to a mysql database, I've googled around and tried a few implementations but they all fail.
For example I tried this: http://markalexanderbain.suite101.com/using-a-mysql-databases-with-c-a70097
#include "StdAfx.h"
#include <iostream>
#include <my_global.h>
#include <mysql.h>
#pragma comment(lib, "libmysql")
#pragma comment(lib, "mysqlclient")
using namespace std;
MYSQL *connection, mysql;
MYSQL_RES *result;
MYSQL_ROW row;
int query_state;
int main()
{mysql_init(&mysql);
//connection = mysql_real_connect(&mysql,"host","user","password","database",0,0,0);
connection = mysql_real_connect(&mysql,"localhost","bainm","not_telling","cpp_data",0,0,0);
if (connection == NULL)
cout << mysql_error(&mysql) << endl;
return 1;
}
It compiles and generates an exe file, but it closes every time I try to run it, I've added a cin.get at the end so it wouldn't close.
But the program still closes and Visual Studio 2010 gives me the following error message: The program '[32856] mysql test.exe: Native' has exited with code -1073741515 (0xc0000135).
The only thing that is common in all these implementations is the Include files and the #pragma comment(lib, "libmysql") and #pragma comment(lib, "mysqlclient")
I'm using these files which are included with mysql ga 5.5.16 32 bit.
So I'm looking for a way to write to a mysql database, if any one knows how to fix this to make it work or knows another way with a tutorial please let me know.
Error code 0xc0000135 means "The Application Failed To Initialize Properly". This is almost certainly due to a missing DLL (or two); probably the ones relating to MySQL mentioned in your #pragma lib statements.
Either add them to your path or copy them to the same directory as the .exe.
EDIT: Follow the instructions from here, in order to compile with the correct options. The file mysqlclient.dll should be included with the MySQL installation (do a system file find if you cannot find it).