首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从Objective链接C++库(libtorrent)

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

Stack Overflow用户
提问于 2013-09-26 06:18:07
回答 1查看 1.5K关注 0票数 2

我试图使用Xcode 5.0 Objective项目中的libtorrent库,但没有成功。

我已经从使用LLVM 5.0的源代码中构建了boost 1.54和libtorrent-rasterbar (最新版本),没有问题。此外,通过MacPorts,我获得了pkg,以便为libtorrent-rasterbar库获得适当的c173。在我的构建设置中,pkgconfig库和c标志的输出如下:

代码语言:javascript
复制
      -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 "Linker“和"C/C++ Flag”设置中。

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

代码语言:javascript
复制
#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文件中调用:

代码语言:javascript
复制
testclass* pPackage = new testclass();
testclass->addFilesFromPath([_sessionDir UTF8String]);

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

架构x86_64的未定义符号: "libtorrent::parent_path(std::__1::basic_string,std::__1::allocator > const&)",引用于: createpackage.o中的libtorrent::add_files(libtorrent::file_storage&,std::__1::basic_string、std::__1::allocator > const&,未签名的int)。 "libtorrent::detail::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::allocator >)>,无符号int)",引用自: libtorrent::add_files(libtorrent::file_storage&,std::__1::basic_string,std::__1::allocator > const&,unsigned )在createpackage.o中 "libtorrent::complete(std::__1::basic_string,std::__1::allocator > const&)",引用自: createpackage.o中的std::__1::allocator std::__1::basic_string,std::__1::allocator> const&,unsigned int) "libtorrent::filename(std::__1::basic_string,std::__1::allocator > const&)",引用自:createpackage.ld中的std::__1::allocator std::__1::basic_string、std::__1::allocator> const&、unsigned。o: x86_64 clang: std::__1::allocator:x86_64 x86_64: error: linker命令失败,退出代码1(使用-v查看调用)

我很困惑。检查了libtorrent-光栅条体系结构是x86_64。另外,boost也是可以的。我是C++ / Objetive-C代码混合方法的新手。

谢谢。

编辑1:

我用了一个极小的样本。创建了以下CPP文件:

代码语言:javascript
复制
#include "libtorrent/file.hpp"
#include "libtorrent/storage.hpp"
#include "libtorrent/create_torrent.hpp"

int main()
{
    libtorrent::file_storage fs;
    libtorrent::add_files(fs, ".");
}

在命令行,尝试:

代码语言:javascript
复制
c++ test.cpp $(pkg-config /usr/local/lib/pkgconfig/libtorrent-rasterbar.pc --cflags --libs) -lboost_system

建造是成功的。因此,我想知道如何将所有这些pkg-config数据放入OSX中适当的目标配置中。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-10-01 21:47:15

最后解决了问题。

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

代码语言:javascript
复制
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

与以下比较:

代码语言:javascript
复制
$ nm libtorrent-rasterbar.a | grep 'add_files'
00000000000002f0 T __ZN10libtorrent6detail14add_files_implERNS_12file_storageERKSsS4_N5boost8functionIFbSsEEEj
0000000000006e68 S __ZN10libtorrent6detail14add_files_implERNS_12file_storageERKSsS4_N5boost8functionIFbSsEEEj.eh

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

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

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19020724

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档