我正在尝试在一个新的应用程序中使用Mecab,但我无法使该库正常工作。最初,我尝试了以下应该与iOS 6兼容的存储库:
https://github.com/gumob/mecab
我尝试构建通用二进制文件,然后添加库和头文件,以及简单地将整个项目添加为依赖项,但是在两种情况下,我最终都遇到很多错误。所以,我放弃了那个,回到原来的分支:
https://github.com/FLCLjp/iPhone-libmecab
我相信它最初是为iOS 3制作的。但是,如果您对该存储库中包含的示例应用程序进行了以下更改,则该应用程序将在iOS 9.3模拟器和设备上构建并运行:
将应用程序委托(delegate)中的第26行更改为
[self.window addSubview:viewController.view];
至
[self.window setRootViewController:viewController];
将“其他来源”文件夹中的mecab.xcodeproj中找到的LibMecabSample目标和mecab库目标的“build设置-体系结构”选项更改为“标准体系结构(armv7 arm64)”。
由于此版本似乎可以正常工作,因此我尝试将在该存储库中找到的mecab.xcodeproj实现到我的应用中。我基本上只是尝试重新创建它在LibMecabSample应用程序中的实现方式,而且一切似乎都是相同的。我什至在文本程序中对xcode项目文件进行了差异化处理,以确保build设置没有任何差异。有几个区别(ARC,启用模块和用户定义的PREBINDING-无设置),但是更改它们似乎并没有影响,并且在尝试构建时仍然出现以下错误:
CompileC /Users/username/Library/Developer/Xcode/DerivedData/myApp-alstnncsvqtenwfmppcsobrovoao/Build/Intermediates/myApp.build/Debug-iphoneos/myApp.build/Objects-normal/armv7/Mecab.o myApp/Mecab.m normal armv7 objective-c com.apple.compilers.llvm.clang.1_0.compiler
cd /Users/username/Developer/myApp
export LANG=en_US.US-ASCII
export PATH="/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin:/Applications/Xcode.app/Contents/Developer/usr/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin"
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang -x objective-c -arch armv7 -fmessage-length=0 -fdiagnostics-show-note-include-stack -fmacro-backtrace-limit=0 -std=c99 -fobjc-arc -fmodules -fmodules-cache-path=/Users/username/Library/Developer/Xcode/DerivedData/ModuleCache -fmodules-prune-interval=86400 -fmodules-prune-after=345600 -fbuild-session-file=/Users/username/Library/Developer/Xcode/DerivedData/ModuleCache/Session.modulevalidation -fmodules-validate-once-per-build-session -Wnon-modular-include-in-framework-module -Werror=non-modular-include-in-framework-module -Wno-trigraphs -fpascal-strings -O0 -fno-common -Wno-missing-field-initializers -Wno-missing-prototypes -Werror=return-type -Wunreachable-code -Wno-implicit-atomic-properties -Werror=deprecated-objc-isa-usage -Werror=objc-root-class -Wno-arc-repeated-use-of-weak -Wduplicate-method-match -Wno-missing-braces -Wparentheses -Wswitch -Wunused-function -Wno-unused-label -Wno-unused-parameter -Wunused-variable -Wunused-value -Wempty-body -Wconditional-uninitialized -Wno-unknown-pragmas -Wno-shadow -Wno-four-char-constants -Wno-conversion -Wconstant-conversion -Wint-conversion -Wbool-conversion -Wenum-conversion -Wshorten-64-to-32 -Wpointer-sign -Wno-newline-eof -Wno-selector -Wno-strict-selector-match -Wundeclared-selector -Wno-deprecated-implementations -DDEBUG=1 -DOBJC_OLD_DISPATCH_PROTOTYPES=0 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS9.3.sdk -fstrict-aliasing -Wprotocol -Wdeprecated-declarations -miphoneos-version-min=9.3 -g -Wno-sign-conversion -fembed-bitcode-marker -I/Users/username/Library/Developer/Xcode/DerivedData/myApp-alstnncsvqtenwfmppcsobrovoao/Build/Intermediates/myApp.build/Debug-iphoneos/myApp.build/myApp.hmap -I/Users/username/Library/Developer/Xcode/DerivedData/myApp-alstnncsvqtenwfmppcsobrovoao/Build/Products/Debug-iphoneos/include -I/Users/username/Developer/myApp/myApp -I/Users/username/Library/Developer/Xcode/DerivedData/myApp-alstnncsvqtenwfmppcsobrovoao/Build/Intermediates/myApp.build/Debug-iphoneos/myApp.build/DerivedSources/armv7 -I/Users/username/Library/Developer/Xcode/DerivedData/myApp-alstnncsvqtenwfmppcsobrovoao/Build/Intermediates/myApp.build/Debug-iphoneos/myApp.build/DerivedSources -F/Users/username/Library/Developer/Xcode/DerivedData/myApp-alstnncsvqtenwfmppcsobrovoao/Build/Products/Debug-iphoneos -fno-objc-arc -include /Users/username/Library/Developer/Xcode/DerivedData/myApp-alstnncsvqtenwfmppcsobrovoao/Build/Intermediates/PrecompiledHeaders/LibMecabSample_Prefix-djuzfnofhgzpgqhajbqeodymirzc/LibMecabSample_Prefix.pch -MMD -MT dependencies -MF /Users/username/Library/Developer/Xcode/DerivedData/myApp-alstnncsvqtenwfmppcsobrovoao/Build/Intermediates/myApp.build/Debug-iphoneos/myApp.build/Objects-normal/armv7/Mecab.d --serialize-diagnostics /Users/username/Library/Developer/Xcode/DerivedData/myApp-alstnncsvqtenwfmppcsobrovoao/Build/Intermediates/myApp.build/Debug-iphoneos/myApp.build/Objects-normal/armv7/Mecab.dia -c /Users/username/Developer/myApp/myApp/Mecab.m -o /Users/username/Library/Developer/Xcode/DerivedData/myApp-alstnncsvqtenwfmppcsobrovoao/Build/Intermediates/myApp.build/Debug-iphoneos/myApp.build/Objects-normal/armv7/Mecab.o
In file included from /Users/username/Developer/myApp/myApp/Mecab.m:12:
/Users/username/Developer/myApp/myApp/Mecab.h:13:2: error: unknown type name 'mecab_t'
mecab_t *mecab;
^
/Users/username/Developer/myApp/myApp/Mecab.m:29:11: warning: implicit declaration of function 'mecab_new2' is invalid in C99 [-Wimplicit-function-declaration]
mecab = mecab_new2([[@"-d " stringByAppendingString:path] UTF8String]);
^
/Users/username/Developer/myApp/myApp/Mecab.m:32:49: warning: implicit declaration of function 'mecab_strerror' is invalid in C99 [-Wimplicit-function-declaration]
fprintf(stderr, "error in mecab_new2: %s\n", mecab_strerror(NULL));
^
/Users/username/Developer/myApp/myApp/Mecab.m:32:49: warning: format specifies type 'char *' but the argument has type 'int' [-Wformat]
fprintf(stderr, "error in mecab_new2: %s\n", mecab_strerror(NULL));
~~ ^~~~~~~~~~~~~~~~~~~~
%d
/Users/username/Developer/myApp/myApp/Mecab.m:38:8: error: unknown type name 'mecab_node_t'
const mecab_node_t *node;
^
/Users/username/Developer/myApp/myApp/Mecab.m:42:9: warning: implicit declaration of function 'mecab_sparse_tonode2' is invalid in C99 [-Wimplicit-function-declaration]
node = mecab_sparse_tonode2(mecab, buf, l);
^
/Users/username/Developer/myApp/myApp/Mecab.m:65:3: warning: implicit declaration of function 'mecab_destroy' is invalid in C99 [-Wimplicit-function-declaration]
mecab_destroy(mecab);
^
5 warnings and 2 errors generated.
CompileC /Users/username/Library/Developer/Xcode/DerivedData/myApp-alstnncsvqtenwfmppcsobrovoao/Build/Intermediates/myApp.build/Debug-iphoneos/myApp.build/Objects-normal/arm64/Mecab.o myApp/Mecab.m normal arm64 objective-c com.apple.compilers.llvm.clang.1_0.compiler
cd /Users/username/Developer/myApp
export LANG=en_US.US-ASCII
export PATH="/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin:/Applications/Xcode.app/Contents/Developer/usr/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin"
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang -x objective-c -arch arm64 -fmessage-length=0 -fdiagnostics-show-note-include-stack -fmacro-backtrace-limit=0 -std=c99 -fobjc-arc -fmodules -fmodules-cache-path=/Users/username/Library/Developer/Xcode/DerivedData/ModuleCache -fmodules-prune-interval=86400 -fmodules-prune-after=345600 -fbuild-session-file=/Users/username/Library/Developer/Xcode/DerivedData/ModuleCache/Session.modulevalidation -fmodules-validate-once-per-build-session -Wnon-modular-include-in-framework-module -Werror=non-modular-include-in-framework-module -Wno-trigraphs -fpascal-strings -O0 -fno-common -Wno-missing-field-initializers -Wno-missing-prototypes -Werror=return-type -Wunreachable-code -Wno-implicit-atomic-properties -Werror=deprecated-objc-isa-usage -Werror=objc-root-class -Wno-arc-repeated-use-of-weak -Wduplicate-method-match -Wno-missing-braces -Wparentheses -Wswitch -Wunused-function -Wno-unused-label -Wno-unused-parameter -Wunused-variable -Wunused-value -Wempty-body -Wconditional-uninitialized -Wno-unknown-pragmas -Wno-shadow -Wno-four-char-constants -Wno-conversion -Wconstant-conversion -Wint-conversion -Wbool-conversion -Wenum-conversion -Wshorten-64-to-32 -Wpointer-sign -Wno-newline-eof -Wno-selector -Wno-strict-selector-match -Wundeclared-selector -Wno-deprecated-implementations -DDEBUG=1 -DOBJC_OLD_DISPATCH_PROTOTYPES=0 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS9.3.sdk -fstrict-aliasing -Wprotocol -Wdeprecated-declarations -miphoneos-version-min=9.3 -g -Wno-sign-conversion -fembed-bitcode-marker -I/Users/username/Library/Developer/Xcode/DerivedData/myApp-alstnncsvqtenwfmppcsobrovoao/Build/Intermediates/myApp.build/Debug-iphoneos/myApp.build/myApp.hmap -I/Users/username/Library/Developer/Xcode/DerivedData/myApp-alstnncsvqtenwfmppcsobrovoao/Build/Products/Debug-iphoneos/include -I/Users/username/Developer/myApp/myApp -I/Users/username/Library/Developer/Xcode/DerivedData/myApp-alstnncsvqtenwfmppcsobrovoao/Build/Intermediates/myApp.build/Debug-iphoneos/myApp.build/DerivedSources/arm64 -I/Users/username/Library/Developer/Xcode/DerivedData/myApp-alstnncsvqtenwfmppcsobrovoao/Build/Intermediates/myApp.build/Debug-iphoneos/myApp.build/DerivedSources -F/Users/username/Library/Developer/Xcode/DerivedData/myApp-alstnncsvqtenwfmppcsobrovoao/Build/Products/Debug-iphoneos -fno-objc-arc -include /Users/username/Library/Developer/Xcode/DerivedData/myApp-alstnncsvqtenwfmppcsobrovoao/Build/Intermediates/PrecompiledHeaders/LibMecabSample_Prefix-fbzdoofsvssuyecvsrlrazplcjeo/LibMecabSample_Prefix.pch -MMD -MT dependencies -MF /Users/username/Library/Developer/Xcode/DerivedData/myApp-alstnncsvqtenwfmppcsobrovoao/Build/Intermediates/myApp.build/Debug-iphoneos/myApp.build/Objects-normal/arm64/Mecab.d --serialize-diagnostics /Users/username/Library/Developer/Xcode/DerivedData/myApp-alstnncsvqtenwfmppcsobrovoao/Build/Intermediates/myApp.build/Debug-iphoneos/myApp.build/Objects-normal/arm64/Mecab.dia -c /Users/username/Developer/myApp/myApp/Mecab.m -o /Users/username/Library/Developer/Xcode/DerivedData/myApp-alstnncsvqtenwfmppcsobrovoao/Build/Intermediates/myApp.build/Debug-iphoneos/myApp.build/Objects-normal/arm64/Mecab.o
In file included from /Users/username/Developer/myApp/myApp/Mecab.m:12:
/Users/username/Developer/myApp/myApp/Mecab.h:13:2: error: unknown type name 'mecab_t'
mecab_t *mecab;
^
/Users/username/Developer/myApp/myApp/Mecab.m:29:11: error: implicit declaration of function 'mecab_new2' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
mecab = mecab_new2([[@"-d " stringByAppendingString:path] UTF8String]);
^
/Users/username/Developer/myApp/myApp/Mecab.m:32:49: error: implicit declaration of function 'mecab_strerror' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
fprintf(stderr, "error in mecab_new2: %s\n", mecab_strerror(NULL));
^
/Users/username/Developer/myApp/myApp/Mecab.m:32:49: warning: format specifies type 'char *' but the argument has type 'int' [-Wformat]
fprintf(stderr, "error in mecab_new2: %s\n", mecab_strerror(NULL));
~~ ^~~~~~~~~~~~~~~~~~~~
%d
/Users/username/Developer/myApp/myApp/Mecab.m:38:8: error: unknown type name 'mecab_node_t'
const mecab_node_t *node;
^
/Users/username/Developer/myApp/myApp/Mecab.m:42:9: error: implicit declaration of function 'mecab_sparse_tonode2' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
node = mecab_sparse_tonode2(mecab, buf, l);
^
/Users/username/Developer/myApp/myApp/Mecab.m:65:3: error: implicit declaration of function 'mecab_destroy' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
mecab_destroy(mecab);
^
1 warning and 6 errors generated.
Ld /Users/username/Library/Developer/Xcode/DerivedData/myApp-alstnncsvqtenwfmppcsobrovoao/Build/Intermediates/myApp.build/Debug-iphoneos/myApp.build/Objects-normal/armv7/myApp normal armv7
cd /Users/username/Developer/myApp
export IPHONEOS_DEPLOYMENT_TARGET=9.3
export PATH="/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin:/Applications/Xcode.app/Contents/Developer/usr/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin"
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang -arch armv7 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS9.3.sdk -L/Users/username/Library/Developer/Xcode/DerivedData/myApp-alstnncsvqtenwfmppcsobrovoao/Build/Products/Debug-iphoneos -L/Users/username/Developer/myApp -F/Users/username/Library/Developer/Xcode/DerivedData/myApp-alstnncsvqtenwfmppcsobrovoao/Build/Products/Debug-iphoneos -filelist /Users/username/Library/Developer/Xcode/DerivedData/myApp-alstnncsvqtenwfmppcsobrovoao/Build/Intermediates/myApp.build/Debug-iphoneos/myApp.build/Objects-normal/armv7/myApp.LinkFileList -miphoneos-version-min=9.3 -dead_strip -Xlinker -no_deduplicate -fembed-bitcode-marker -ObjC -fobjc-arc -fobjc-link-runtime -lstdc++ -liconv -framework CoreGraphics -framework UIKit -framework Foundation /Users/username/Library/Developer/Xcode/DerivedData/myApp-alstnncsvqtenwfmppcsobrovoao/Build/Products/Debug-iphoneos/libmecab.a -Xlinker -dependency_info -Xlinker /Users/username/Library/Developer/Xcode/DerivedData/myApp-alstnncsvqtenwfmppcsobrovoao/Build/Intermediates/myApp.build/Debug-iphoneos/myApp.build/Objects-normal/armv7/myApp_dependency_info.dat -o /Users/username/Library/Developer/Xcode/DerivedData/myApp-alstnncsvqtenwfmppcsobrovoao/Build/Intermediates/myApp.build/Debug-iphoneos/myApp.build/Objects-normal/armv7/myApp
ld: file not found: /Users/username/Library/Developer/Xcode/DerivedData/myApp-alstnncsvqtenwfmppcsobrovoao/Build/Intermediates/myApp.build/Debug-iphoneos/myApp.build/Objects-normal/armv7/Mecab.o
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Ld /Users/username/Library/Developer/Xcode/DerivedData/myApp-alstnncsvqtenwfmppcsobrovoao/Build/Intermediates/myApp.build/Debug-iphoneos/myApp.build/Objects-normal/arm64/myApp normal arm64
cd /Users/username/Developer/myApp
export IPHONEOS_DEPLOYMENT_TARGET=9.3
export PATH="/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin:/Applications/Xcode.app/Contents/Developer/usr/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin"
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang -arch arm64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS9.3.sdk -L/Users/username/Library/Developer/Xcode/DerivedData/myApp-alstnncsvqtenwfmppcsobrovoao/Build/Products/Debug-iphoneos -L/Users/username/Developer/myApp -F/Users/username/Library/Developer/Xcode/DerivedData/myApp-alstnncsvqtenwfmppcsobrovoao/Build/Products/Debug-iphoneos -filelist /Users/username/Library/Developer/Xcode/DerivedData/myApp-alstnncsvqtenwfmppcsobrovoao/Build/Intermediates/myApp.build/Debug-iphoneos/myApp.build/Objects-normal/arm64/myApp.LinkFileList -miphoneos-version-min=9.3 -dead_strip -Xlinker -no_deduplicate -fembed-bitcode-marker -ObjC -fobjc-arc -fobjc-link-runtime -lstdc++ -liconv -framework CoreGraphics -framework UIKit -framework Foundation /Users/username/Library/Developer/Xcode/DerivedData/myApp-alstnncsvqtenwfmppcsobrovoao/Build/Products/Debug-iphoneos/libmecab.a -Xlinker -dependency_info -Xlinker /Users/username/Library/Developer/Xcode/DerivedData/myApp-alstnncsvqtenwfmppcsobrovoao/Build/Intermediates/myApp.build/Debug-iphoneos/myApp.build/Objects-normal/arm64/myApp_dependency_info.dat -o /Users/username/Library/Developer/Xcode/DerivedData/myApp-alstnncsvqtenwfmppcsobrovoao/Build/Intermediates/myApp.build/Debug-iphoneos/myApp.build/Objects-normal/arm64/myApp
ld: file not found: /Users/username/Library/Developer/Xcode/DerivedData/myApp-alstnncsvqtenwfmppcsobrovoao/Build/Intermediates/myApp.build/Debug-iphoneos/myApp.build/Objects-normal/arm64/Mecab.o
clang: error: linker command failed with exit code 1 (use -v to see invocation)
CreateUniversalBinary /Users/username/Library/Developer/Xcode/DerivedData/myApp-alstnncsvqtenwfmppcsobrovoao/Build/Products/Debug-iphoneos/myApp.app/myApp normal armv7\ arm64
cd /Users/username/Developer/myApp
export PATH="/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin:/Applications/Xcode.app/Contents/Developer/usr/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin"
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/lipo -create /Users/username/Library/Developer/Xcode/DerivedData/myApp-alstnncsvqtenwfmppcsobrovoao/Build/Intermediates/myApp.build/Debug-iphoneos/myApp.build/Objects-normal/armv7/myApp /Users/username/Library/Developer/Xcode/DerivedData/myApp-alstnncsvqtenwfmppcsobrovoao/Build/Intermediates/myApp.build/Debug-iphoneos/myApp.build/Objects-normal/arm64/myApp -output /Users/username/Library/Developer/Xcode/DerivedData/myApp-alstnncsvqtenwfmppcsobrovoao/Build/Products/Debug-iphoneos/myApp.app/myApp
fatal error: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/lipo: can't open input file: /Users/username/Library/Developer/Xcode/DerivedData/myApp-alstnncsvqtenwfmppcsobrovoao/Build/Intermediates/myApp.build/Debug-iphoneos/myApp.build/Objects-normal/armv7/myApp (No such file or directory)
我不确定这仅仅是链接器问题还是与C编译器有关,但是我已经花了几天的时间进行修补,但似乎无法弄清楚。如果有人可以提供提示,我将非常感激。
最佳答案
花了几个小时弄清楚了,但我知道了。一旦知道了步骤,实际上并没有那么困难。
好的,首先,手动将mecab
文件夹复制到您的项目文件夹(否则将无法正常工作。然后右键单击项目的顶部树对象,然后选择“添加文件”。
在mecab文件夹中选择mecab.xcodeproj文件。它应该已经添加到您的项目树中,以及它的子文件夹,然后是所有类。
然后将Mecab.h/.m和Node.h/.m文件拖到您的项目中,确保选择复制它们。
然后转到项目的目标,并在build phases
中添加这三个库。
然后转到build settings
中的“标题搜索路径”,并将路径添加到您的mecab文件夹中。如果像我一样将其复制到项目的文件夹中,则它将看起来像这样。
然后将IPADic文件夹拖到您的项目中,注意不要将其放在mecab.xcproj子文件夹下。您希望在主捆绑包中找到它,否则mecab将找不到它。
然后清理并构建。它应该工作。我刚刚将其添加到应用程序委托(delegate)中,并向其提供了测试字符串,然后设置了断点。有效!
关于c++ - 将较旧的Mecab库实现到现代iOS应用中,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37405004/