如何使用cmake查找库?

发布于 2024-09-12 11:41:17 字数 656 浏览 2 评论 0原文

要将可执行文件与驻留在标准位置的库链接,可以在 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 应该真正链接到放置在 sibdirectory 中的库?我想知道 target_link_libraries 有一些有用的选项,可以指定该库位于标准位置的子目录中,例如,

target_link_libraries(generate_mesh OpenMesh/OpenMeshCore)

如果不可能,是否有办法使用 find_library 来搜索 /usr/local/lib< /code> 递归地,包括它的子目录,对于给定的库文件?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

挽清梦 2024-09-19 11:41:17

您可以将不同的目录添加到find_library。要使用此库,请通过 cmake -DFOO_PREFIX=/some/path ... 调用 cmake。

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})

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})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文