15

要将可执行文件与位于标准位置的库链接,可以在 CmakeLists.txt 文件中执行以下操作:

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

如果将要链接的库放置在

/usr/local/lib/libOpenMeshCore.dylib

但是,在这种情况下,库位于

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

如何指定 target_link_libraries 应该真正链接到放置在 sib 目录中的库?我想知道 target_link_libraries 有一些有用的选项可以指定该库位于标准位置的子目录中,例如

target_link_libraries(generate_mesh OpenMesh/OpenMeshCore)

如果这是不可能的,有没有办法使用 find_library/usr/local/lib递归搜索给定的库文件,包括它的子目录?

4

1 回答 1

23

您可以将不同的目录添加到find_library. 要使用这个库调用 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})
于 2010-08-02T11:25:30.493 回答