Hellhound
Hellhound

Reputation: 531

CMake FIND_LIBRARY returns wrong path

In my project I use differend 3rd libraries, which has been prebuild and added to external-debs subdirectory. I use multiple FindXXX CMake scripts to locate those requirements.

When I try to locate a FreeImage prebuild, CMake find the library successfully but returns always the wrong path lib/libfreeimage.a instead of ./external-debs/freeimage/lib/windows/x64/libfreeimage.a

This is my search operation:

FIND_LIBRARY(FREE_IMAGE_LIBRARY 
             NAMES ${LIB_PREFIX}freeimage.${LIB_POSTFIX}
             HINTS ${FREE_IMAGE_LIBRARY_DIR}
)

And this are my predefintions:

# Determine platform architecture
IF (CMAKE_SIZEOF_VOID_P EQUAL 8 )
  SET(ARCHITECTURE "x64")
ELSE()
  SET(ARCHITECTURE "x86")
ENDIF()

#Determine platform specific include and library 
#paths settings.
IF (WIN32)
   IF (MINGW)
      SET (LIB_PREFIX "lib")
      SET (LIB_POSTFIX "a")
   ELSEIF (MSVC)
      SET (LIB_POSTFIX "lib")
   ENDIF(MINGW)   

   SET (PLATFORM "windows")   
ELSE (UNIX)
   SET (LIB_PREFIX "lib")
   SET (LIB_POSTFIX "a")
   SET (PLATFORM "linux") 
ENDIF (WIN32)

SET(FREE_IMAGE_INCLUDE_DIR  ${CMAKE_SOURCE_DIR}/external-deps/freeimage/include) 
SET(FREE_IMAGE_LIBRARY_DIR  ${CMAKE_SOURCE_DIR}/external-deps/freeimage/lib/${PLATFORM}/${ARCHITECTURE})

I use this same code to locate an other pre-build 3rd libary dependency (jsoncpp) located in same external-deps subfolder without any issue.

Execute: FindJSON.cmake
-- [INFO]: Find JSON include: ./external-deps/jsoncpp/include
-- [INFO]: Find JSON lib: ./external-deps/jsoncpp/lib/windows/x64/libjsoncpp.a
Execute: FindFreeImage.cmake
-- [INFO]: Find FreeImage include: ./external-deps/freeimage/include
-- [INFO]: Find FreeImage lib: /lib/libfreeimage.a

I'am using CMake 2.8.x on windows platform. Has anyone an idea why CMake sometimes return a wrong library path?

Upvotes: 3

Views: 5035

Answers (2)

Daniel
Daniel

Reputation: 317

One thing to remember about find_library() is that CMake does not do the search if it thinks it already knows where the library is. Removing CMake's cache (build directory) can often help.

Upvotes: 1

ComicSansMS
ComicSansMS

Reputation: 54737

It looks like CMake has found another installation of the library in your MinGW environment. This might be a false positive.

Use NO_DEFAULT_PATH if you want to force CMake to use a library from a specific directory:

FIND_LIBRARY(FREE_IMAGE_LIBRARY 
         NAMES ${LIB_PREFIX}freeimage.${LIB_POSTFIX}
         HINTS ${FREE_IMAGE_LIBRARY_DIR}
         NO_DEFAULT_PATH
)

Take a look at the CMake command reference to get some insight on the subtle rules that drive the path resolution in find_library.

Upvotes: 4

Related Questions