MySQL : Undefined Reference Errors

Aug 27, 2009 at 9:11pm
Hmm... Not sure what's going on with this. I'll give all the information I can think of, just tell me if I'm missing something.

Code (BoGUS.cpp) :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream> // input/output streams.
#include <mysql.h> // the mysql header
using namespace std; // omg, std namespace
// jeez, shortest header i've ever had.

MYSQL *connection, mysql; // variables
MYSQL_RES *result; // more variables
MYSQL_ROW row; // even more
int query_state; // and even more

class BBQSQL { // zomfg its a class!
	public: // public
		void connect(); // here's where we define the connect function!
		void disconnect(); // here's where we define the disconnect function!
	private: // private
		int disconnected; // here's a useless variable that i put because i was unsure about classes requiring private
};


void BBQSQL::connect() { // the mysql connect function
	mysql_init(&mysql); // dunno what this does. it initiates mysql i believe.
	//connection = mysql_real_connect(&mysql,"host","user","password","database",0,0,0);
	connection = mysql_real_connect(&mysql,/*Host*/,/*Username*/,/*Password*/,"bogusnews",0,0,0);
	if (connection == NULL) { // if the connection fails
		cout << mysql_error(&mysql) << endl; // give us an error
	}
}

void BBQSQL::disconnect() { // the mysql disconnect function
	mysql_free_result(result); // clearing results, i think
	mysql_close(connection); // dropping the connection
	disconnected = 1; // working with that useless variable
}

int main() { // The main function
	BBQSQL BoGUS; // defining BoGUS as a mysql class :)
	BoGUS.connect(); // connecting...!
	query_state = mysql_query(connection, "SELECT MAX(ID) FROM bogusnews"); // omg im querying for the highest ID.
	if (query_state !=0) { // if the query state has an error...
		cout << mysql_error(connection) << endl; // show us what happened.
	} // why am i commenting this? its just the end of an if statement
	result = mysql_store_result(connection); // results!
	while ( ( row = mysql_fetch_row(result)) != NULL ) { // OMG! lets see if the results are working
		cout << row[1] << endl; // this is where we show the results.
	} // why am i commenting on this? its just the end of a while statement
	BoGUS.disconnect(); // disconnecting!!!!!1
} // the end of the main function :O 


Command (Terminal) :
g++ BoGUS.cpp -I/usr/include/mysql -DBIG_JOINS=1 -fno-strict-aliasing -DUNIV_LINUXD


Errors (G++) :
/tmp/ccZBgIbR.o: In function `BBQSQL::disconnect()':
BoGUS.cpp:(.text+0x6b): undefined reference to `mysql_free_result'
BoGUS.cpp:(.text+0x78): undefined reference to `mysql_close'
/tmp/ccZBgIbR.o: In function `BBQSQL::connect()':
BoGUS.cpp:(.text+0x96): undefined reference to `mysql_init'
BoGUS.cpp:(.text+0xda): undefined reference to `mysql_real_connect'
BoGUS.cpp:(.text+0xf4): undefined reference to `mysql_error'
/tmp/ccZBgIbR.o: In function `main':
BoGUS.cpp:(.text+0x147): undefined reference to `mysql_query'
BoGUS.cpp:(.text+0x162): undefined reference to `mysql_error'
BoGUS.cpp:(.text+0x18f): undefined reference to `mysql_store_result'
BoGUS.cpp:(.text+0x1cd): undefined reference to `mysql_fetch_row'
collect2: ld returned 1 exit status


Site From Which The MySQL Codes Came :

http://c-programming.suite101.com/article.cfm/using_a_mysql_databases_with_c
Aug 27, 2009 at 9:19pm
You need to link with the mysql library (look further down that page for the Compiling and Running the C++ Code section).
Last edited on Aug 27, 2009 at 9:21pm
Aug 27, 2009 at 9:23pm
{EDITED - NOT SOLVED}
ah, thanks. At first i was working with some other stuff in it, and i ended up having to go some other place to find the command for it, someone said to do :
mysql_config --cflags

on the Ubuntu forums, guess that wasn't right. ill try that now.

{EDIT} It compiled but when i execute it, it says my host is invalid. my host is a website link (provided by my web provider) what's going on there?
Unknown MySQL server host '/*Server Host*/' (1)
Segmentation fault

Could this have to do with my web-host's hatred of 3rd party MySQL software? Navicat and what not don't work. But PHP connections work. I assumed this would work more like a PHP connection than a TP MySQL program o.o
Last edited on Aug 27, 2009 at 9:50pm
Aug 27, 2009 at 10:08pm
Well the host name has to be something that resolves to an ip address.
You can use a NULL pointer or "localhost" to mean a connection to the local machine.

otherwise you can put a dotted decimal ip address like "92.23.101.234" or a URL like
"www.cplusplus.com" - but what ever you use it must be able to be resolved to a IP address.

What did your ISP give you (and I would understand your reluctance if you don't want to give that information away).
Aug 27, 2009 at 10:11pm
oh, its not hosted on my computer or in my network, its non-ISP related. its hosted on a website elsewhere. I used a website link, but it didn't work for some odd reason. Im still thinking it has something to do with the my webhost's hatred of 3rd party software. They require you to use phpMyAdmin which they provide on the site. But again, PHP is still working with it. Is it possible that they only let database connection occur from the inside? if that's the case i might need to host the db on one of my computers.

{EDIT} looks like im gonna go talk to their tech support. yet again.
Last edited on Aug 27, 2009 at 10:12pm
Aug 27, 2009 at 10:27pm
Yup, it was my web-host. That's it.
Topic archived. No new replies allowed.