Home > Net >  Clang compiling for iOS (arm64) with -shared LDFLAG - Exec format error
Clang compiling for iOS (arm64) with -shared LDFLAG - Exec format error

Time:10-04

Newbie alert here, sorry in advance if this question duplicates (didn't find the answer elsewhere)!

I run into problems with simple hello binary for iOS (arm64) build on macOS machine (x86_64).

The problem is that when I add LDFLAGS with shared framework (i.e. "-shared -framework CoreMedia" or other framework) to build my binary, it compiles fine but when it executes on device I get Exec format error:

iPhone:/tmp root# ./hello 
-sh: ./hello: cannot execute binary file: Exec format error

Build without -shared flag it runs as intended:

iPhone:/tmp root# ./hello 
Hello

Can someone please explain to me why this flag causes exec error on binary? Is it related to different platform which I'm building on than the targeted device?

Should I build on arm64 platform to get -shared flag working fine?

Just in case, build script is:

export CLANG_BIN=`xcrun --sdk iphoneos --find clang`
export CLANGXX_BIN=`xcrun --sdk iphoneos --find clang  `
export SDK=`xcrun --sdk iphoneos --show-sdk-path`

export CFLAGS="-fno-builtin -fno-stack-protector -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk/ -fno-stack-protector -Wno-builtin-requires-header -fno-stack-check"
#export LDFLAGS="-shared -framework CoreMedia" # <- exec error when this added to compile
export LDFLAGS="-framework CoreMedia" # <- with just this, bin executes fine

export CXX="$CLANGXX_BIN $CFLAGS -isysroot $SDK"

$CXX -arch arm64 -o hello hello.c $LDFLAGS -Wall -Wconversion

CodePudding user response:

-shared means you're building a shared library.
You cannot run a shared library.

  • Related