C++/mysql 连接器 - 对 get_driver_instance 的未定义引用 - 已经尝试了简单的东西

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15995319/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 19:58:54  来源:igfitidea点击:

C++ / mysql Connector - undefined reference to get_driver_instance - already tried the easy stuff

c++mysqlubuntu-12.10

提问by Ray in NY

Yes this question has been asked before ... I've tried everything mentioned in the previous answers. My setup is really straightforward so this shouldn't be so hard.

是的,之前有人问过这个问题......我已经尝试了之前答案中提到的所有内容。我的设置非常简单,所以这不应该那么难。

I just want to program against mysql using C++. My source code is taken verbatem from the 'hello world' type example here:

我只想使用 C++ 对 mysql 进行编程。我的源代码是从这里的“hello world”类型示例中逐字提取的:

http://dev.mysql.com/doc/refman/5.1/en/connector-cpp-examples-complete-example-1.html

http://dev.mysql.com/doc/refman/5.1/en/connector-cpp-examples-complete-example-1.html

I am on Ubuntu 12.10. I am trying:

我在 Ubuntu 12.10 上。我在尝试:

g++ -Wall -o firsttry_prog -I/usr/include/mysqlcppconn -I/usr/local/boost_1_53_0  -L/usr/lib/x86_64-linux-gnu -l:libmysqlclient_r.so.18 -L/usr/lib/mysqlcppconn -lmysqlcppconn  firsttry.cpp

It compiles (if I use -c option) but won't build, giving me the infamous:

它编译(如果我使用 -c 选项)但不会构建,给我臭名昭著的:

/tmp/ccn768hj.o: In function `main':
firsttry.cpp:(.text+0x3a): undefined reference to `get_driver_instance'

A few details:

一些细节:

  • 'firsttry.cpp'is just what I named the source code file, again taken verbatem from the official example
  • As you can see I AM linking in the mysqlclientlibrary and the mysqlcppconnlibrary. Many times when this question has been asked previously, the answer was to link those.
  • Some other historical answers suggest the sample source code is wrong and that the function in question needs to be in the sql::mysql namespace etc. I am pretty sure the source code is fine. Again, it compiles, and changing the namespaces in the source code just seems to make it worse.
  • 'firsttry.cpp'正是我命名的源代码文件,再次逐字取自官方示例
  • 如您所见,我正在链接mysqlclient库和mysqlcppconn库。很多时候以前曾问过这个问题,答案是将它们联系起来。
  • 其他一些历史答案表明示例源代码是错误的,有问题的函数需要在 sql::mysql 命名空间等中。我很确定源代码是好的。同样,它会编译,更改源代码中的命名空间似乎只会让它变得更糟。

Thank you in advance for any help you can provide.

预先感谢您提供的任何帮助。

回答by d3l

So I have now had this problem for a week now and I became very frustrated with it as well. I just now was able to finally build a program that does nothing except login to mysql and I literally squealed with joy. Here is what I have and I hope it helps.

所以我现在已经遇到这个问题一个星期了,我也对此感到非常沮丧。我刚刚终于能够构建一个除了登录 mysql 之外什么都不做的程序,我真的高兴得尖叫起来。这是我所拥有的,我希望它有所帮助。

I first compiled the c++ connector library from source but after a while I thought maybe I did something wrong so I then just used apt to get it with:

我首先从源代码编译了 c++ 连接器库,但过了一段时间我想也许我做错了什么,所以我只是使用 apt 来获取它:

sudo apt-get install  libmysqlcppconn-dev

And here is my simple tester source file "tester.cpp"

这是我的简单测试器源文件“tester.cpp”

#include <stdlib.h>
#include <iostream>
#include <mysql_connection.h>
#include <driver.h>
#include <exception.h>
#include <resultset.h>
#include <statement.h>

using namespace sql;
int main(void){
  sql::Driver *driver;
  sql::Connection *con;

  driver = get_driver_instance();
  con = driver->connect("tcp://127.0.0.1:3306","root","YOURPASSWORD");

  return 0;
}

And finally g++ compile command:

最后是 g++ 编译命令:

sudo g++ -Wall -I/usr/include/cppconn -o testapp tester.cpp -L/usr/lib -lmysqlcppconn

This worked for me and I hope it helps you solve your problem!

这对我有用,我希望它可以帮助您解决问题!

回答by Tarun

For me simply swapping the order of the last two arguments fixed this problem. I don't know why but the linker is able to find the function get_driver_instanceif I specify the -lmysqlcppconnoption at the end after the source file.

对我来说,只需交换最后两个参数的顺序即可解决此问题。我不知道为什么,但是get_driver_instance如果我-lmysqlcppconn在源文件的末尾指定选项,链接器能够找到该函数。

g++ -Wall -o firsttry_prog -I/usr/include/mysqlcppconn -L/usr/lib/mysqlcppconn firsttry.cpp -lmysqlcppconn

Also note that I took out the following options as I think they are redundant

另请注意,我删除了以下选项,因为我认为它们是多余的

-I/usr/local/boost_1_53_0  -L/usr/lib/x86_64-linux-gnu -l:libmysqlclient_r.so.18

回答by nakajuice

In case you are as forgetful as me and didn't link the library in CMakeLists.txt:

如果你和我一样健忘并且没有在 CMakeLists.txt 中链接库:

target_link_libraries(<target> mysqlcppconn)

回答by huangxiaowei

If all the paths are included throw param -I. You would see whether there is a problem if you compile like this:

如果包含所有路径,则抛出参数 -I。如果你这样编译,你会看到是否有问题:

g++ -g  -o0  -I/usr/local/include -I/usr/local/boost/include -c main.cpp -o main.o
g++ -g  -o0 -L/usr/local/lib -L/usr/local/mysql/lib -lmysqlcppconn  main.o  -o test  

the problem will appear:

问题会出现:

main.o: In function `main':
/home/huangxw/workspace/public/soal/test/main.cpp:165: undefined reference to `get_driver_instance'
collect2: ld returned 1 exit status

Now you must adjust the order of -lmysqlcppconnand main.o:

现在您必须调整-lmysqlcppconnand的顺序main.o

g++ -g  -o0  -I/usr/local/include -I/usr/local/boost/include -c main.cpp -o main.o
g++ -g  -o0 -L/usr/local/lib -L/usr/local/mysql/lib main.o  -o test  -lmysqlcppconn

That is all!! The reason is simple. You can find out using the web or ask me to elaborate.

就这些!!原因很简单。您可以使用网络查找或要求我详细说明。