从 Objective-C 链接C++库(libtorrent)

Linking C++ library (libtorrent) from Objective-C

本文关键字:libtorrent C++ Objective-C 链接      更新时间:2023-10-16

我正在尝试使用Xcode 5.0 Objective-C Project中的libtorrent库,但没有成功。

我已经使用 LLVM 5.0 从源代码构建了 boost 1.54 和 libtorrent-rasterbar(最新(,没有问题。此外,通过MacPorts,我获得了pkg-config,以获得libtorrent-rasterbar库的正确标志。从我的构建设置中,pkgconfig 库和 cflags 的输出是:

      -DTORRENT_USE_OPENSSL -DWITH_SHIPPED_GEOIP_H 
-DBOOST_ASIO_HASH_MAP_BUCKETS=1021 
    -DBOOST_EXCEPTION_DISABLE -DBOOST_ASIO_ENABLE_CANCELIO 
    -DBOOST_ASIO_DYN_LINK -DTORRENT_LINKING_SHARED -I/usr/local/include 
    -I/usr/local/include/libtorrent 
    -L/usr/local/lib -ltorrent-rasterbar 

当然,我将这些参数添加到 Xcode "链接器标志"和"C/C++ 标志"设置中。

不幸的是,我无法正确链接我调用的函数。这是我在testclass.cpp文件中编写的示例类:

#include "libtorrent/entry.hpp"
#include "libtorrent/bencode.hpp"
#include "libtorrent/torrent_info.hpp"
#include "libtorrent/file.hpp"
#include "libtorrent/storage.hpp"
#include "libtorrent/hasher.hpp"
#include "libtorrent/create_torrent.hpp"
void testclass::addFilesFromPath(const char* path)
{
    libtorrent::file_storage fs;
    libtorrent::add_files(fs, path);
}

已尝试从 createpackage.mm 文件调用:

testclass* pPackage = new testclass();
testclass->addFilesFromPath([_sessionDir UTF8String]);

链接器找不到符号,输出为:

建筑的未定义符号x86_64:
"libtorrent::p arent_path(std::__1::basic_string, std::__1::allocator> const&(", 参考自: libtorrent::add_files(libtorrent::file_storage&, std::__1::basic_string, std::__1::分配器> const&, unsigned int( in 创建包.o
"libtorrent::d etail::add_files_impl(libtorrent::file_storage&, std::__1::basic_string, std::__1:::allocator> const&, std::__1:::basic_string, std::__1:::allocator> const&, boost::function, std::__1::分配器>(>, 无符号 int(",引用自: libtorrent::add_files(libtorrent::file_storage&, std::__1::basic_string, std::__1::分配器> const&, unsigned int( in 创建包.o
"libtorrent::complete(std::__1::basic_string, std::__1::allocator> const&(", 参考自: libtorrent::add_files(libtorrent::file_storage&, std::__1::basic_string, std::__1::分配器> const&, unsigned int( in 创建包.o
"libtorrent::filename(std::__1::basic_string, std::__1::allocator> const&(", 参考自: libtorrent::add_files(libtorrent::file_storage&, std::__1::basic_string, std::__1::分配器> const&, unsigned int( in createpackage.o ld:找不到用于体系结构x86_64的符号 clang:错误:链接器命令失败,退出代码为 1(使用 -v 查看 调用(

我很不解。 已检查 libtorrent-raster 条形图架构是否x86_64。此外,提升是可以构建的。我是这种C++/Objetive-C代码混合方法的新手。

谢谢。

编辑 1:

我采用了最小的样本。制作了以下 CPP 文件:

#include "libtorrent/file.hpp"
#include "libtorrent/storage.hpp"
#include "libtorrent/create_torrent.hpp"
int main()
{
    libtorrent::file_storage fs;
    libtorrent::add_files(fs, ".");
}

在命令行中,尝试:

c++ test.cpp $(pkg-config /usr/local/lib/pkgconfig/libtorrent-rasterbar.pc --cflags --libs) -lboost_system

生成成功。所以我想知道如何将所有 pkg 配置数据放入 OSX 中的正确目标配置中。

终于解决了。

让我们检查符号,比较生成的对象文件和 libtorrent 库中包含的符号。

nm createpackage.o|grep 'add_files'
                 U __ZN10libtorrent6detail14add_files_implERNS_12file_storageERKNSt3__112basic_stringIcNS3_11char_traitsIcEENS3_9allocatorIcEEEESB_N5boost8functionIFbS9_EEEj
00000000000002a0 S __ZN10libtorrent9add_filesERNS_12file_storageERKNSt3__112basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEEj
00000000000018e0 S __ZN10libtorrent9add_filesERNS_12file_storageERKNSt3__112basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEEj.eh

儗:

$ nm libtorrent-rasterbar.a | grep 'add_files'
00000000000002f0 T __ZN10libtorrent6detail14add_files_implERNS_12file_storageERKSsS4_N5boost8functionIFbSsEEEj
0000000000006e68 S __ZN10libtorrent6detail14add_files_implERNS_12file_storageERKSsS4_N5boost8functionIFbSsEEEj.eh

许多人可以想象看到该输出的不同之处在于,我正在为我的 .mm 文件使用 LLVM 标准C++库,而 libtorrent 是使用 GCC Stdlib 编译的,这就是不同符号引用char_traits、basic_string等的原因。

因此,将标准C++库> XCode 构建设置更改为 libstdc++ 解决了这个问题。