且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在centos 6中的mysql连接器cpp未定义引用

更新时间:2023-09-14 13:17:52

您当前的构建命令: g ++ demo.cpp -o demo 不包含链接器 ld 的信息代码>应该链接到哪个库.因此,您会收到一个链接器错误:

Your current build command: g++ demo.cpp -o demo doesn't contain informations for the linker ld which libraries should be linked against. Because of that you get a linker error:

demo.cpp :(.text + 0x3a):对'get_driver_instance'的未定义引用
collect2:ld返回1个退出状态

demo.cpp:(.text+0x3a): undefined reference to 'get_driver_instance'
collect2: ld returned 1 exit status

此文档中写了需要哪些库.

您可以链接静态或动态链接.
静态链接意味着您的可执行文件将在未安装所需库的计算机上运行,​​因为这些库位于可执行文件内部.这也使可执行文件更大.对于MySQL Connector/C ++,库为: libmysqlcppconn-static.a libmysqlclient.a
动态链接意味着您的可执行文件需要在其应运行的计算机上查找库.所需的库是: libmysqlcppconn.so .

You can either link static or dynamically.
Static linking means that your executable will run on machines that doesn't have the needed libraries installed as the libraries are inside the executable. This also makes the executable bigger in size. In the case of the MySQL Connector/C++ the libraries are: libmysqlcppconn-static.a and libmysqlclient.a
Dynamic linking means that your executable will need to find the libraries on the machine where it should run. The needed library is: libmysqlcppconn.so.

具有动态链接(使用 libmysqlcppconn.so )的构建命令应如下所示:

Your build command with dynamic linking (using libmysqlcppconn.so) should look like:

g++ demo.cpp -o demo -lmysqlcppconn

还要注意 -l 和 -L 之间的区别,如此处所述在官方gcc链接器文档中:

Further note the difference between -l and -L as mentioned here on SO or here in the official gcc linker documentation:

-L 是包含库的目录的路径.库的搜索路径.

-L is the path to the directories containing the libraries. A search path for libraries.

-l 是您要链接到的库的名称.

-l is the name of the library you want to link to.

这里不需要路径( -L ),因为库应该位于/usr/local/lib 下,这是默认安装,并且已经在搜索中链接器的路径.

You dont need a path (-L) here as the libraries should lie under /usr/local/lib which is the default installation and is already in the search path of the linker.