Use CMAKE_<LANG>_IMPLICIT_LINK_DIRECTORIES as CMAKE_SYSTEM_LIBRARY_PATH
As an user I expect to the same behavior if I link to a library as if I use find_library
and use the result to link to it.
Given the following code:
cmake_minimum_required(VERSION 3.1)
project(testodbc C)
set(library_name odbc32)
unset(found_library CACHE)
find_library(found_library ${library_name})
include(CheckCSourceCompiles)
foreach(library ${found_library} ${library_name})
get_filename_component(library_var ${library} NAME_WE)
unset(${library_var} CACHE)
set(CMAKE_REQUIRED_LIBRARIES ${library})
check_c_source_compiles([[
#ifdef WIN32
# include <windows.h>
#endif
#include <sql.h>
int main()
{
SQLHENV env;
SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &env);
SQLFreeHandle(SQL_HANDLE_ENV, env);
return 0;
}]] ${library_var})
message("Trying to link to ${library} -> '${${library_var}}'")
endforeach()
MSVC 🟢:
-- Performing Test odbc32
-- Performing Test odbc32 - Success
Trying to link to C:/Program Files (x86)/Windows Kits/10/Lib/10.0.17763.0/um/x64/odbc32.lib -> '1'
-- Performing Test odbc32
-- Performing Test odbc32 - Success
Trying to link to odbc32 -> '1'
MinGW-GCC 🟡:
-- Performing Test odbc32
-- Performing Test odbc32 - Success
Trying to link to C:/Windows/System32/odbc32.dll -> '1'
-- Performing Test odbc32
-- Performing Test odbc32 - Success
Trying to link to odbc32 -> '1'
MinGW-Clang/LLVM
-- Performing Test odbc32
-- Performing Test odbc32 - Failed
Trying to link to C:/Windows/System32/odbc32.dll -> ''
-- Performing Test odbc32
-- Performing Test odbc32 - Success
Trying to link to odbc32 -> '1'
By setting set(CMAKE_LIBRARY_PATH "${CMAKE_C_IMPLICIT_LINK_DIRECTORIES}")
the output is exactly what I expected in the first place:
MSVC 🟢:
-- Performing Test odbc32
-- Performing Test odbc32 - Success
Trying to link to C:/Program Files (x86)/Windows Kits/10/Lib/10.0.17763.0/um/x64/odbc32.lib -> '1'
-- Performing Test odbc32
-- Performing Test odbc32 - Success
Trying to link to odbc32 -> '1'
MinGW-GCC 🟢:
-- Performing Test libodbc32
-- Performing Test libodbc32 - Success
Trying to link to C:/mingw64/x86_64-w64-mingw32/lib/libodbc32.a -> '1'
-- Performing Test odbc32
-- Performing Test odbc32 - Success
Trying to link to odbc32 -> '1'
MinGW-Clang/LLVM 🟢:
-- Performing Test libodbc32
-- Performing Test libodbc32 - Success
Trying to link to C:/llvm-mingw/x86_64-w64-mingw32/lib/libodbc32.a -> '1'
-- Performing Test odbc32
-- Performing Test odbc32 - Success
Trying to link to odbc32 -> '1'
If CMAKE_SYSTEM_LIBRARY_PATH
would contain the CMAKE_<LANG>_IMPLICIT_LINK_DIRECTORIES
then the code would work as expected from the first time.
This is not only about odbc32
but any library part of the compiler link directories, being libatomic
on GCC, or other libraries like zlib
which would magically work if you use -lz
but not if you search for them using find_library
.