C++ Linux下如何使用libtar

发布于 2024-11-14 03:04:22 字数 611 浏览 2 评论 0原文

我已经下载并安装了libtar。 我已正确添加头文件( #include )。 我使用 KDevelop,当我开始输入函数 tar_append_treetar_open 时,它会自动识别它们。 我将 cmake 与 KDevelop 结合使用(作为创建新项目时的一个选项),到目前为止,所有需要链接的内容(pthreads、共享内存和数学)我所要做的就是在 CMAKE_EXE_LINKER_FLAGS 中添加适当的标志。我知道 libtar 需要 -ltar 才能工作,但我仍然得到:对 tar_open 的未定义引用,对 tar_append_tree 的未定义引用。 有人可以帮忙吗?我不知道有任何其他 tar 库,并且我确实需要在压缩之前在 tarball 中组织数千个 xml 文件。

编辑:我现在发现 libarchive 应该比 libtar 好得多。然而我在这里遗漏了一些东西。我如何明确告诉 KDevelop/Cmake 如何链接这些库? libarchive 没有链接标志(至少我在谷歌上找不到),当我尝试编译时,我再次得到对导入函数的未定义引用。

谢谢你!

I have downloaded and installed libtar.
I have added the header file correctly ( #include <libtar.h> ).
I use KDevelop, which automatically recognizes the functions tar_append_tree and tar_open when I start typing them.
I use cmake with KDevelop (as an option when creating a new project), and so far everything that needed linking (pthreads,shared memory and math) all I had to do was add the appropriate flag in CMAKE_EXE_LINKER_FLAGS. I know that libtar needs -ltar in order to work, but I still get: undefined reference to tar_open , undefined reference to tar_append_tree.
Can anyone please help ? I am not aware of any other tar library, and I really need to organize thousands of xml files in a tarball prior to compressing.

edit: I have now found libarchive which is supposed to be much better than libtar. However I am missing something here. How do I explicitly tell KDevelop/Cmake how to link with those libraries ? There is no link flag for libarchive (at least not one I could find on google) and again when I try to compile I get undefined references to the imported functions.

Thank you!

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

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

发布评论

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

评论(1

荭秂 2024-11-21 03:04:22

我确信解决方案不止一种,但这是我的。 创建了两个文件

  • 下的项目文件夹中
  • 我在 /usr/lib/pkgconfig 中的 cmake/Modules/ libtar.pc FindLIBTAR.cmake

FindLIBTAR.cmake

# - Try to find LIBTAR 
# Find LIBTAR headers, libraries and the answer to all questions.
#
#  LIBTAR_FOUND               True if libuuid got found
#  LIBTAR_INCLUDE_DIRS        Location of libuuid headers 
#  LIBTAR_LIBRARIES           List of libraries to use libuuid 
#

INCLUDE( FindPkgConfig )

IF ( LIBTAR_FIND_REQUIRED )
    SET( _pkgconfig_REQUIRED "REQUIRED" )
ELSE ( LIBTAR_FIND_REQUIRED )
    SET( _pkgconfig_REQUIRED "" )   
ENDIF ( LIBTAR_FIND_REQUIRED )

IF ( LIBTAR_MIN_VERSION )
    PKG_SEARCH_MODULE( LIBTAR ${_pkgconfig_REQUIRED} libtar>=${LIBTAR_MIN_VERSION} )
ELSE ( LIBTAR_MIN_VERSION )
    PKG_SEARCH_MODULE( LIBTAR ${_pkgconfig_REQUIRED} libtar )
ENDIF ( LIBTAR_MIN_VERSION )

IF( NOT LIBTAR_FOUND AND NOT PKG_CONFIG_FOUND )
    FIND_PATH( LIBTAR_INCLUDE_DIRS libtar.h )
    FIND_LIBRARY( LIBTAR_LIBRARIES libtar)

    # Report results
    IF ( LIBTAR_LIBRARIES AND LIBTAR_INCLUDE_DIRS ) 
        SET( LIBTAR_FOUND 1 )
        IF ( NOT LIBTAR_FIND_QUIETLY )
            MESSAGE( STATUS "Found libtar: ${LIBTAR_LIBRARIES}" )
        ENDIF ( NOT LIBTAR_FIND_QUIETLY )
    ELSE ( LIBTAR_LIBRARIES AND LIBTAR_INCLUDE_DIRS )   
        IF ( LIBTAR_FIND_REQUIRED )
            MESSAGE( SEND_ERROR "Could NOT find libtar" )
        ELSE ( LIBTAR_FIND_REQUIRED )
            IF ( NOT LIBTAR_FIND_QUIETLY )
                MESSAGE( STATUS "Could NOT find libtar" )   
            ENDIF ( NOT LIBTAR_FIND_QUIETLY )
        ENDIF ( LIBTAR_FIND_REQUIRED )
    ENDIF ( LIBTAR_LIBRARIES AND LIBTAR_INCLUDE_DIRS )
ENDIF( NOT LIBTAR_FOUND AND NOT PKG_CONFIG_FOUND )

MARK_AS_ADVANCED( LIBTAR_LIBRARIES LIBTAR_INCLUDE_DIRS )

libtar.pc

###########################################################################
# libtar installation details
###########################################################################

prefix=/usr
exec_prefix=${prefix}
libdir=${exec_prefix}/lib
includedir=${prefix}/include

Name: libtar
URL: http://www.feep.net/libtar/
Description: Library for Tar files
Version: 1.2.11-8
Libs: -L${libdir} -ltar -Wl,-Bsymbolic-functions -Wl,-z,relro 
Libs.private: 
Cflags: -I${includedir}

FindLIBTAR.cmake 使用 pkgconfig 查找 libtar 库和 libtar.pc< pkgconfig 使用 /code> 来查找库。如果您不想将 cmake 文件放在项目目录中,则可以将 cmake 文件放在 CMake 放置所有查找文件的同一位置 (/usr/share/cmake/Modules/)。如果您确实将其保留在项目文件夹中,则需要通过以下方式将其添加到 CMAKE_MODULE_PATH 中:

SET(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake/Modules/")

I'm sure there is more than one solution, but here is mine. I created two files

  • FindLIBTAR.cmake in my project's folder under cmake/Modules/
  • libtar.pc in /usr/lib/pkgconfig

FindLIBTAR.cmake

# - Try to find LIBTAR 
# Find LIBTAR headers, libraries and the answer to all questions.
#
#  LIBTAR_FOUND               True if libuuid got found
#  LIBTAR_INCLUDE_DIRS        Location of libuuid headers 
#  LIBTAR_LIBRARIES           List of libraries to use libuuid 
#

INCLUDE( FindPkgConfig )

IF ( LIBTAR_FIND_REQUIRED )
    SET( _pkgconfig_REQUIRED "REQUIRED" )
ELSE ( LIBTAR_FIND_REQUIRED )
    SET( _pkgconfig_REQUIRED "" )   
ENDIF ( LIBTAR_FIND_REQUIRED )

IF ( LIBTAR_MIN_VERSION )
    PKG_SEARCH_MODULE( LIBTAR ${_pkgconfig_REQUIRED} libtar>=${LIBTAR_MIN_VERSION} )
ELSE ( LIBTAR_MIN_VERSION )
    PKG_SEARCH_MODULE( LIBTAR ${_pkgconfig_REQUIRED} libtar )
ENDIF ( LIBTAR_MIN_VERSION )

IF( NOT LIBTAR_FOUND AND NOT PKG_CONFIG_FOUND )
    FIND_PATH( LIBTAR_INCLUDE_DIRS libtar.h )
    FIND_LIBRARY( LIBTAR_LIBRARIES libtar)

    # Report results
    IF ( LIBTAR_LIBRARIES AND LIBTAR_INCLUDE_DIRS ) 
        SET( LIBTAR_FOUND 1 )
        IF ( NOT LIBTAR_FIND_QUIETLY )
            MESSAGE( STATUS "Found libtar: ${LIBTAR_LIBRARIES}" )
        ENDIF ( NOT LIBTAR_FIND_QUIETLY )
    ELSE ( LIBTAR_LIBRARIES AND LIBTAR_INCLUDE_DIRS )   
        IF ( LIBTAR_FIND_REQUIRED )
            MESSAGE( SEND_ERROR "Could NOT find libtar" )
        ELSE ( LIBTAR_FIND_REQUIRED )
            IF ( NOT LIBTAR_FIND_QUIETLY )
                MESSAGE( STATUS "Could NOT find libtar" )   
            ENDIF ( NOT LIBTAR_FIND_QUIETLY )
        ENDIF ( LIBTAR_FIND_REQUIRED )
    ENDIF ( LIBTAR_LIBRARIES AND LIBTAR_INCLUDE_DIRS )
ENDIF( NOT LIBTAR_FOUND AND NOT PKG_CONFIG_FOUND )

MARK_AS_ADVANCED( LIBTAR_LIBRARIES LIBTAR_INCLUDE_DIRS )

libtar.pc

###########################################################################
# libtar installation details
###########################################################################

prefix=/usr
exec_prefix=${prefix}
libdir=${exec_prefix}/lib
includedir=${prefix}/include

Name: libtar
URL: http://www.feep.net/libtar/
Description: Library for Tar files
Version: 1.2.11-8
Libs: -L${libdir} -ltar -Wl,-Bsymbolic-functions -Wl,-z,relro 
Libs.private: 
Cflags: -I${includedir}

The FindLIBTAR.cmake uses pkgconfig to find the libtar library and libtar.pc is used by pkgconfig to find the library. You can place the cmake file in the same place where CMake places all find files (/usr/share/cmake/Modules/) if you don't want to put it in your project's directory. If you do keep it in your project's folder, you will need to add that to CMAKE_MODULE_PATH via:

SET(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake/Modules/")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文