5

In Xcode 4.3.2, building an iPad app, which includes libavutil.a from the ffmpeg distribution, it builds and runs correctly but when I try to run unit tests (Cmd-U) I get the following linker error:

ld: illegal text-relocation to cstring in /myPath/libavutil.a(imgutils.o) from _av_image_check_size in /myPath/libavutil.a(imgutils.o) for architecture armv7 clang: error: linker command failed with exit code 1 (use -v to see invocation)

This is on the device (We can't run it in the simulator currently because we don't have fat binaries for all the libraries we are including).

Why will it build and run correctly normally but not link when running unit tests?

Mualig
  • 1,444
  • 1
  • 19
  • 42
user1499742
  • 161
  • 1
  • 5
  • Looks like the answer to this is the same as the answer to this question: [link] http://stackoverflow.com/questions/6650178/illegal-text-reloc-to-non-lazy-ptr-error-while-building-in-xcode-4-with-libav-l – user1499742 Jul 05 '12 at 23:01

4 Answers4

11

The answer, from here.

is to add:

-read_only_relocs suppress

to the linker flags.

The other link explains why. The solution was originally found here.

Community
  • 1
  • 1
user1499742
  • 161
  • 1
  • 5
  • 8
    This no longer works. That flag is unsupported on x86_64 and is also incompatible with BYTECODE=YES. – dgatwood Mar 30 '17 at 18:07
  • This still works. I just used this flag with Xcode 9.4.1 to fix an app that includes a library which caused this error when compiling for the iPhone 5 / iOS 10 simulator. – Dorian Roy Jul 05 '18 at 10:11
4

Just to be specific, the line that specifically worked to fix a similar linking error with ffmpeg for 32bit OSX link using xcode/c++11, was to add -Wl,-read_only_relocs,suppress to the link line. Variations of this didn't work.

Hariprasad
  • 3,556
  • 2
  • 24
  • 40
  • With the flag, the lib can be compiled success, but will crash on 32 bit device when using the lib:``` dyld: REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB has segment 0 which is not a writable segment (__TEXT) in /private/var/containers/Bundle/Application/25DBDD6A-F5C1-4DD3-B432-F9FAB43A3673/UPLiveSDKDemo.app/Frameworks/SomeLibDll.framework/SomeLibDll `` – rotoava May 05 '17 at 07:22
  • @rotoava Did you find any solution? I am also facing the same issue with 32 bit device. – Pankaj Vavadiya Nov 16 '18 at 07:25
  • @PankajVavadiya Sorry, this has passed too along time, and I forget the final solution. I didn't come back to write the result, maybe I did not find a solution. The 32-bit machine only has the iPhone 5, and our SDK ignores the simulator problem. At the same time, we only provide static libraries when the dynamic libraries have problems, and so on. – rotoava Nov 17 '18 at 08:44
2

I solved this linker error by enabling Position-Independent Code in the compiler and linker settings in XCode.

Richmar1
  • 191
  • 10
1

For those who have read answers above but it did not help, check your "Product -> Scheme -> Edit Scheme". It should be "debug" for simulator and for device it does not matter. Otherwise you will get strange bugs like it cannot see some parts of your project.

Dake
  • 23
  • 8