如下图所示。
我选择了用于编译c ++代码的通用架构。我现在想从终端做同样的事情。
我尝试使用这些选项 -arch x86_64,-arch i386,-m32,-m64
但我一直收到以下错误。
$ g++ -dynamiclib myclass.cc -I hashlib2plus/header/ -L hashlib2plus/lib/ -o myclass.so -v
Apple LLVM version 7.0.2 (clang-700.1.81)
Target: x86_64-apple-darwin15.3.0
Thread model: posix
"/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang" -cc1 -triple x86_64-apple-macosx10.11.0 -Wdeprecated-objc-isa-usage -Werror=deprecated-objc-isa-usage -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -main-file-name myclass.cc -mrelocation-model pic -pic-level 2 -mthread-model posix -mdisable-fp-elim -masm-verbose -munwind-tables -target-cpu core2 -target-linker-version 253.9 -v -dwarf-column-info -resource-dir /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/7.0.2 -I hashlib2plus/header/ -stdlib=libc++ -fdeprecated-macro -fdebug-compilation-dir /Users/akshaythakare/temp/cpp_dll -ferror-limit 19 -fmessage-length 80 -stack-protector 1 -mstackrealign -fblocks -fobjc-runtime=macosx-10.11.0 -fencode-extended-block-signature -fcxx-exceptions -fexceptions -fmax-type-align=16 -fdiagnostics-show-option -fcolor-diagnostics -o /var/folders/xs/1kz_zcnj1v324_rkdx_ftbx00000gn/T/myclass-13c695.o -x c++ myclass.cc
clang -cc1 version 7.0.2 based upon LLVM 3.7.0svn default target x86_64-apple-darwin15.3.0
ignoring nonexistent directory "/usr/include/c++/v1"
#include "..." search starts here:
#include <...> search starts here:
hashlib2plus/header
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1
/usr/local/include
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/7.0.2/include
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include
/usr/include
/System/Library/Frameworks (framework directory)
/Library/Frameworks (framework directory)
End of search list.
"/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld" -demangle -dynamic -dylib -arch x86_64 -macosx_version_min 10.11.0 -o myclass.so -Lhashlib2plus/lib/ /var/folders/xs/1kz_zcnj1v324_rkdx_ftbx00000gn/T/myclass-13c695.o -lc++ -lSystem /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/7.0.2/lib/darwin/libclang_rt.osx.a
Undefined symbols for architecture x86_64:
"sha512wrapper::sha512wrapper()", referenced from:
MyClass::hashCheck() in myclass-13c695.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
是的,从Xcode运行时代码运行正常。
这是我的代码,以防有人知道如何解决错误
MyClass.h
#ifndef _MYCLASS_H_
#define _MYCLASS_H_
class MyClass{
public:
MyClass();
/* Use virtual otherwise linker will try to perform static linkage */
virtual int hashCheck();
};
#endif
MyClass.cc
#include "myclass.h"
#include <dlfcn.h>
#include <iostream>
#include <dirent.h>
#include <vector>
#include "hashlib2plus/header/hashlibpp.h"
// g++ -dynamiclib -flat_namespace myclass.cc -o myclass.so
// hashlib2plus/hl_sha2ext.cpp hashlib2plus/hl_sha512wrapper.cpp
using namespace std;
void hasher(std::string);
std::vector <std::string> vecFile;
extern "C" MyClass* create_object(){
return new MyClass;
}
extern "C" void destroy_object(MyClass* object){
delete object;
}
MyClass::MyClass(){}
std::string globalMasterHash = "sd239d023md302k23s02ssd239d023md302k23s02ssd239d023md302k23s02ssd239d023md302k23s02s";
int MyClass::hashCheck(){
std::string masterHash = "";
hashwrapper *myWrapper = new sha512wrapper();
std::string parentDir = "./static/";
hasher(parentDir);
while(vecFile.empty() != true){
std::string hash2 = myWrapper->getHashFromFile(vecFile.back());
vecFile.pop_back();
masterHash = myWrapper->getHashFromString(hash2 + masterHash);
}
std::cout << masterHash << std::endl;
if(masterHash.compare(globalMasterHash) == 0){
return 1;
} else {
return 0;
}
return 0;
}
void hasher(std::string curr_directory){
DIR *dir;
dirent *ent;
if((dir = opendir(curr_directory.c_str())) != NULL){
while ((ent = readdir(dir))!=NULL){
if(ent->d_name[0] != '.'){
std::string path = curr_directory + ent->d_name;
if (ent->d_type == DT_DIR){
path += "/";
hasher(path);
} else {
vecFile.push_back(path);
}
}
}
}
return;
}
我正在链接click here
的lib