在 cmakelist.txt 中添加和链接 mysql 库 [英] Add and link mysql libraries in a cmakelist.txt

查看:155
本文介绍了在 cmakelist.txt 中添加和链接 mysql 库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个需要使用 MySQL 库的项目中工作.我在过去取得了成功,使用了一个简单的 makefile,我在其中编写了特定的标志.

I'm working in a project where I need to use MySQL LIBRARIES. I had success in the past, using a simple makefile where I wrote the specific flags.

CFLAGS+=`mysql_config --cflags`
LIB+=`mysql_config --libs`

但是...对于我的项目需要使用 cmakelist,我对此有困难.我可以使用以下代码添加 GTK 库:

However... for my project is required to use a cmakelist and I'm having difficulties with that. I can add GTK libraries with this code:

find_package(PkgConfig REQUIRED)
pkg_check_modules(GTK REQUIRED gtk+-3.0)

include_directories(${GTK_INCLUDE_DIRS})
link_directories(${GTK_LIBRARY_DIRS})
target_link_libraries( cgm ${GTK_LIBRARIES} )

但是对于 MySQL,我遇到了麻烦.我尝试了很多事情都没有成功,但我相信这类似于 GTK 示例.谁能帮我解决这个问题?

but for MySQL I'm in trouble. I tried many things unsuccessfully, but I believe that is similar to the GTK example. Can anyone help me with this problem?

推荐答案

最简单的方法是找到(例如使用谷歌)FindMySQL.cmake 脚本,它对你有用.这个脚本可以像往常一样与 find_package 命令一起使用:

The simplest way could be to find (e.g. with google) FindMySQL.cmake script, which works for you. This script can be used with find_package command as usual:

list(CMAKE_MODULE_PATH APPEND <directory-where-FindMySQL.cmake-exists>)
find_package(MySQL REQUIRED)

include_directories(${MYSQL_INCLUDE_DIR})
target_link_libraries(cgm ${MYSQL_LIB})

(变量名MYSQL_INCLUDE_DIRMYSQL_LIB 的具体脚本可以不同).

(Names of variables MYSQL_INCLUDE_DIR and MYSQL_LIB can be different for concrete script).

但是手动链接 MySQL 库并不难,知道计算 CFLAGS 和 LIBS 的方法.

But it is not difficult to link with MySQL library manually, knowing way for compute CFLAGS and LIBS.

在配置阶段(执行cmake)程序可以用execute_process,用于为特定目标添加 CFLAGS 和 LIBS 使用 target_compile_optionstarget_link_libraries 相应地:

During configuration stage(executing of cmake) programs can be run with execute_process, for add CFLAGS and LIBS for specific target use target_compile_options and target_link_libraries correspondingly :

execute_process(COMMAND mysql_config --cflags
    OUTPUT_VARIABLE MYSQL_CFLAGS OUTPUT_STRIP_TRAILING_WHITESPACE)
execute_process(COMMAND mysql_config --libs
    OUTPUT_VARIABLE MYSQL_LIBS OUTPUT_STRIP_TRAILING_WHITESPACE)

target_compile_options(cgm PUBLIC ${MYSQL_CFLAGS})
target_link_libraries(cgm ${MYSQL_LIBS})

这篇关于在 cmakelist.txt 中添加和链接 mysql 库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆