be used as 造句 as,use for,use with

Compiling external C++ library for use with iOS project - Stack Overflow
to customize your list.
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.
J it only takes a minute:
Join the Stack Overflow community to:
Ask programming questions
Answer and help your peers
Get recognized for your expertise
I'm completely new to using C++ libraries, so appreciate this might be a bit specific for my case (let me know and I can provide more details).
I have an external C++ library that I'm trying to use with an iOS project. The library follows a configure, make, make build pattern to output a .a library file. When I try and add this library file to Xcode, I get the following error:
ignoring file
/Users/Developer/iOS/TestProj/libpresage.a, file was
built for archive which is not the architecture being linked (i386):
/Users/Developer/iOS/TestProj/libpresage.a
Based on , I've tried turning Build Active Architecture Only to NO, and I get the same error. This makes me suspect that I've compiled the library for the incorrect architecture.
Running lipo -info on the .a file gives:
input file libpresage.a is not a fat file Non-fat file: libpresage.a
is architecture: x86_64
Given that this isn't armv7s, armv7, or arm64, I try and compile the C++ library again with the following parameters:
./configure CC="gcc -arch armv7s" \
CXX="g++ -arch armv7s" \
CPP="gcc -E" CXXCPP="g++ -E"
Error in compiling, I get:
ld: library not found for -lcrt1.3.1.o
clang: error: linker command failed with exit code 1 (use -v to see invocation)
./configure CC="gcc -arch arm64" \
CXX="g++ -arch arm64" \
CPP="gcc -E" CXXCPP="g++ -E"
Error in compiling, I get:
ld: warning: ld: warning: ignoring file
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk/usr/lib/libSystem.dylib,
missing required architecture arm64 in file
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk/usr/lib/libSystem.dylib
(2 slices)ignoring file
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk/usr/lib/libstdc++.dylib,
missing required architecture arm64 in file
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk/usr/lib/libstdc++.dylib
(2 slices)
ld: dynamic main executables must link with libSystem.dylib for
architecture arm64 clang: error: linker command failed with exit code
1 (use -v to see invocation)
Is there something obvious that I'm missing?
Thanks for the replies, so I've managed to get the library into Xcode as a custom build target, pointing the 'make' command to the libraries MakeFile. This build fine.
My steps from here:
Add a dependency from my Objective C iOS app target to the custom build target.
Reference the library and make an Objective C++ wrapper.
This seems fine until I need to call the external C++ library, then I get the error when compiling:
Undefined symbols for architecture armv7:
"Presage::Presage(PresageCallback*)", referenced from:
-[PresageBridge init] in PresageBridge.o
"Presage::~Presage()", referenced from:
-[PresageBridge init] in PresageBridge.o
ld: symbol(s) not found for architecture armv7
clang: error: linker command failed with exit code 1 (use -v to see invocation)
My objective C++ wrapper (linking the external C++ library header presage.h):
#import "PresageBridge.h"
#include "presage.h"
@implementation PresageBridge
- (instancetype)init
if(self = [super init])
Presage hello(&callback);
Based on the code above it doesn't seem like I'm missing the header, and what's interesting is that I've also tried creating an instance of other classes in the external library and they seem to be working, which suggests that Xcode can't link presage.h properly for some reason.
So I've used many a 3rd party C++ library in my iOS projects. There are different strategies people use for this. As some have already cited, you can include the code within the project directly, build the static lib with Xcode, or build it command line. In the case of cross platform C++ libs which use the GNU configure and build system, I prefer command line. You only need to build it once and you only have to revisit it if you need to update the version or add a new architecture slice.
The generalized approach you want is:
-Figure out the right configure arguments to use to build each slice. Typically, you only need to focus on getting one of the arm as well as i386 working. The rest are easy one you have this done. In some cases, you actually need to modify the configure file to add the host or make some other adjustments.
-Once you can build all slices, you want to run lipo to build a fat binary.
The best way then to deal with this is create a build script which will do all the work for you. This way, it's easier to redo. More importantly, you can reuse the script or permute it to build other external libs.
There are many ways you can build the script. Here is one. I happen to have several variations of this type of script. This script was used to build cURL. It more or less worked for presage with very little mod (ie. change curl to presage). Note I didn't test it in Xcode (ie. linking it and running it). I did find that I had to disable sqlite, else it built tool items which don't build right. If you need it, you can figure that part out.
There are many ways you could make it more slick. For example using an array to store all the architectures. This is just brute force.
The key points of the script are:
-Getting the latest SDK
-Building each slice
-Then running lipo
Note that it should work out of the box, however, YMMV. Be prepared to have to debug it if necessary. For example, I haven't confirmed the host type, but generally that is what I've always used. You want to put this at the directory for presage (same directory where configure). When it is done, all architectures are in the output directory. The universal lib is in the presage directory.
Also remember it is your responsibility to properly link in the universal lib as well as have the header files search path defined properly.
#!/bin/bash
PLATFORMPATH="/Applications/Xcode.app/Contents/Developer/Platforms"
TOOLSPATH="/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin"
export IPHONEOS_DEPLOYMENT_TARGET="8.0"
findLatestSDKVersion()
sdks=`ls $PLATFORMPATH/$1.platform/Developer/SDKs`
for sdk in $sdks
arr[${#arr[@]}]=$sdk
# Last item will be the current SDK, since it is alpha ordered
count=${#arr[@]}
if [ $count -gt 0 ]; then
sdk=${arr[$count-1]:${#1}}
num=`expr ${#sdk}-4`
SDKVERSION=${sdk:0:$num}
SDKVERSION="8.0"
hosttarget=$1
platform=$2
if [[ $hosttarget == "x86_64" ]]; then
hostarget="i386"
elif [[ $hosttarget == "arm64" ]]; then
hosttarget="arm"
export CC="$(xcrun -sdk iphoneos -find clang)"
export CPP="$CC -E"
export CFLAGS="-arch ${target} -isysroot $PLATFORMPATH/$platform.platform/Developer/SDKs/$platform$SDKVERSION.sdk -miphoneos-version-min=$SDKVERSION"
export AR=$(xcrun -sdk iphoneos -find ar)
export RANLIB=$(xcrun -sdk iphoneos -find ranlib)
export CPPFLAGS="-arch ${target}
-isysroot $PLATFORMPATH/$platform.platform/Developer/SDKs/$platform$SDKVERSION.sdk -miphoneos-version-min=$SDKVERSION"
export LDFLAGS="-arch ${target} -isysroot $PLATFORMPATH/$platform.platform/Developer/SDKs/$platform$SDKVERSION.sdk"
mkdir -p $pwd/output/$target
./configure --prefix="$pwd/output/$target" --disable-shared --disable-sqlite --host=$hosttarget-apple-darwin
make clean
make install
findLatestSDKVersion iPhoneOS
buildit armv7 iPhoneOS
buildit armv7s iPhoneOS
buildit arm64 iPhoneOS
buildit i386 iPhoneSimulator
buildit x86_64 iPhoneSimulator
LIPO=$(xcrun -sdk iphoneos -find lipo)
$LIPO -create $pwd/output/armv7/lib/libpresage.a
$pwd/output/armv7s/lib/libpresage.a $pwd/output/arm64/lib/libpresage.a $pwd/output/x86_64/lib/libpresage.a $pwd/output/i386/lib/libpresage.a -output libpresage.a
Considering that you are new with C++ libraries, I guess you will need to do a bit more research.
However, I will try to outline some steps things you need to take into consideration :
you need to make sure you compile for the same architecture both the static library (.a) and the project
from your error , you need to compile your static library for i386
OR change your project to x86_64 ( the difference between these architectures is a bit more complex, but for now let's say that i386 means desktop 32 bit while x86_64 means desktop 64 bit)
arm architectures are for iPhone , not for your MacOS (that's why it fails to find libraries with arm architecture inside the MacOSX folder) !
There are multiple ways to approach these issues .
For the first one I would suggest to include into your workspace the static library, and add it as dependency to your build target . For this you need to understand XCode builds.
I'm guessing that you actually are trying to make a phone application, so for the 3rd option you need to configure your g++ build to look into the iPhoneSDK from XCode when linking arm targets (look after iPhoneOS.platform) for this.
Making an arm build will only work on iPhones . If you want it to work on simulator , you will need to link your static library to libraries inside the iPhoneSimulator.platform.
If you want your static library to work for both iPhones and iPhone simulator,
you will need to make a fat lib (basically a library containing symbols for both platforms)
If you are missing these platforms, you can download them from XCode (but I believe they are there)
As you can see, things are going to get more and more complex along the way, so I strongly recommend to use XCode for compiling the static library (it is still doable with g++ thou).
I believe the following concepts you would be useful to research upon :
arm, x86 , x86_64
static library
static linkage
fat lib (universal library)
XCode workspace with multiple projects
Hope this helps :).
C++ Works on iOS, you can just add it in your project. Or if you really want to have your dynamic library, you can compile it with Xcode and specify your target architecture.
Set your architecture back to default then try the following.
1. In Build Phases->Link Binary With Libraries add libz.dylib and libxml2.dylib libraries to your project.
2. In BuildSettings->Search Paths set Always Search User Paths to Yes, and under Framework Search Paths add the correct path of your framework. Use terminal to get the right path of your framework.
3. Try setting your "Compiler Source As" to C++ or use hit and trial and check with all options. Complier Source As is also under BuildSettings. Use cmd+f to search it.
Try these and let me know, also tell me about the framework or sdk that you are trying to use in your project.
Your Answer
Sign up or
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Post as a guest
By posting your answer, you agree to the
Not the answer you're looking for?
Browse other questions tagged
The week's top questions and answers
Important community announcements
Questions that need answers
By subscribing, you agree to the
Stack Overflow works best with JavaScript enabled}

我要回帖

更多关于 use to used to 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信