1

I need to link my project to the libmysql.dll dynamic library (I need to do it because I'm building my project as /MDd, reference: https://dev.mysql.com/doc/refman/5.6/en/c-api-building-clients.html)

Now the tricky part is that it is an import library (reference: https://msdn.microsoft.com/en-us/library/d14wsce5.aspx) so there is a libmysql.lib as well.

I'm using CMake for the build:

set(MYSQL_DIR "C:/Program Files/MySQL/MySQL Connector C 6.1"
    CACHE PATH "The path to the MySQL C API library")
include_directories(${MYSQL_DIR}/include)
find_library(mysql NAMES libmysql PATHS ${MYSQL_DIR}/lib)
message(STATUS "mysql library: " ${mysql})

CMake finds the library libmysql.lib but when I try to compile I get the following linker error:

LINK : fatal error LNK1104: cannot open file 'mysql.lib'

mysql as you can check above is the name of the CMake variable that contains the path to libmysql.lib.

I have tried to link directly to the .dll but it does not work either, CMake does not find the .dll.

Question

How should I proceed in CMake to link to the import library? Thanks for any help.

1 Answers1

2

You need to use result of find_library() call in target_link_libraries(). In your case it is target_link_libraries(main ${mysql}).

arrowd
  • 33,231
  • 8
  • 79
  • 110
  • thanks @arrowd it compiles now, but when launching the program I get an error telling me that **libmysql.dll** is missing from my pc. I think I should somehow let know to CMake where to find the .dll as well. – Giancarlo Giuffra Feb 19 '16 at 15:46
  • You need to place it along your .exe file. – arrowd Feb 19 '16 at 15:57
  • is there any other way to specify where to look for the dll? ideally with CMake? – Giancarlo Giuffra Feb 19 '16 at 16:00
  • 1
    It is OS loader that looks for libraries. For Windows the rules are: look in the .exe dir, look in the working dir, look in the Windows\System32 dir. – arrowd Feb 19 '16 at 16:02
  • thanks @arrowd. I found another way of doing it by setting the path environment variable as explained in this answer http://stackoverflow.com/a/2119707/4970597 – Giancarlo Giuffra Feb 19 '16 at 16:15