1

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?

core
  • 13
  • 1
  • 4

1 Answers1

5

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

(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 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})
huch
  • 675
  • 8
  • 13
Tsyvarev
  • 60,011
  • 17
  • 110
  • 153
  • So many thanks to you!!! I just had to adapt: target_compile_options(cgm ${MYSQL_CFLAGS}) to SET( CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${GCC_MYSQL_COMPILE_FLAGS}" ) and it worked perfectly! You helped me a lot! – core Jan 28 '16 at 21:06
  • @ThomasKettenbach: Next time, before suggest an edit to a code or some other important part of the post, ask the post's author about your intent in the comments. Otherwise you risk to have your suggested edit declined as "conflicted with author's intention". Note, that rewiers of suggested edits are not always very familiar with the topic. See [help/editing](https://stackoverflow.com/help/editing) for more details about editing other's posts. – Tsyvarev Feb 14 '18 at 13:13
  • @Tsyvarev: Thanks for letting me know, i will keep this in mind from now on. And thanks for accepting my edit. My changes were: Added `OUTPUT_STRIP_TRAILING_WHITESPACE` to execute_process and scope `PUBLIC` in target_compile_operations. I've tested with `mariadb-devel-5.5.56-2.el7` on a centos 7. My issue was: mysql_config tool adds a newline char at the end, which is reported as an error by cmake 3.4.3 – huch Feb 15 '18 at 06:34
  • @ThomasKettenbach: Yes, such sort of comment would be nice before suggesting an edit in the code. Even if you won't get responce on it (after waiting a day at least), you may try to suggest an edit anywhere: reviewers see the comments and may find explanations very reasonable for accept an edit. As for my answer and code in it, *OUTPUT_STRIP_TRAILING_WHITESPACE* is actually an useful option for almost any program's output, and, unlike for many other commands, `target_compile_options` actually **requires** *PUBLIC* (or some other) keyword. – Tsyvarev Feb 15 '18 at 08:45