快速链接并使用C/C ++库进行构建
我以前没有任何有关Swift的经验.
I don't have any prior experience with swift.
我有一个静态库libseriallib.a
,它是用C ++编写的,但是通过使用extern C
具有C包装器接口.
I have a static library libseriallib.a
which was written in C++ but has a C wrapper interface by using extern C
.
我想将此库链接到一个快速的iOS应用程序中.我正在从头开始创建此应用程序.
I want to link this library into a swift iOS application. I am creating this application from scratch.
libseriallib.a
依赖于三个库.它们是:libz.a
,'libcrypto.a ,
libssh.a`.
There are three libraries that libseriallib.a
depends on. These are: libz.a
, 'libcrypto.a,
libssh.a`.
我遵循了此处(使用模块)并尝试构建项目.但是,我对如何链接这三个依赖关系并使其成功构建感到困惑.现在,我只在XCode Build Phases->Link Binary With Libraries
部分中添加了libseriallib.a
.
I followed the second method mentioned here (Using module) and tried to build the project. However, I am confused how to link the three dependencies and get it building successfully. Right now I only added libseriallib.a
to XCode Build Phases->Link Binary With Libraries
section.
我现在遇到的错误如下:
The errors I have right now look like this:
Apple Mach-O Linker Warning Group
ld: warning: URGENT: building for iOS simulator, but linking in object file (/Users/duminda/dev/swiftlibtest/libseriallib.a(seriallib.cpp.o)) built for OSX. Note: This will be an error in the future.
ld: warning: object file (/Users/duminda/dev/swiftlibtest/libseriallib.a(seriallib.cpp.o)) was built for newer OSX version (10.11) than being linked (10.0)
ld: warning: URGENT: building for iOS simulator, but linking in object file (/Users/duminda/dev/swiftlibtest/libseriallib.a(mem_buffer.cpp.o)) built for OSX. Note: This will be an error in the future.
ld: warning: object file (/Users/duminda/dev/swiftlibtest/libseriallib.a(mem_buffer.cpp.o)) was built for newer OSX version (10.11) than being linked (10.0)
ld: warning: URGENT: building for iOS simulator, but linking in object file (/Users/duminda/dev/swiftlibtest/libseriallib.a(buffers.cpp.o)) built for OSX. Note: This will be an error in the future.
ld: warning: object file (/Users/duminda/dev/swiftlibtest/libseriallib.a(buffers.cpp.o)) was built for newer OSX version (10.11) than being linked (10.0)
ld: warning: URGENT: building for iOS simulator, but linking in object file (/Users/duminda/dev/swiftlibtest/libseriallib.a(packet_reader2.cpp.o)) built for OSX. Note: This will be an error in the future.
ld: warning: object file (/Users/duminda/dev/swiftlibtest/libseriallib.a(packet_reader2.cpp.o)) was built for newer OSX version (10.11) than being linked (10.0)
Apple Mach-O Linker Error Group
"std::runtime_error::what() const", referenced from:
"std::__1::__basic_string_common<true>::__throw_length_error() const", referenced from:
"std::__1::locale::use_facet(std::__1::locale::id&) const", referenced from:
"std::__1::ios_base::getloc() const", referenced from:
"std::runtime_error::runtime_error(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)", referenced from:
现在,看来我也必须链接libstd++
.
Now, it looks like I have to link libstd++
also.
是否有全面的资源来介绍如何将C/C ++库与快速的iOS应用程序链接?
Is there any comprehensive resource to how to link a C/C++ library with a swift iOS app?
任何帮助将不胜感激.
在这种情况下,您不必使用模块映射.您可以在Build Phases->Link Binary With Libraries
部分或Build Settings -> Other Linker Flags
中添加所有4个静态库,在这种情况下,您需要将它们指定为-lseriallib -lcrypto -lz -lssh
.无论哪种情况,您都需要按照ikliashchou
的建议在Build Settings -> Library Search Path
中添加它们的路径.
You don't have to use module maps in this case. You can add all 4 static libraries in the Build Phases->Link Binary With Libraries
section or in the Build Settings -> Other Linker Flags
, in which case you need to specify them as -lseriallib -lcrypto -lz -lssh
. In either case you also need to add their paths in Build Settings -> Library Search Path
, as suggested by ikliashchou
.
关于C ++符号的错误,可以通过在Other Linker Flags
上添加-lc++
来解决.这就是说包装器是一个单独的库;如果包装程序代码是项目的一部分,则不应出现这些错误.
As for the errors about C++ symbols, those can be resolved by adding -lc++
to Other Linker Flags
. That's if the wrapper is a separate library; you should not be getting those errors if the wrapper code is part of the project.
有关体系结构的警告不会阻止该应用程序在模拟器中运行(目前).但是,一旦您尝试在设备上运行它,它就不会构建,因为静态库不是为iOS设备的体系结构构建的.
The warnings about architectures won't prevent the app from running in the simulator (for now). However, as soon as you try running it on a device, it won't build because the static libs are not built for an iOS device's architecture.
在这些静态库中为设备构建代码本身就是一个主题,并且可能很棘手.需要考虑的几件事:
Building the code in those static libs for a device is a topic in its own right and may be tricky. A few things to consider:
- 从库源创建Xcode静态库项目.这 每个库都需要完成.
- 将所有库源包含到您的应用程序项目中(在 在这种情况下,这种方法可能太乱了.
- 这些库可能还具有其他不易依赖的依赖项 适用于iOS设备.
- 库代码可能已经移植到了iOS,因此Google为 它.
- Create an Xcode static library project from the library source. This would need to be done for each library.
- Include all the library sources into your application project (in this case this approach would probably be too messy).
- The libraries may have yet other dependencies that are not readily available for iOS devices.
- The library code may have already been ported to iOS, so Google for it.
以下一些链接可能会有所帮助: -如何使用Xcode编译库一个生成文件? -可以建立一个静态的iOS的库而不使用Xcode IDE? -编译用于iOS项目的外部C ++库
Here are some links that might be helpful: - How do I compile a library in Xcode using a makefile? - Can I build a static library for iOS without using the Xcode IDE? - Compiling external C++ library for use with iOS project