centOS7下使用cmake编译mariadbpp出错

  • 2020 年 2 月 13 日
  • 笔记

mariadbpp是C++的mariadb库,最近在CentOS7下编译mariadbpp总出错,错误信息如下:

CMake Error at CMakeLists.txt:17 (find_package):   By not providing "FindMariaDBClient.cmake" in CMAKE_MODULE_PATH this   project has asked CMake to find a package configuration file provided by   "MariaDBClient", but CMake did not find one.     Could not find a package configuration file provided by "MariaDBClient"   with any of the following names:       MariaDBClientConfig.cmake     mariadbclient-config.cmake     Add the installation prefix of "MariaDBClient" to CMAKE_PREFIX_PATH or set   "MariaDBClient_DIR" to a directory containing one of the above files.  If   "MariaDBClient" provides a separate development package or SDK, be sure it has been

我在CentOS7系统下已经使用yum安装了mariadb数据库,包括mariadb的C语言库mariadb-connector-c 安装mariadb-connector-c很简单,在mariadb-connector-c所在目录直接运行如下命令即可:

[root@VM_0_9_centos mariadb-connector-c]#  [root@VM_0_9_centos mariadb-connector-c]# mkdir build  [root@VM_0_9_centos mariadb-connector-c]# cd build/  [root@VM_0_9_centos build]# ls  [root@VM_0_9_centos build]# cmake ..  -- The C compiler identification is GNU 4.8.5  -- Check for working C compiler: /usr/bin/cc  -- Check for working C compiler: /usr/bin/cc -- works  ....................  [root@VM_0_9_centos build]# make  [root@VM_0_9_centos build]# make  install

刚开始的时候我是直接从github上面下载的mariadbpp的zip压缩包,然后和上面相同的方式使用cmake编译总是报错。 最后使用google在stackoverflow上面找到一篇俄文的问题在C ++中连接MariaDB 在github上面https://github.com/viaduck/mariadbpp 我直接在CentOS7中使用git克隆源代码,然后按照如下方法编译源代码

Initialize Git submodules: git submodule update --init  Install mariadbclient or mysqlclient libraries.  Link against the mariadbclientpp CMake target in your project.
git clone https://github.com/viaduck/mariadbpp.git  cd mariadbpp  git submodule update --init  mkdir build  cd build  cmake ..  make  make install

默认mariadbpp编译出来的是静态库,如果需要动态库需要稍微修改下CMakeLists.txt文件的

# set up target   add_library(mariadbclientpp ${mariadbclientpp_SOURCES} ${mariadbclientpp_PUBLIC_HEADERS} ${mariadbclientpp_PRIVATE    _HEADERS})

修改成

# set up target  add_library(mariadbclientpp SHARED ${mariadbclientpp_SOURCES} ${mariadbclientpp_PUBLIC_HEADERS} ${mariadbclientpp_PRIVATE    _HEADERS})

即加了一个SHARED 默认在/usr/local/lib/目录下生成的是libmariadbclientpp.a静态库文件,修改后在/usr/local/lib/目录下生成的是libmariadbclientpp.so动态库文件。 这样就可以使用mariadbpp库,编写C++代码操作mysql或者mariadb数据库了。

参考资料: 1、mariadbpp 2、在C ++中连接MariaDB 3、《CMake实践》笔记三:构建静态库(.a) 与 动态库(.so) 及 如何使用外部共享库和头文件 4、mariadb-connector-c