mysql8没有cmakelist,在cmakelist.txt中添加和链接mysql库

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`

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} )

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?

解决方案

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 )

find_package(MySQL REQUIRED)

include_directories(${MYSQL_INCLUDE_DIR})

target_link_libraries(cgm ${MYSQL_LIB})

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

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

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)

execute_process(COMMAND mysql_config --libs

OUTPUT_VARIABLE MYSQL_LIBS)

target_compile_options(cgm ${MYSQL_CFLAGS})

target_link_libraries(cgm ${MYSQL_LIBS})

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值