# c++ mysql stored procedure  compile error



## xnl96 (Apr 5, 2012)

*I* have this code: 

```
#include<iostream>
using namespace std;

#include<mysql.h>
#include <driver.h>
#include <exception.h>
#include <resultset.h>
#include <statement.h>
#include <prepared_statement.h>

#define EXAMPLE_HOST "localhost"
#define EXAMPLE_USER "test"
#define EXAMPLE_PASS ""
#define EXAMPLE_DB "test"

int main(int argc, const char **argv)
{
  string url(argc >= 2 ? argv[1] : EXAMPLE_HOST);
  const string user(argc >= 3 ? argv[2] : EXAMPLE_USER);
  const string pass(argc >= 4 ? argv[3] : EXAMPLE_PASS);
  const string database(argc >= 5 ? argv[4] : EXAMPLE_DB);
  try {
    
    sql::Driver * driver = get_driver_instance();
    std::auto_ptr< sql::Connection > con(driver->connect(url, user, pass));
    con->setSchema(database);
    std::auto_ptr< sql::Statement > stmt(con->createStatement());
    
    stmt->execute("DROP TABLE IF EXISTS test");
    stmt->execute("CREATE TABLE test(id INT)");
    stmt->execute("DROP PROCEDURE IF EXISTS p");
    stmt->execute("CREATE PROCEDURE p() BEGIN INSERT INTO test(id) VALUES (123); END;");
    
    stmt->execute("CALL p()");
    
    std::auto_ptr< sql::ResultSet > res(stmt->executeQuery("SELECT id FROM test"));
    
    cout << "\t... running 'SELECT id FROM test'" << endl;
    while (res->next()) {
      cout << "\t... id: " << res->getInt("id") << endl;
    } 
  } catch (sql::SQLException &e) {
    return EXIT_FAILURE;
  }
  
  return EXIT_SUCCESS;
}
```

and this Makefile:

```
CC=g++
CFLAGS=-g -c -Wall -o standalone  -Wl,-Bdynamic -lmysqlcppconn
MYSQLINCLUDE = -I/usr/local/include/mysql
MYSQLINCLUDESP = -I/usr/local/include/cppconn/
LDFLAGS = -L/usr/local/lib/mysql  -lmysqlclient 
LDFLAGSSP = -L/usr/local/lib
SOURCES=./main.cpp  
OBJECTS=$(SOURCES:.cpp=.o)
EXECUTABLE=licenta

all: $(SOURCES) $(EXECUTABLE)
	
$(EXECUTABLE): $(OBJECTS)
	$(CC) $(LDFLAGS) $(LDFLAGSSP) $(OBJECTS) -o $@

.cpp.o:
	$(CC) $(MYSQLINCLUDE) $(MYSQLINCLUDESP) $(CFLAGS) $< -o $@
clean:
	rm -rf $(OBJECTS) $(EXECUTABLE) *.core
```
and *I* get this error

```
make
g++ -I/usr/local/include/mysql -I/usr/local/include/cppconn/ -g -c -Wall -o standalone  -Wl,-Bdynamic
 -lmysqlcppconn ./main.cpp -o ./main.o
g++: -Bdynamic: linker input file unused because linking not done
g++: -lmysqlcppconn: linker input file unused because linking not done
g++ -L/usr/local/lib/mysql  -lmysqlclient -L/usr/local/lib ./main.o -o licenta
./main.o(.text+0x3bf): In function `main':
./main.cpp:24: undefined reference to `get_driver_instance'
*** Error code 1
```

what i do wrong ? i need other lib paht?

What am I doing wrong? Do I need a different lib path?


----------



## dh (Apr 6, 2012)

Include mysql_driver.h header.
You didn't even bother to google the error first, did you?


----------



## xnl96 (Apr 7, 2012)

This is not helping me because I use get_driver_instance() from driver.h, anyway if I include  [fiile]mysql_driver.h[/file] I get this error

```
g++ -L/usr/local/lib/mysql  -lmysqlclient -L/usr/local/lib ./main.o -o licenta
./main.o(.text+0x3bf): In function `main':
./main.cpp:27: undefined reference to `get_driver_instance'
*** Error code 1
```

The problem was in Makefile, this is correct:

```
CC=g++
CFLAGS=-g -c -Wall -o standalone -Wl,-Bdynamic 
MYSQLINCLUDE = -I/usr/local/include/mysql
MYSQLINCLUDESP = -I/usr/local/include/
LDFLAGS = -L/usr/local/lib/mysql  -lmysqlclient [B]-lmysqlcppconn[/B]
LDFLAGSSP = -L/usr/local/lib
SOURCES=./main.cpp  
OBJECTS=$(SOURCES:.cpp=.o)
EXECUTABLE=licenta

all: $(SOURCES) $(EXECUTABLE)
	
$(EXECUTABLE): $(OBJECTS)
	$(CC) $(LDFLAGS) $(LDFLAGSSP) $(OBJECTS) -o $@

.cpp.o:
	$(CC) $(MYSQLINCLUDE) $(MYSQLINCLUDESP) $(CFLAGS) $< -o $@
clean:
	rm -rf $(OBJECTS) $(EXECUTABLE) *.core
```


----------

