Add and link mysql libraries in cmakelist.txt

I am working on a project where I need to use MySQL LIBRARIES. I have had success in the past using a simple makefile where I wrote specific flags.

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

However ... my project needs to use cmakelist and I am having difficulty with this. 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 have problems. I tried many things unsuccessfully, but I find this to be similar to the GTK example. Can someone help me with this problem?

0
mysql libraries cmake
source share
1 answer

The easiest way is to find (e.g. with google) a FindMySQL.cmake script that works for you. This script can be used with the 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}) 

(The variable names MYSQL_INCLUDE_DIR and MYSQL_LIB may be different for a particular script).

But it’s easy to manually link the MySQL library, knowing the way to calculate CFLAGS and LIBS.

During the configuration phase (execution of cmake ), programs can be executed with execute_process , to add CFLAGS and LIBS for a specific purpose, use target_compile_options and target_link_libraries respectively:

 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}) 
+1
source share

All Articles