CMake:如何在库中添加子目录?

时间:2019-02-20 02:28:32

标签: cmake subdirectory

我不能强制cmake搜索/ usr / local / lib / db5子目录中的库。

要搜索库,我使用以下脚本:

link_directories(/usr/local/lib/db5 /usr/local/lib /usr/lib)

set (LIBRARIES
        c m util ssl pthread db)

foreach (LIBRARY ${LIBRARIES})
    find_library ("${LIBRARY}_FOUND" ${LIBRARY})
    message (STATUS "Check the ${LIBRARY} is installed: " ${${LIBRARY}_FOUND})
    if ( "${${LIBRARY}_FOUND}" STREQUAL "${LIBRARY}_FOUND-NOTFOUND" )
        message (STATUS "Adding library sources")
        add_subdirectory (../${LIBRARY} lib/${LIBRARY})
    endif ()
endforeach ()

该库肯定在目录中。

ogogon@:/usr/local/src/util# ls /usr/local/lib/db5
libdb_cxx-5.3.a     libdb_cxx-5.3.so.0.0.0  libdb_cxx.so        libdb_stl-5.3.so.0  libdb_stl.a     libdb-5.3.so        libdb-5.so
libdb_cxx-5.3.so    libdb_cxx-5.so      libdb_stl-5.3.a     libdb_stl-5.3.so.0.0.0  libdb_stl.so        libdb-5.3.so.0      libdb.a
libdb_cxx-5.3.so.0  libdb_cxx.a     libdb_stl-5.3.so    libdb_stl-5.so      libdb-5.3.a     libdb-5.3.so.0.0.0  libdb.so

图书馆搜索不会成功。

ogogon@ot:/usr/local/src/util# ./configure 
-- The C compiler identification is Clang 6.0.0
-- The CXX compiler identification is Clang 6.0.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Check the c is installed: /usr/lib/libc.so
-- Check the m is installed: /usr/lib/libm.so
-- Check the util is installed: /usr/lib/libutil.so
-- Check the ssl is installed: /usr/lib/libssl.so
-- Check the pthread is installed: /usr/lib/libpthread.so
-- Check the db is installed: db_FOUND-NOTFOUND
-- Adding library sources
CMake Error at CMakeLists.txt:27 (add_subdirectory):
  add_subdirectory given source "../db" which is not an existing directory.


-- Configuring incomplete, errors occurred!
See also "/usr/local/src/util/CMakeFiles/CMakeOutput.log".

如果我从列表中删除库数据库-一切正常。

ogogon@ot:/usr/local/src/util# ./configure 
-- The C compiler identification is Clang 6.0.0
-- The CXX compiler identification is Clang 6.0.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Check the c is installed: /usr/lib/libc.so
-- Check the m is installed: /usr/lib/libm.so
-- Check the util is installed: /usr/lib/libutil.so
-- Check the ssl is installed: /usr/lib/libssl.so
-- Check the pthread is installed: /usr/lib/libpthread.so
-- Configuring done
-- Generating done
-- Build files have been written to: /usr/local/src/util

我在做什么错?我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

现在我这样解决了这个问题:

set (LIBPATHS /usr/local/lib/db5 /usr/local/lib /usr/lib libs5)

set (LIBRARIES
        c m util db ssl pthread)

foreach (LIBRARY ${LIBRARIES})
    find_library ("${LIBRARY}_FOUND" ${LIBRARY} PATHS ${LIBPATHS})
    message (STATUS "Check the ${LIBRARY} is installed: " ${${LIBRARY}_FOUND})
    if ( "${${LIBRARY}_FOUND}" STREQUAL "${LIBRARY}_FOUND-NOTFOUND" )
        message (STATUS "Adding library sources")
        add_subdirectory (../${LIBRARY} lib/${LIBRARY})
    endif ()
endforeach ()

但是,如果Berkley DB库的版本更改,则必须调整路径。有没有办法写类似/ usr / local / lib / db *的东西?