XCode中的条件链接静态库与环境变量 [英] Conditional link static library in XCode with environment variable

查看:510
本文介绍了XCode中的条件链接静态库与环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将静态库( .a 文件)链接到我的代码中,但有一些限制

I want to link a static library (.a file) into my code with some restrictions


  • 条件应该是环境变量而不是构建类型(Debug,Release)或体系结构。

  • 如果未使用静态库(未导入,则不使用代码),然后最终的二进制文件不应该包含对它的任何引用。

代码应如下所示:

#ifdef CRASH_LOGGING
[Crittercism enableWithAppID:@"abc"]
#endif

环境变量的名称应该相似。

And the environment variable should have a similar name.

我玩过使用 OTHER_LINKER_FLAGS = -weak_library ,从目标中删除 .a ,将其设置为可选,但我不能让它工作。要么库没有链接,我得到编译错误,要么部分 .a 属于最终的可执行文件。

I played with OTHER_LINKER_FLAGS = -weak_library, removing the .a from the target, setting it as optional, but I can't get it to work. Either the library is not linked, I get a compile error, or part the .a belongs to the final executable.

我怎样才能实现这个目标?

How can I achieve this?

推荐答案

最后我通过向<$添加更多参数来解决这个问题c $ c> xcodebuild 命令行。

In the end I ended up solving this problem by adding more parameters to the xcodebuild command line.

基本上您需要做的是调整:

Basically what you need to do is to adjust:


  • 标题所在 .h 文件位于

  • .a 所在的位置

  • 告诉链接器您要使用库 -lCrittercism_v4_0_7

  • Where the header .h files are located
  • Where the library .a is located
  • Tell the linker that you want to use the library -lCrittercism_v4_0_7

/usr/bin/xcodebuild -configuration Release clean
"LIBRARY_SEARCH_PATHS=\${LIBRARY_SEARCH_PATHS} \${PROJECT_DIR}/Libraries/CrittercismSDK"
"HEADER_SEARCH_PATHS=\${HEADER_SEARCH_PATHS} \${PROJECT_DIR}/Libraries/CrittercismSDK" 
"OTHER_LDFLAGS=-lCrittercism_v4_0_7"

使用这种方法,您根本不需要将库添加到目标或Xcode。如果最后三个参数未添加到命令行,则库根本不属于最终可执行文件。

With this approach you don't need to add the library to the target or to Xcode at all. If the last three parameters aren't added to the command line, the library won't belong to the final executable at all.

这篇关于XCode中的条件链接静态库与环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆