While trying to connect to a mysql database using c++ code I'm having a segmentation fault when I call connect.
This is the code:
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <string.h>
#include "mysql_connection.h"
#include "mysql_driver.h"
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
#include <cppconn/connection.h>
#include <cppconn/prepared_statement.h>
#include "iolib.h"
int main(int argc, char* argv[])
{
cout<<"1\n";
sql::Driver *driver = sql::mysql::get_driver_instance();
cout<<"Driver: "<<driver<<", "<<driver->getName()<<"\n";
sql::Connection *conn = driver->connect("tcp://<ip>:3306", "root", "password");
cout<<"Connected\n";
return 0;
}
The output is:
1
Driver: 0x1755050, MySQL Connector C++ (libmysql)
Segmentation fault (core dumped)
Previously I forgot to add this machine to the allowed IPs, then it gave me an error saying I wasn't authorized instead of segmentation fault. After I allowed the IP I get this error. I can block the IP to get the not allowed IP error again.
This is the makefile:
CC=g++
CFLAGS=-c -g -rdynamic
LDFLAGS=-lpthread -ldl -rdynamic -lmysqlcppconn
EXECUTABLE=myexe
all: main
$(CC) $(LDFLAGS) *.o -o $(EXECUTABLE)
@echo -e "\033[32mFinished!\033[0m"
main:
$(CC) $(CFLAGS) -o iolib.o iolib.cpp
$(CC) $(CFLAGS) -o importer.o importer.cpp
@echo -e "\033[36mObject files compiled!\033[0m"
This machine is running Fedora 19 and the libraries were installed with "yum install mysql mysql-connector-c++-devel mysql++". I may be off a tad bit with the exact commands here but important to say they were all installed with yum.
Searching the web for similar problems, the most likely thing I've come across are people saying that there must be a problem with my libraries so ldd myexe follows.
linux-vdso.so.1 => (0x00007fff72e6b000)
libpthread.so.0 => /lib64/libpthread.so.0 (0x000000370f000000)
libdl.so.2 => /lib64/libdl.so.2 (0x000000370ec00000)
libmysqlcppconn.so.6 => /lib64/libmysqlcppconn.so.6 (0x00007f2c335ee000)
libstdc++.so.6 => /lib64/libstdc++.so.6 (0x0000003716800000)
libm.so.6 => /lib64/libm.so.6 (0x000000370f800000)
libgcc_s.so.1 => /lib64/libgcc_s.so.1 (0x0000003710800000)
libc.so.6 => /lib64/libc.so.6 (0x000000370e800000)
/lib64/ld-linux-x86-64.so.2 (0x000000370e400000)
libmysqlclient.so.18 => /usr/lib64/mysql/libmysqlclient.so.18 (0x00007f2c3301d000)
libz.so.1 => /lib64/libz.so.1 (0x000000370f400000)
libssl.so.10 => /lib64/libssl.so.10 (0x0000003728800000)
libcrypto.so.10 => /lib64/libcrypto.so.10 (0x0000003723400000)
libgssapi_krb5.so.2 => /lib64/libgssapi_krb5.so.2 (0x0000003724c00000)
libkrb5.so.3 => /lib64/libkrb5.so.3 (0x0000003725800000)
libcom_err.so.2 => /lib64/libcom_err.so.2 (0x0000003723c00000)
libk5crypto.so.3 => /lib64/libk5crypto.so.3 (0x0000003725400000)
libkrb5support.so.0 => /lib64/libkrb5support.so.0 (0x0000003725000000)
libkeyutils.so.1 => /lib64/libkeyutils.so.1 (0x0000003724800000)
libresolv.so.2 => /lib64/libresolv.so.2 (0x0000003711000000)
libselinux.so.1 => /lib64/libselinux.so.1 (0x0000003710400000)
libpcre.so.1 => /lib64/libpcre.so.1 (0x0000003710000000)
How can I make this work?
I appreciate any help.
Segmentation fault (core dumped)
so the core is present, and you should use it to find out what causes the issue – Noam Rathaus