在 cmakelist.txt 中添加和链接 mysql 库

编程入门 行业动态 更新时间:2024-10-14 18:13:05
本文介绍了在 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_options 和 target_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屋!

更多推荐

[db:关键词]

本文发布于:2023-03-31 05:00:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/795902.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:链接   cmakelist   txt   mysql

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!