简体   繁体   中英

How to get CMake find_library to find correct library for build type

I'm trying to get find_library in CMake to correctly find a third party library for linking into my project. However, it is returning the 32bit version of the library even though the build target is 64bit. Is there a way to force it to use the correct library based on the build target?

I have a directory which contains two versions of a library, someLib.lib (32 bit) and someLib_64.lib (64 bit).

I used the Visual Studio 2019 CMake template to create a new project with a debug x64 configuration. When I attempt to build the project I get an error as it is trying to link the 32 bit library to the 64 bit application. Looking in the CMake variable cache shows that the find_library call has resolved to someLib.lib instead of someLib_64.lib. This happens even if I specify someLib_64 under the NAMES clause of the find_library call.

cmake_minimum_required (VERSION 3.8)

find_path (LIB_INCLUDE_DIR someLib.h c:/Progra~1/someLib/include /usr/include)

find_library (SOME_LIB NAMES someLib_64 HINTS c:/Progra~1/someLib/Lib /usr/lib)
if (NOT DEFINED ${SOME_LIB})
    find_library (SOME_LIB NAMES someLib HINTS c:/Progra~1/someLib/Lib /usr/lib)
endif (NOT DEFINED ${SOME_LIB})

# Add source to this project's executable.
add_executable (myProject "myProject.cpp" "myProject.h")
target_link_libraries (myProject ${SOME_LIB})
target_include_directories (myProject PUBLIC ${LIB_INCLUDE_DIR})

I would expect this to return c:/Program Files/SomeLib/Lib/someLib_64.lib but instead it returns c:/Program Files/SomeLib/Lib/someLib.lib. Is there a way to get it to correctly link the 64 bit library when building a 64 bit project?

Turns out it was a simple solution - I had to delete and recreate the CMake cache (Project -> CMake Cache -> Delete Cache). As I was developing the script at some point it had bound to the 32 bit version and despite selecting generate cache it wasn't actually updating the variable assignment.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM