-
MySQL Connector C++ "linker" error.
Hello guys. Been working this for some time now, but I'm getting more and more confused.
When I use the MingW compiler on Windows, to compile my program:
Code:
#include <iostream>
#include <mysql_connection.h>
#include <mysql_driver.h>
using namespace std;
int main(){
sql::mysql::MySQL_Driver *driver;
sql::Connection *con;
driver = sql::mysql::get_mysql_driver_instance();
con = driver->connect("tcp://127.0.0.1:3306", "root", " ");
cout << "connection OK";
cin.get();
delete con;
}
Using the following command:
Code:
g++ hw.cpp -o hw.exe
I get the following error:
Code:
C:\Users\kk\AppData\Local\Temp\ccRrhIs3.o:hw.cpp:(.text+0x7): undefined referen
ce to `_imp___ZN3sql5mysql19get_driver_instanceEv'
collect2: ld returned 1 exit status
I did a few attempts on how to link a libary, but it didn't work.
- realchamp.
-
Re: MySQL Connector C++ "linker" error.
If I remember correctly you use -l to link additional libraries (the letter l as in link that is, not I)
-
Re: MySQL Connector C++ "linker" error.
Quote:
Originally Posted by
S_M_A
If I remember correctly you use -l to link additional libraries (the letter l as in link that is, not I)
I did a link to mysqlcppconn.lib which were located insite the Connector C++ package, but nothing really happened. :(
-l mysqlcppconn.lib
-
Re: MySQL Connector C++ "linker" error.
it should be -lmysqlcppconn without the space. It might not be able to find the lib to link, you could try specifying the libs location by adding -L/c/path/to/lib.
-
Re: MySQL Connector C++ "linker" error.
Quote:
Originally Posted by
Cheezewizz
it should be -lmysqlcppconn without the space. It might not be able to find the lib to link, you could try specifying the libs location by adding -L/c/path/to/lib.
Thanks for your reply.
Unfortunately it still dont work.
This is were it wants the mysqlcppconn.lib in:
Code:
c:/mingw/bin/../lib/gcc/mingw32/4.5.0/../../../../mingw32/bin/
If I open my explorer and enter that path, it gives me
Code:
C:\MinGW\mingw32\bin
So I dropped the mysqlcppconn.lib in there and then compiled once agian and..
Code:
C:\MinGW\bin>g++ hw.cpp -lmysqlcppconn -o hw.exe
Info: løser std::cout ved at lænke til __imp___ZSt4cout (automatisk import)
Info: løser std::cin ved at lænke til __imp___ZSt3cin (automatisk import)
c:/mingw/bin/../lib/gcc/mingw32/4.5.0/../../../../mingw32/bin/ld.exe: warning: a
uto-importing has been activated without --enable-auto-import specified on the c
ommand line.
This should work unless it involves constant data structures referencing symbols
from auto-imported DLLs.
C:\Users\kk\AppData\Local\Temp\ccldRR4I.o:hw.cpp:(.text+0x7): undefined referen
ce to `_imp___ZN3sql5mysql19get_driver_instanceEv'
collect2: ld returned 1 exit status
And I've even put the mysqlcppconn.lib into multiple folders, to insure that it's in place!
-
Re: MySQL Connector C++ "linker" error.
where was the lib for mysqlconnector initially installed to? you don't have to move it you can just specify it's location on the command line. -l is for the addtional library names and then -L is for additional library locations...
-
Re: MySQL Connector C++ "linker" error.
The -l flag is a bit strange. If you say "-lmylibrary", then you need to have libmylibrary.lib available. Note, not only is the extension ".lib" not included in the -l statement, but also the prefix "lib". At least, that's how gcc works in Linux (with .a or .so rather than .lib, though); I'm assuming it's the same for MinGW.
If the name of the library you are trying to link does not start with "lib", then you cannot use the -l flag. Instead, simply put the library name on the command line after the names of your source files.
-
Re: MySQL Connector C++ "linker" error.
that makes sense but then does it not normally complain about not being able to find a library named blah if you're trying to link it against the wrong name? Not only that but i've been using Mingw gcc port for a while and have used it like -ldxd9 -lwininet etc. and they've always been OK. if you google get_driver_instance, the first result points to another forum with others having similar problems and the solution, i think, was the location of the lib file...
-
Re: MySQL Connector C++ "linker" error.
Quote:
Originally Posted by
Cheezewizz
where was the lib for mysqlconnector initially installed to? you don't have to move it you can just specify it's location on the command line. -l is for the addtional library names and then -L is for additional library locations...
I think it's installed here: C:\Program Files (x86)\MySQL\Connector ODBC 5.1
Or here: C:\Program Files\MySQL\MySQL Connector C++ 1.0.5\lib\opt
The opt folder contains the mysqlcppconn.lib
Quote:
Originally Posted by
Lindley
The -l flag is a bit strange. If you say "-lmylibrary", then you need to have libmylibrary.lib available. Note, not only is the extension ".lib" not included in the -l statement, but also the prefix "lib". At least, that's how gcc works in Linux (with .a or .so rather than .lib, though); I'm assuming it's the same for MinGW.
If the name of the library you are trying to link does not start with "lib", then you cannot use the -l flag. Instead, simply put the library name on the command line after the names of your source files.
Can I just put that prefix on the file name? Also what do you mean with put the library on the command line?
Like:
Code:
g++ hw.cpp -o hw.exe mysqlcppconn.lib
Quote:
Originally Posted by
Cheezewizz
that makes sense but then does it not normally conplain about not being able to find a library named blah if you're trying to link it against the wrong name? Not only that but i've been using Mingw gcc port for a while and have used it like -ldxd9 -lwininet etc. and they've always been OK. if you google get_driver_instance, the first result points to another forum with others having similar problems and the solution, i think, was the location of the lib file...
I will try to google that, thanks! Was it this that you refered to?:: http://forums.mysql.com/read.php?167...601#msg-299601
-
Re: MySQL Connector C++ "linker" error.
that's the one yep. I've just downloaded the lib and tried your code and end up with exactly the same issue. So I was wrong and it's obviously not a problem with not being able to locate the lib. It does however say somewhere on their site that "Binary incompatibilities can also occur if you are using a different C Run-Time Libraries (CRT) than we used for building" and i believe the prebuilt binaries were done with visual studio so maybe that's messing it up? could try grabbing the source and building your own binaries with mingw...
edit: er yeah i just tried one of the older binaries that they compiled with VS2005, and using VS2005 your test code compiles and links fine with it, so unfortunately it must be an issue with Mingw...
-
Re: MySQL Connector C++ "linker" error.
Quote:
Originally Posted by
Cheezewizz
that's the one yep. I've just downloaded the lib and tried your code and end up with exactly the same issue. So I was wrong and it's obviously not a problem with not being able to locate the lib. It does however say somewhere on their site that "Binary incompatibilities can also occur if you are using a different C Run-Time Libraries (CRT) than we used for building" and i believe the prebuilt binaries were done with visual studio so maybe that's messing it up? could try grabbing the source and building your own binaries with mingw...
edit: er yeah i just tried one of the older binaries that they compiled with VS2005, and using VS2005 your test code compiles and links fine with it, so unfortunately it must be an issue with Mingw...
Thanks for your reply. That just cleared up alot for me.
Should it work if I compiled it on Linux?
-
Re: MySQL Connector C++ "linker" error.
i don't see why not as long as you grab the right source for whatever distro you're using
-
Re: MySQL Connector C++ "linker" error.
Quote:
Originally Posted by
Cheezewizz
i don't see why not as long as you grab the right source for whatever distro you're using
I will go try compiling it on Linux. Thanks :)
-
Re: MySQL Connector C++ "linker" error.
Linux attempt on Debian 5.0.
Code:
g++ hw.cpp -Llibmysqlcppconn.so.1.0.5 -o hw
/tmp/ccOXABZ5.o: In function `main':
hw.cpp:(.text+0x5e): undefined reference to `sql::mysql::get_mysql_driver_instance()'
collect2: ld returned 1 exit status
root@vs170144:~#
I'm seriously on the egde to give up. :(
-
Re: MySQL Connector C++ "linker" error.
Not having much luck with this are ya? thought about just using the C API directly instead? :P well the first thing to try (again) would be to make sure that it's definitely checking the right path for the shared library. check out this guy's post in another forum http://forums.mysql.com/read.php?167...772#msg-282772 and see if that works out...
Also will it make any difference that Debian isn't on this list? I used to occasionally struggle with getting stuff to work on there til I gave up and moved to Ubuntu...
Code:
Supported Platforms
Linux
* FC4 (x86)
* RHEL 3 (ia64, x86, x86_64)!
* RHEL 4 (ia64, x86, x86_64)o
* RHEL 5 (ia64*, x86, x86_64)o
* SLES 9 (ia64, x86, x86_64)o
* SLES 10 (ia64, x86_64)o
* SuSE 11.0, (x86_64)
* Ubuntu 8.04 (x86)
* Ubuntu 8.10 (x86_64)
-
Re: MySQL Connector C++ "linker" error.
Correct I noticed Debian isn't on that list, but in most cases it will run either way. Ubuntu and Debian can run together, from my experience. :)
I will try that, thanks! :)
-
Re: MySQL Connector C++ "linker" error.
Okay some updates.
I can now compile it and it compiles, but:
Code:
g++ hw.cpp -o ./hw -lmysqlcppconn
/usr/bin/ld: warning: libstdc++.so.5, needed by /usr/local/lib/libmysqlcppconn.so, may conflict with libstdc++.so.6
But then I want to execute my hw file.
It compiles now, but surely it does not work :(
How am I supposed to get around that? :S
-
Re: MySQL Connector C++ "linker" error.
Howdy RealChamp. At this link [link]http://blog.ulf-wendel.de/?p=215[/link] about two thirds down the page there's a bit that says
Code:
As said, you need to link both "mysqlcppconn-static.lib" and "libmysql.lib". Add both to the list of additional dependencies.
. On this post they are using Visual Studio. But still if both of those libraries are needed in that for that compiler then maybe you'll also need it. I wouldn't be surprised if i was way off. I'm just reading as much as I can on C++ and everything actually on this website; and figured it was worth taking a look into.
-
Re: MySQL Connector C++ "linker" error.
Quote:
Originally Posted by
viperbyte
Howdy RealChamp. At this link [link]http://blog.ulf-wendel.de/?p=215[/link] about two thirds down the page there's a bit that says
Code:
As said, you need to link both "mysqlcppconn-static.lib" and "libmysql.lib". Add both to the list of additional dependencies.
. On this post they are using Visual Studio. But still if both of those libraries are needed in that for that compiler then maybe you'll also need it. I wouldn't be surprised if i was way off. I'm just reading as much as I can on C++ and everything actually on this website; and figured it was worth taking a look into.
Thanks alot! I linked the mysqlcppconn-static to the project too(on Debian, still).
Code:
# g++ hw.cpp -o hw -lmysqlcppconn-static -lmysqlcppconn
/usr/bin/ld: warning: libstdc++.so.5, needed by /usr/local/lib/libmysqlcppconn.so, may conflict with libstdc++.so.6
# ./hw
terminate called after throwing an instance of 'sql::InvalidArgumentException'
what(): You should not call directly the constructor
Aborted
Tell me if I'm compiling it wrong, because I do not know. Also I've changed abit in the code, but this run error annoyes me(the terminate called after throwing an instance of 'sql::InvalidARgumentException')
New code:
Code:
#include <iostream>
#include <stdlib.h>
#include <mysql_connection.h>
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
#include <cppconn/prepared_statement.h>
int main(){
sql::Driver *driver;
sql::Connection *con;
sql::Statement *stmt;
sql::ResultSet *res;
sql::PreparedStatement *pstmt;
try{
driver = get_driver_instance();
con = driver->connect("tcp://127.0.0.1:3306", "root", "root");
con->setSchema("test");
stmt = con->createStatement();
stmt->execute("insert into example values(4,'four'),(5, 'five')");
delete stmt;
pstmt = con->prepareStatement("select * from example");
res = pstmt->executeQuery();
while (res->next())
std::cout<<res->getInt("id")<<" "<<res->getString("data")<<std::endl;
delete res;
delete pstmt;
pstmt = con->prepareStatement("delete from example where id=?");
pstmt->setInt(1,4);
pstmt->executeUpdate();
pstmt->setInt(1,5);
pstmt->executeUpdate();
delete pstmt;
delete con;
}catch(sql::SQLException &e){
std::cout<<e.what();
}
}
Been on Google to find a result for my problem, but it's not really the same I think.
It's something about memory, but I doubt that's the problem :)
-
Re: MySQL Connector C++ "linker" error.
Quote:
Originally Posted by
realchamp
Tell me if I'm compiling it wrong, because I do not know. Also I've changed abit in the code, but this run error annoyes me(the terminate called after throwing an instance of 'sql::InvalidARgumentException')
Before you do anything else, are you using a debugger?
That exception is being thrown because one of those calls you're making caused the error, but you failed to mention at all which one of those lines is throwing the exception. This gives the impression that you're not using a debugger. You seem to be writing code and hoping something works, which is not the way a programmer is supposed to approach writing something such as you're doing.
You don't need Google searches -- what you need to do is identify the line that is causing the problem, and from there either debug into the library, or read the library documentation carefully to make sure you are doing things correctly.
Secondly, I don't know about the particular API you're using, but it is bad design to blindly call "delete" like that, unless it is documented that you must do this to release the memory. Usually the API takes care of the cleanup automatically, or the API gives you a function that you call to clean up the resources yourself.
If you supposed to call delete, then RAII certainly isn't high on this particular C++ wrapper's list, which is a shame.
Regards,
Paul McKenzie
-
Re: MySQL Connector C++ "linker" error.
Thanks for reply. I was not given a specific line where the error is from. But I will go check the documentation for a debug compiler, thanks!
Okay I've installed the g++ Debug. (apt-get install gdb) and compiled my program with an adionnally paramenter(-g).
Code:
This GDB was configured as "x86_64-linux-gnu"...
(gdb) run
Starting program: hw
[Thread debugging using libthread_db enabled]
Could not open /proc/25899/status
Could not open /proc/25899/status seems to be the problem and that is ... something about stats..
I've set a breakpoint at the initailization of the program(about line 1) and it throws me that error or incase I skip to the next error it's just something about a function..(Cannot find bounds of current function).
This line is the problem.
Code:
driver = get_driver_instance();
-
Re: MySQL Connector C++ "linker" error.
Just for fun I would try also linking libmysql.lib if I knew how.
-
Re: MySQL Connector C++ "linker" error.
Quote:
Originally Posted by
viperbyte
Just for fun I would try also linking libmysql.lib if I knew how.
There are no such library, atleast not for Linux. :)
-
Re: MySQL Connector C++ "linker" error.
Quote:
Originally Posted by
realchamp
I've set a breakpoint at the initailization of the program(about line 1) and it throws me that error or incase I skip to the next error it's just something about a function..(Cannot find bounds of current function).
This line is the problem.
Code:
driver = get_driver_instance();
I suggest resorting to the old technique of putting printf()'s or couts to really see how far your program goes before it throws the exception:
Code:
printf("I am at point 1\n");
some SQL code...
printf("I am at point 2\n");
some SQL code...
etc...
So if the debugger is giving you a problem, at least you have that technique as a poor-mans debugger.
Secondly, if that line is the one that is actually throwing an exception, then I would conclude there is something wrong with the build, as that is the initialization function and nothing else needs to be done to call it.
Many times, library code just bombing out at the very function call indicates a build problem of the library (wrong build, bad build, etc.), or your own code is being built with the wrong switches used (more likely the latter if that library was already built for you). If you build your app with the wrong switches, improper constants, structs, etc. would be used by your app, differing what is in the library code. Yes, you get a build, but the build is a big mess where the same struct, class, etc. is defined differently in two places, causing random crashes.
Check that you built your code with the correct switches.
Regards,
Paul McKenzie
-
Re: MySQL Connector C++ "linker" error.
I hope you're not having that big mess that Paul said might be happening. I would go nuts if that was happening to me. I found this article usefull for me by the way in case you would be interested in it: http://dev.mysql.com/tech-resources/...ector-cpp.html