0

I'm trying to use mariadb in my C application, normally I run my application with a make file containing the following code: gcc -g src/*.c -o applicazione `mariadb_config --cflags --include --libs`

I am trying to use Clion and Cmakelist with the following code:

project(project C)

set(CMAKE_C_STANDARD 99)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})

FIND_LIBRARY(mariadb mariadb)


include_directories(/usr/include/mariadb)
link_directories(/usr/include/mariadb)

include_directories(/usr/include/mariadb/mysql)
link_directories(/usr/include/mariadb/mysql)


add_executable(project main.c file1.c file2.c file3.c)

But I get this compilation error, and other similar errors for other mysql functions:

 undefined reference to `mysql_error'

I think this library libmariadbclient.so is missing to insert but I can't do it

tecn603
  • 211
  • 5
  • 14
  • Did you look up how to use a library in cmake? – user253751 Aug 21 '20 at 12:16
  • 1
    You forgot `target_link_libraries` call which performs linking. The duplicate question and its answers describe how this call could be written. – Tsyvarev Aug 21 '20 at 12:17
  • i didn't quite understand how to link the target library. I try this `find_library(maria_db_lib libmariadbclient) target_link_libraries(project PRIVATE "${maria_db_lib}") ` but I get the error: `Cannot specify link libraries for target "project" which is not built by this project`. I think the library path is this: `/usr/lib/x86_64-linux-gnu/libmariadbclient.so` – tecn603 Aug 21 '20 at 12:30
  • 1
    `target_link_libraries` call should come **after** `add_executable` one. Look, call to `target_link_libraries(project ...)` uses `project` target which is defined only with `add_executable(project ...)` call. – Tsyvarev Aug 21 '20 at 17:43
  • thanks, I use `target_link_libraries(project PRIVATE libmariadbclient.so)` after `add_executable(project ...)` and now it work – tecn603 Aug 21 '20 at 18:11

0 Answers0