Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find a library with cmake?

To link an executable with a library that resides in a standard location, one can do the following in a CmakeLists.txt file:

create_executable(generate_mesh generate_mesh.cpp)
target_link_libraries(generate_mesh OpenMeshCore)

This would work if the library, that is being linked against, was placed in

/usr/local/lib/libOpenMeshCore.dylib

However, in this case the library resides under

/usr/local/lib/OpenMesh/libOpenMeshCore.dylib

How can I specify that target_link_libraries should really link against a library placed in a sibdirectory? I wonder there is some useful option to target_link_libraries that would specify that the library is in a subdirectory in a standandard location, e.g.

target_link_libraries(generate_mesh OpenMesh/OpenMeshCore)

If that is not possible, is there a way to use find_library to search /usr/local/lib recursively, including its sub-directories, for the given library file?

like image 694
D R Avatar asked Aug 01 '10 05:08

D R


People also ask

Where is my CMake library?

A short-hand signature is: find_library (<VAR> name1 [path1 path2 ...]) This command is used to find a library. A cache entry, or a normal variable if NO_CACHE is specified, named by <VAR> is created to store the result of this command.

Where are CMake packages?

CMake searches for a file called Find<package>. cmake in the CMAKE_MODULE_PATH followed by the CMake installation. If the file is found, it is read and processed by CMake. It is responsible for finding the package, checking the version, and producing any needed messages.


1 Answers

You can add different directories to find_library. To use this library call cmake by cmake -DFOO_PREFIX=/some/path ....

find_library( CPPUNIT_LIBRARY_DEBUG NAMES cppunit cppunit_dll cppunitd cppunitd_dll
            PATHS   ${FOO_PREFIX}/lib
                    /usr/lib
                    /usr/lib64
                    /usr/local/lib
                    /usr/local/lib64
            PATH_SUFFIXES debug )

find_library( CPPUNIT_LIBRARY_RELEASE NAMES cppunit cppunit_dll
            PATHS   ${FOO_PREFIX}/lib
                    /usr/lib
                    /usr/lib64
                    /usr/local/lib
                    /usr/local/lib64
            PATH_SUFFIXES release )

if(CPPUNIT_LIBRARY_DEBUG AND NOT CPPUNIT_LIBRARY_RELEASE)
    set(CPPUNIT_LIBRARY_RELEASE ${CPPUNIT_LIBRARY_DEBUG})
endif(CPPUNIT_LIBRARY_DEBUG AND NOT CPPUNIT_LIBRARY_RELEASE)

set( CPPUNIT_LIBRARY debug     ${CPPUNIT_LIBRARY_DEBUG}
                    optimized ${CPPUNIT_LIBRARY_RELEASE} )

# ...
target_link_libraries(foo ${CPPUNIT_LIBRARY})
like image 88
Rudi Avatar answered Oct 10 '22 03:10

Rudi