Skip to content

Instantly share code, notes, and snippets.

@griwes
Created June 29, 2016 12:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save griwes/ab353f2bada761fd37c2496b41971786 to your computer and use it in GitHub Desktop.
Save griwes/ab353f2bada761fd37c2496b41971786 to your computer and use it in GitHub Desktop.
stupid cmake hacks
On 03/10/2011 12:25 AM, Andreas Pakulat wrote:
> On 09.03.11 21:36:25, Ankur Handa wrote:
>> I'm using find_library to find a library in a given directory but it has
>> many different versions of this library listed as libcxcore.so,
>> libcxcore.so.2.1 and libcxcore.so.2.1.0  I wrote a very simple cmake file
>>
>> cmake_minimum_required(VERSION 2.8)
>>
>> find_library(OpenCV_LIBRARY_RELEASE NAMES "cxcore" PATHS
>> "/homes/ahanda/openCV2.1/OpenCV-2.1.0/opencvinstall/lib" NO_DEFAULT_PATH)
>>
>> if ( OpenCV_LIBRARY_RELEASE)
>>         message("LIBRARY FOUND = " ${OpenCV_LIBRARY_RELEASE})
>> endif()
>>
>> if ( NOT OpenCV_LIBRARY_RELEASE)
>>         message("LIBRARY NOT FOUND")
>> endif()
>>
>> It works fine if I search for "cxcore" for which it returns me libcxcore.so
>> and the OpenCV_LIBRARY_RELEASE
>> as /homes/ahanda/openCV2.1/OpenCV-2.1.0/opencvinstall/lib/libcxcore.so
>>
>> However if I try to search for cxcore21 or cxcore210 or even
>> libcxcore.so.2.1.0 it won't return me anything and I'd always see LIBRARY
>> NOT FOUND being printed.
>>
>> Could anyone please let me know how to really search for libcxcore.so.2.1.0
> 
> You can't I think. Thats simply not how cmake or the linker work. The
> linker during the compilation phase will always link against
> lib<name>.so. So you need to make sure that your lib<name>.so is from
> cxcore 2.1.0 and search simply for cxcore.

The linker exactly links against what is denoted on the command line,
so if you mention /homes/ahanda/.../libcxcore.so.2.1.0 directly, it
will take right this file for resolving symbols regardless whether
and how the libcxcore.so symlink is set.

The OP's issue is that FIND_LIBRARY() is somewhat restricted w.r.t. the
names of the library to look for; AFAICS, it works roughly as follows:

- If the user-supplied name in consideration has a valid library suffix
  - specified by CMAKE_FIND_LIBRARY_SUFFIXES - it is searched literally.
- If no literally matching library is found, FIND_LIBRARY() looks for a
  concatenation of the library prefixes in CMAKE_FIND_LIBRARY_PREFIXES,
  the supplied name and the library suffixes as a regular expression,
  i.e. (lib)<name>(\.so|\.a) on *nix.

Hence, FIND_LIBRARY() can't find libcxcore.so.2.1.0 even if the latter
is specified literally because ".so.2.1.0" is no valid library suffix.
A possible solution - provided that one definitely knows that exactly
libcxcore.so.2.1.0 should be found - is to temporarily add ".so.2.1.0"
to CMAKE_FIND_LIBRARY_SUFFIXES. So, Ankur, try

LIST(APPEND CMAKE_FIND_LIBRARY_SUFFIXES ".so.2.1.0")
FIND_LIBRARY(OpenCV_LIBRARY_RELEASE NAMES "libcxcore.so.2.1.0" ...)
LIST(REMOVE_ITEM CMAKE_FIND_LIBRARY_SUFFIXES ".so.2.1.0")

and you'll probably see libcxcore.so.2.1.0 being found. Alternatively,
you might use FIND_PATH() or FIND_FILE() to search libcxcore.so.2.1.0,
but in that way, you would lose FIND_LIBRARY()'s special capabilities,
e.g. looking in CMAKE_LIBRARY_PATH or CMAKE_PREFIX_PATH/lib.

Regards,

Michael
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment