问题描述
我在一个项目中工作,我需要使用MySQL LIBRARIES。我以前有成功,使用一个简单的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?
推荐答案
最简单的方法可以是找到(例如用google) 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_DIR
code> MYSQL_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
)程序可以使用,用于为特定目标使用添加CFLAGS和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})
这篇关于在cmakelist.txt中添加和链接mysql库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!