Search code examples
cmakelibmysqlimport-libraries

CMake linking to an import library


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.


Solution

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