前提:
我正在尝试构建一个C的静态库,通过一个Objective-c桥接头从Swift项目中引用.我构建了静态库,将其包含在我的项目中,Sourcekit识别我想从静态库中调用的函数,但我仍然遇到错误(详见下文).
我按照Wenderlich教程在XCode中创建静态库.当我生成通用二进制文件时,运行lipo命令:
lipo -info libMyStaticLib.a
Run Code Online (Sandbox Code Playgroud)
我明白了:
Architectures in the fat file: libMyStaticLib.a are: armv7 armv7s i386 arm64
Run Code Online (Sandbox Code Playgroud)
我发现它没有构建x86_64架构,因为当我在需要库的项目中链接它时,我得到了错误:
ld: warning: ignoring file
/Users/jamesmart/SourcetreeProjects/MyProject/libMyStaticLib.a, missing
required architecture x86_64 in file
/Users/jamesmart/SourcetreeProjects/MyProject/libMyStaticLib.a (4 slices)
Undefined symbols for architecture x86_64:
"_helloWorld", referenced from:
__TFC7MyProject16BridgePlayground12sendIntToGetfS0_FT_T_ in BridgePlayground.o
ld: symbol(s) not found for architecture x86_64
Run Code Online (Sandbox Code Playgroud)
我见过类似的StackOverflow问题:
XCode 6.1在extern库的文件中缺少所需的体系结构X86_64
我已经尝试了列出的解决方案,但似乎没有什么能够构建那个darn x86_64架构.我也尝试将静态库构建到许多目标设备:
只是试图链接图片,但我没有足够的声誉,抱歉...
但我已经尝试过针对模拟器和iOS设备,两种选择都没有解决这个问题.
在我的静态库构建设置中,"仅构建活动体系结构"设置为"否",并且在我的运行脚本构建阶段中作为参数传递给xcodebuild函数:
xcodebuild -target SegmentationLib ONLY_ACTIVE_ARCH=NO -configuration
${CONFIGURATION} -sdk iphoneos BUILD_DIR="${BUILD_DIR}"
BUILD_ROOT="${BUILD_ROOT}"
xcodebuild -target SegmentationLib -configuration ${CONFIGURATION} -sdk
iphonesimulator …Run Code Online (Sandbox Code Playgroud) 我使用 NTTP(非类型模板参数)lambdastring_view在编译时将 a 存储到类型中:
template<auto getStrLambda>
struct MyType {
static constexpr std::string_view myString{getStrLambda()};
};
int main() {
using TypeWithString = MyType<[]{return "Hello world";}>;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这有效,并实现了我的主要目的。
我现在的问题是,为了使其更易于使用,我如何编写一个包装函数来为我创建 lambda?
我在想这样的事情:
// This helper function generates the lambda, rather than writing the lambda inline
consteval auto str(auto&& s) {
return [s]() consteval {
return s;
};
};
template<auto getStrLambda>
struct MyType {
static constexpr std::string_view myString{getStrLambda()};
};
int main() {
using TypeWithString = MyType<str("Hello world")>; …Run Code Online (Sandbox Code Playgroud)