Theos引用三方静态库的问题

最近在捣腾将FFmpeg集成到ios程序上实现编解码一些功能,发现THEOS引用编译好的库出现了一些问题,具体问题是这样。
我已经把FFmpeg编译了armv7、arm64、x86等版本,使用lipo查看对应的.a文件也都能看到支持的架构,如下:
Architectures in the fat file: FFmpeg-iOS/lib/libavcodec.a are: armv7 i386 x86_64 arm64
Architectures in the fat file: FFmpeg-iOS/lib/libavdevice.a are: armv7 i386 x86_64 arm64
Architectures in the fat file: FFmpeg-iOS/lib/libavfilter.a are: armv7 i386 x86_64 arm64
Architectures in the fat file: FFmpeg-iOS/lib/libavformat.a are: armv7 i386 x86_64 arm64
Architectures in the fat file: FFmpeg-iOS/lib/libavutil.a are: armv7 i386 x86_64 arm64
Architectures in the fat file: FFmpeg-iOS/lib/libswresample.a are: armv7 i386 x86_64 arm64
Architectures in the fat file: FFmpeg-iOS/lib/libswscale.a are: armv7 i386 x86_64 arm64

而且我将他们和头文件一起放入xcode设置好相应的引入可以在项目中正常使用,但是我当我尝试在theos里面进行集成时却出现了一些问题,我是这样做的:
1、将编译好的头文件放入THEOS的include目录下(因为头文件中间有些有包含关系,放入项目对应目录更改较为麻烦)
2、将.a文件放入THEOS的lib目录下
3、编写makefiles文件,文件如下:

#makefiles
ARCHS = arm64
TARGET = iphone:latest:8.0 #此句加入与否不影响编译结果
include $(THEOS)/makefiles/common.mk

TOOL_NAME = consoleapp
consoleapp_FILES = main.mm
consoleapp_FRAMEWORKS =UIKit AVFoundation CoreImage OpenGLES VideoToolbox CoreGraphics CoreMedia AudioToolbox
consoleapp_PRIVATE_FRAMEWORK = IOSurface IOKit

consoleapp_LDFLAGS+=libs/libbz2.1.0.tbd
consoleapp_LDFLAGS+=libs/libiconv.2.tbd
consoleapp_LDFLAGS+=libs/libz.1.tbd
consoleapp_LDFLAGS+=-lavcodec
consoleapp_LDFLAGS+=-lavfilter
consoleapp_LDFLAGS+=-lavformat
consoleapp_LDFLAGS+=-lavutil
consoleapp_LDFLAGS+=-lswresample
consoleapp_LDFLAGS+=-lswscale
consoleapp_LDFLAGS+=-lavdevice

include $(THEOS_MAKE_PATH)/tool.mk

当我进行编译时,编译链接阶段发生如下错误:

Making all for tool consoleapp…
==> Compiling main.mm (arm64)…
==> Linking tool consoleapp (arm64)…
Undefined symbols for architecture arm64:
“_clock_gettime”, referenced from:
_av_gettime_relative in libavutil.a(time.o)
_av_gettime_relative_is_monotonic in libavutil.a(time.o)
ld: symbol(s) not found for architecture arm64

搜索相关资料在linux下面加入librt即编译库加上-lrt即可,但是我在osx全路径下查找librt并没有相对应的库,问题的根本好像是苹果系列的系统在实现clock_gettime时有所出入(事实上我之前开过一贴是关于libevent引用出现同样的问题,当时不太清楚发帖的说明而被锁帖囧,libevent并不是我想要的必须使用的库,这个问题没解决也就绕过了),然而比较奇葩的是,我把它做为非越狱应用在xcode8上编译却能顺利通过并在arm64(iphone6)的机器上机器上跑起来,附上xcode的库依赖


请问坛子里有没有大神碰到过类似问题并且顺利解决,或者有其他解决思路呢?(我都想直接改FFmpeg的源码调用_clock_gettime的地方然后重新编译了,但是怕改动引起连锁反应暂时还没动手)

可以在makefile里定义个clockgettime宏吧

根据错误信息,我把FFmpeg的相关实现抠了出来,定位到:
int64_t av_gettime_relative(void)
{

#if HAVE_CLOCK_GETTIME && defined(CLOCK_MONOTONIC)
#ifdef APPLE
if (clock_gettime)
#endif
{
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return (int64_t)ts.tv_sec * 1000000 + ts.tv_nsec / 1000;
}
#endif
return av_gettime() + 42 * 60 * 60 * INT64_C(1000000);
}

int av_gettime_relative_is_monotonic(void)
{

#if HAVE_CLOCK_GETTIME && defined(CLOCK_MONOTONIC)
#ifdef APPLE
if (!clock_gettime)
return 0;
#endif
return 1;
#else
return 0;
#endif
}

在工程里面ffmpeg是作为.a静态库引用的,这个时候定义宏会起作用吗?我现在就是不太理解为什么xcode能过编而theos却不能过编呢

很明显问题出在:
av_gettime_relative方法中的if (clock_gettime)
av_gettime_relative_is_monotonic方法中的if (!clock_gettime)
这两个判断,按照楼上大神的说法,在makefiles里面加入
consoleapp_CFLAGS += -D clock_gettime
错误依旧:sob:

不对啊, 他代码都有兼容iOS/OSX的样子了

if HAVE_CLOCK_GETTIME && defined(CLOCK_MONOTONIC)
ifdef APPLE

你自己看看 HAVE_CLOCK_GETTIME 和 APPLE 分别是什么

是啊,我也看到了平台兼容性代码
#define HAVE_CLOCK_GETTIME 1
APPLE是他的makefile宏,用来区分平台做条件编译的

因为在xcode里面可以使用,所以我的思路是源码理论上不需要改动按道理可以集成到theos进行编译,但是现在情况是xcode与theos依赖关系都一致了,但是编译却报错,是不是有些xcode默认依赖而theos没有呢?这问题折腾了一些时间了甚是无助啊

我意思是要是编译了APPLE里的代码, 怎么还会调用clockgettime

这也是我疑惑的地方,表现上xcode集成和theos集成不一样啊

所以看一下APPLE和HAVE_CLOCK_GETTIME的定义大概就真相大白了吧

准备着手这样做,理想状态下是不到万不得已不改三方代码啊,毕竟ffmpeg是个成熟的编码库,而且集成到xcode也可以顺利使用啊,还是多谢楼上提供的帮助:pray: