Kagami Sascha Rosylight
Kagami Sascha Rosylight

Reputation: 1482

CMake find_library can't find my library even with HINTS

I have the following code:

project(test)
cmake_minimum_required(VERSION 3.17)

set(PNG_DIR C:/Users/Kagami/.emscripten_cache/wasm/ports-builds/libpng)

find_library(PNG_STATIC_LIBRARY
  NAMES libpng.a
  HINTS ${PNG_DIR}
)

message(${PNG_DIR})
message(${PNG_STATIC_LIBRARY})

I have a file named libpng.a in that PNG_DIR directory but find_library still returns -NOTFOUND. What would be the possible reasons?

Upvotes: 2

Views: 3862

Answers (2)

ComicSansMS
ComicSansMS

Reputation: 54589

The NAMES parameter to find_library specifies the library name, not the file name.

So in your case the correct command would be

find_library(PNG_STATIC_LIBRARY
  NAMES png
  HINTS ${PNG_DIR}
)

Note that you can give more than one name as arguments to the NAMES, in case the library ships under different names on the different platforms.

An additional complication here comes from the fact that on Windows we have two incompatible toolchains, MinGW and MSVC. If your library has a .a file ending, it was almost certainly compiled for the MinGW toolchain, so you will not be able to use it from a Visual Studio build. Here you would first need to recompile the library with the correct toolchain.

Upvotes: 1

Aurélien Foucault
Aurélien Foucault

Reputation: 559

find_library will find a library that he can be used with the arch you use. So on your Windows it will not look for .a but for .lib.

But find_path just search a file doesn't matter if it's a real library or not.

From the documentation :

Each library name given to the NAMES option is first considered as a library file name and then considered with platform-specific prefixes (e.g. lib) and suffixes (e.g. .so). Therefore one may specify library file names such as libfoo.a directly. This can be used to locate static libraries on UNIX-like systems.

Upvotes: 3

Related Questions