CMake linking to an import library - cmake

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.

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

Related

cmake link to macOS pre-compiled binaries at custom path

I want to link to the GLFW library using the binaries provided when downloading from the website at a custom path in my project:
include_directories(PROJECT_SOURCE_DIR/third_party/glfw/lib-arm64)
add_executable(rpg main.cpp)
target_include_directories(rpg PRIVATE ${PROJECT_SOURCE_DIR}/third_party/glad/include ${PROJECT_SOURCE_DIR}/third_party/glfw/include)
find_library(GLFW_LIB NAMES glfw3 PATHS PROJECT_SOURCE_DIR/third_party/glfw PATH_SUFFIXES lib-arm64 lib-universal lib-x86_64)
target_link_libraries(rpg PRIVATE ${GLFW_LIB})
I've tried finding both glfw and glfw3 but this fails to find the GLFW library:
CMake Error: The following variables are used in this project, but they are set to NOTFOUND.
Please set them or make sure they are set and tested correctly in the CMake files:
GLFW_LIB
linked by target "rpg" in directory /Users/ashley/Personal/rpg/src
How should I specify the path to link to these rather than use a globally installed version or building from source myself?
Edit
Correcting the use of find_library as per #Tsyvarev suggestion solved this. The prior update to say it still didn't work was in haste - I wasn't correctly expanding the path variable.

CMake library file missing

I'm currently struggling with cmake.
I'm using Cmake for an embedded platform with GCC.
My project is separate into several modules. Each module is build into a static library. At link time, all of these libraries are collected and linked into one binary.
The problem: I created a new folder for some unit tests. All sources are build into a library libunit_tests.a.(I checked the library actually gets created).
However in my linker call other libraries are passed to the linker, mine however gets omitted resulting in an undefined reference error.
My folder structure looks like this
*
unit_tests/
*
unit_tests/inc
*unit_tests/src
There is one Cmake file located at
- /unit_tests/CMakeLists.txt
My actual CMakeLists.txt file is pretty basic
include_directories("./inc")
set(module_name "unit_tests")
set(MODULE_SOURCES
./inc/active_tests.h
./inc/Run_All_Tests.h ./src/Run_All_Tests.c
)
###########################
# add library
###########################
if(MODULE_SOURCES)
# add files to library
add_library("${module_name}"
${MODULE_SOURCES})
target_link_libraries("${module_name}"
-Wl,--start-group
-Wl,--end-group)
endif()
How do i pass this library to the linker to resolve the undefined reference error?
I thought this is done via add_libary and target_link_libraries?

Creating a findable shared library with cmake

I am rewriting libraries from hand-written Makefiles to using cmake. I am getting stuck at the point where I need to library library A from library B.
I can find the libraries using find_package, but when they are being linked cmake complains about not having a rule for building the .so file because it is looking for it in the build directory instead of the installed directory.
This is explained if I look at the /usr/lib/cmake/library/libraryConfigVersion.cmake file, which contains this hardcoded path. This file was created with the following steps:
write_basic_package_version_file(
"${CMAKE_CURRENT_BINARY_DIR}/libraryConfigVersion.cmake"
VERSION ${LIBRARY_VERSION}
COMPATIBILITY AnyNewerVersion
)
export(EXPORT libraryTargets
FILE "${CMAKE_CURRENT_BINARY_DIR}/library/libraryConfigVersion.cmake"
NAMESPACE library::
)
(i have replaced my library name with 'library'). How can I get cmake to write the correct path so that I can easily link against my library from other cmake projects?
Command export actually exports build tree. This is explicitely written in the documentation.
For export install tree, use install(TARGETS ... EXPORT) plus install(EXPORT). Both flows are described in documentation for install command.
See also CMake tutorial Exporting and Importing Targets.

GLEW with CMakeLists

I have the following CMakeLists to include glew into my project. Compiling the project works fine but when I run the project I get an error saying that the program can't find glew32.dll. Any ideas why?? Thanks for answers!
set(CMAKE_PREFIX_PATH "${CMAKE_SOURCE_DIR}/external/glew-1.12.0")
set(CMAKE_LIBRARY_PATH "${CMAKE_SOURCE_DIR}/external/glew-1.12.0/lib/Release/Win32/")
find_package(GLEW REQUIRED)
if(GLEW_FOUND)
message("GLEW Found!")
include_directories(${GLEW_INCLUDE_DIRS})
link_libraries(${GLEW_LIBRARIES})
add_definitions(${GLEW_DEFINITIONS})
else(GLEW_FOUND)
message(FATAL_ERROR "GLEW NOT Found")
endif(GLEW_FOUND)
add_executable(Project ${CODE})
target_link_libraries(Project ${GLEW_LIBRARIES})
You need to have the directory where glew32.dll can be found in your environment's PATH variable. If you're using MSVC set the Environment textbox in your property pages of your application:
PATH=<dir-of-glew32.dll>;%PATH%
If you're running from shell, issue the same command on the command line.
Another option is to use GLEW as a static library. In that case you will not need glew32.dll, the entire glew library will be linked into your program. Check out the GLEW github repository and study the script cmake-testbuild.sh about how to use GLEW as a static library.
Please note, that the GLEW github repository does not contain certain generated files. If you can't run make extensions on your platform, use this repository: glew-with-extensions which already contains the generated files.

Linking libraries using CMake and PkgConfig

I am trying to link a static library (GLFW) to my own library that I am building. I have the following in my CMakeLists.txt file in order to do this:
pkg_search_module(GLFW REQUIRED glfw3)
include_directories(${GLFW_INCLUDE_DIRS})
target_link_libraries(${LIBRARY_NAME} ${GLFW_STATIC_LIBRARIES})
When linking my library, I get the following error: ld: library not found for -lglfw3
Yet, running pkg-config --libs glfw3 in the console gives:
-L/usr/local/lib -lglfw3
So I know that the GLFW library is installed. Why isn't the library being found when I try linking using CMake?
I got the same error when using the argument -lglfw3, and after much trial and error I found I needed to use -lglfw.3
You're adding the library name but not the the linker search path. Try:
link_directories(${GLFW_LIBRARY_DIRS})