What are the compiler options used by Xcode for universal build? - c++

As seen in the image below.
Click here for image
I have selected the Universal architecture for compiling c++ code. I would now like to do the same from terminal.
I tried to use the options
-arch x86_64, -arch i386, -m32, -m64
but i keep getting the following error.
$ 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)
And yes the code was running normally when being run from Xcode.
Here's my code in case anyone knows how to resolve the error
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;
}
The lib i am linking click here

Related

Is this an Apple compiler v14 optimization bug?

Source code:
#include <cstdio>
static const char encodeBase64Table[64 + 1] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
int main() {
// create decoding table
unsigned long invalid = 64;
unsigned long decodeBase64Table[256] = {};
for (unsigned long i = 0; i < 256; i++)
decodeBase64Table[i] = invalid;
for (unsigned long i = 0; i < 64; i++) {
decodeBase64Table[(unsigned char)encodeBase64Table[i]] = i;
}
printf("decodeBase64Table[65] = %lu\n", decodeBase64Table[65]);
return 0;
}
Apple Clang 11.0.3 (expected output, even with -O3):
/t/s/1673994196 ❯❯❯ /usr/bin/clang++ --version
Apple clang version 11.0.3 (clang-1103.0.32.62)
Target: x86_64-apple-darwin21.6.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
/t/s/1673994196 ❯❯❯ /usr/bin/clang++ -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk -O3 -std=c++17 a.cpp
/t/s/1673994196 ❯❯❯ ./a.out
decodeBase64Table[65] = 0
Apple Clang 14.0.0 (unexpected output, only with -O3):
/t/s/1673994196 ❯❯❯ /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++ --version
Apple clang version 14.0.0 (clang-1400.0.29.202)
Target: x86_64-apple-darwin21.6.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
/t/s/1673994196 ❯❯❯ /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++ -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk -O3 -std=c++17 a.cpp
/t/s/1673994196 ❯❯❯ ./a.out
decodeBase64Table[65] = 64

M1 Mac g++ use GMP Library error Undefined symbols for architecture arm64 "__ZlsRSoPK12__mpz_struct"

I use gcc with brew on my M1 Mac, But these code can run successfully with clang, but can not linked with g++(Homebrew).
$ g++-12 -v
Using built-in specs.
COLLECT_GCC=g++-12
COLLECT_LTO_WRAPPER=/opt/homebrew/Cellar/gcc/12.2.0/bin/../libexec/gcc/aarch64-apple-darwin21/12/lto-wrapper
Target: aarch64-apple-darwin21
Configured with: ../configure --prefix=/opt/homebrew/opt/gcc --libdir=/opt/homebrew/opt/gcc/lib/gcc/current --disable-nls --enable-checking=release --with-gcc-major-version-only --enable-languages=c,c++,objc,obj-c++,fortran --program-suffix=-12 --with-gmp=/opt/homebrew/opt/gmp --with-mpfr=/opt/homebrew/opt/mpfr --with-mpc=/opt/homebrew/opt/libmpc --with-isl=/opt/homebrew/opt/isl --with-zstd=/opt/homebrew/opt/zstd --with-pkgversion='Homebrew GCC 12.2.0' --with-bugurl=https://github.com/Homebrew/homebrew-core/issues --with-system-zlib --build=aarch64-apple-darwin21 --with-sysroot=/Library/Developer/CommandLineTools/SDKs/MacOSX12.sdk
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 12.2.0 (Homebrew GCC 12.2.0)
And My code for GMP:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include "gmp.h"
#include "gmpxx.h"
using namespace std;
int main() { // Declare two arbitrary-precision integers
mpz_t x, y;
// Initialize the integers
mpz_init(x);
mpz_init(y);
// Set the value of x to 1234567890
mpz_set_str(x, "1234567890", 10);
// Set the value of y to 9876543210
mpz_set_str(y, "9876543210", 10);
// Declare a third arbitrary-precision integer to store the result
mpz_t result;
mpz_init(result);
// Perform the multiplication
mpz_mul(result, x, y);
// Print the result
std::cout << "Result: " << result << std::endl;
// Clear the variables to free memory
mpz_clear(x);
mpz_clear(y);
mpz_clear(result);
return 0;
}
Error:
$ g++-12 gmp_test.cpp -lgmpxx -lgmp
Undefined symbols for architecture arm64:
"__ZlsRSoPK12__mpz_struct", referenced from:
__Z2t1v in cceShKq0.o
__ZlsIA1_12__mpz_struct17__gmp_binary_exprI10__gmp_exprIS1_S1_ES4_23__gmp_binary_multipliesEERSoS7_RKS3_IT_T0_E in cceShKq0.o
ld: symbol(s) not found for architecture arm64
collect2: error: ld returned 1 exit status
I can run the above code with both llvm clang(with brew) and /usr/bin/clang(with xcode), but cannot run with g++, and I don't not why!

LLVM Segmentation fault on basic function pass

I'm completely new to LLVM. I tried to follow the functions in the following article. This is the function pass that is defined there:
#include "llvm/Pass.h"
#include "llvm/IR/Function.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/IR/LegacyPassManager.h"
#include "llvm/IR/InstrTypes.h"
#include "llvm/Transforms/IPO/PassManagerBuilder.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
using namespace llvm;
namespace {
struct SkeletonPass : public FunctionPass {
static char ID;
SkeletonPass() : FunctionPass(ID) {}
virtual bool runOnFunction(Function &F) {
for (auto &B : F) {
for (auto &I : B) {
if (auto *op = dyn_cast<BinaryOperator>(&I)) {
// Insert at the point where the instruction `op` appears.
IRBuilder<> builder(op);
// Make a multiply with the same operands as `op`.
Value *lhs = op->getOperand(0);
Value *rhs = op->getOperand(1);
Value *mul = builder.CreateMul(lhs, rhs);
// Everywhere the old instruction was used as an operand, use our
// new multiply instruction instead.
for (auto &U : op->uses()) {
User *user = U.getUser(); // A User is anything with operands.
user->setOperand(U.getOperandNo(), mul);
}
// We modified the code.
return true;
}
}
}
return false;
}
};
}
char SkeletonPass::ID = 0;
// Automatically enable the pass.
// http://adriansampson.net/blog/clangpass.html
static void registerSkeletonPass(const PassManagerBuilder &,
legacy::PassManagerBase &PM) {
PM.add(new SkeletonPass());
}
static RegisterStandardPasses
RegisterMyPass(PassManagerBuilder::EP_EarlyAsPossible,
registerSkeletonPass);
Then compiled the file:
$ cd llvm-pass-skeleton
$ mkdir build
$ cd build
$ cmake .. # Generate the Makefile.
$ make # Actually build the pass.
And run it using:
/usr/local/opt/llvm/bin/clang -Xclang -load -Xclang build/skeleton/libSkeletonPass.so example.c
Where example.c is:
#include <stdio.h>
int main(int argc, const char** argv) {
int num;
scanf("%i", &num);
printf("%i\n", num + 2);
return 0;
}
I'm on Mac OS X Mojave and I installed LLVM using homebrew and put it in my PATH.
I get the following error message
clang version 9.0.0 (tags/RELEASE_900/final)
Target: x86_64-apple-darwin18.7.0
Thread model: posix
InstalledDir: /usr/local/Cellar/llvm/9.0.0_1/bin
"/usr/local/Cellar/llvm/9.0.0_1/bin/clang-9" -cc1 -triple x86_64-apple-macosx10.14.0 -Wdeprecated-objc-isa-usage -Werror=deprecated-objc-isa-usage -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -discard-value-names -main-file-name example.c -mrelocation-model pic -pic-level 2 -mthread-model posix -mdisable-fp-elim -masm-verbose -munwind-tables -target-cpu penryn -dwarf-column-info -debugger-tuning=lldb -ggnu-pubnames -target-linker-version 512.4 -v -resource-dir /usr/local/Cellar/llvm/9.0.0_1/lib/clang/9.0.0 -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk -internal-isystem /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/local/include -internal-isystem /usr/local/Cellar/llvm/9.0.0_1/lib/clang/9.0.0/include -internal-externc-isystem /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include -fdebug-compilation-dir /Users/BN/Documents/Library/CS/Compilers/LLVM/llvm-pass-skeleton -ferror-limit 19 -fmessage-length 80 -stack-protector 1 -fblocks -fencode-extended-block-signature -fregister-global-dtors-with-atexit -fobjc-runtime=macosx-10.14.0 -fmax-type-align=16 -fdiagnostics-show-option -fcolor-diagnostics -load build/skeleton/libSkeletonPass.so -o /var/folders/cf/1_hzh5h91x90j4xx_h1mlb4w0000gp/T/example-4b27ba.o -x c example.c
clang -cc1 version 9.0.0 based upon LLVM 9.0.0 default target x86_64-apple-darwin18.7.0
ignoring nonexistent directory "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/local/include"
ignoring nonexistent directory "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/Library/Frameworks"
#include "..." search starts here:
#include <...> search starts here:
/usr/local/Cellar/llvm/9.0.0_1/lib/clang/9.0.0/include
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks (framework directory)
End of search list.
Stack dump:
0. Program arguments: /usr/local/Cellar/llvm/9.0.0_1/bin/clang-9 -cc1 -triple x86_64-apple-macosx10.14.0 -Wdeprecated-objc-isa-usage -Werror=deprecated-objc-isa-usage -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -discard-value-names -main-file-name example.c -mrelocation-model pic -pic-level 2 -mthread-model posix -mdisable-fp-elim -masm-verbose -munwind-tables -target-cpu penryn -dwarf-column-info -debugger-tuning=lldb -ggnu-pubnames -target-linker-version 512.4 -v -resource-dir /usr/local/Cellar/llvm/9.0.0_1/lib/clang/9.0.0 -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk -internal-isystem /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/local/include -internal-isystem /usr/local/Cellar/llvm/9.0.0_1/lib/clang/9.0.0/include -internal-externc-isystem /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include -fdebug-compilation-dir /Users/BN/Documents/Library/CS/Compilers/LLVM/llvm-pass-skeleton -ferror-limit 19 -fmessage-length 80 -stack-protector 1 -fblocks -fencode-extended-block-signature -fregister-global-dtors-with-atexit -fobjc-runtime=macosx-10.14.0 -fmax-type-align=16 -fdiagnostics-show-option -fcolor-diagnostics -load build/skeleton/libSkeletonPass.so -o /var/folders/cf/1_hzh5h91x90j4xx_h1mlb4w0000gp/T/example-4b27ba.o -x c example.c
0 clang-9 0x00000001071fba46 llvm::sys::PrintStackTrace(llvm::raw_ostream&) + 40
1 clang-9 0x00000001071fbe98 SignalHandler(int) + 180
2 libsystem_platform.dylib 0x00007fff6f2aeb5d _sigtramp + 29
3 libsystem_platform.dylib 0x0000000000000001 _sigtramp + 18446603338651079873
4 clang-9 0x0000000106e94485 llvm::object_deleter<llvm::SmallVector<std::__1::pair<llvm::PassManagerBuilder::ExtensionPointTy, std::__1::function<void (llvm::PassManagerBuilder const&, llvm::legacy::PassManagerBase&)> >, 8u> >::call(void*) + 19
5 clang-9 0x00000001071abc95 llvm::llvm_shutdown() + 53
6 clang-9 0x0000000107193061 llvm::InitLLVM::~InitLLVM() + 15
7 clang-9 0x0000000106034857 main + 7249
8 libdyld.dylib 0x00007fff6f0c33d5 start + 1
clang-9: error: unable to execute command: Segmentation fault: 11
clang-9: error: clang frontend command failed due to signal (use -v to see invocation)
clang version 9.0.0 (tags/RELEASE_900/final)
Target: x86_64-apple-darwin18.7.0
Thread model: posix
InstalledDir: /usr/local/Cellar/llvm/9.0.0_1/bin
clang-9: note: diagnostic msg: PLEASE submit a bug report to https://bugs.llvm.org/ and include the crash backtrace, preprocessed source, and associated run script.
clang-9: error: unable to execute command: Segmentation fault: 11
clang-9: note: diagnostic msg: Error generating preprocessed source(s).
The error message states that the two non-existant directories /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/local/include and /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/Library/Frameworks will be ignored. These directories are actually installed under /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include and /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library, respectively. Not sure whether this is relevant and how to tell Clang to look in those locations.
Can somebody help me with this please?

How to clone function from another module and set it AlwaysInline on LLVM pass?

I want to clone a function from another bitcode module and set it AlwaysInline so that it can be inlined.
But an error occurs when applying such custom pass (It seems that the cause is the use of CloneFunction()). How can I fix it?
I'm using LLVM 3.6.0 on OS X and target is Android armeabi-v7a (It should be same on other target).
callee.cpp (compiled to bitcode module callee.bc)
#include <jni.h>
#include <android/log.h>
#ifdef __cplusplus
extern "C" {
#endif
void callee(char *str, int num)
{
__android_log_print(ANDROID_LOG_DEBUG, "target", "%s, 0x%x", str, num);
}
#ifdef __cplusplus
}
#endif
test.cpp (a module custom pass is applied)
#include <jni.h>
#ifdef __cplusplus
extern "C" {
#endif
void caller()
{
}
void Java_com_android_test_TestActivity_test(JNIEnv *env, jobject thiz)
{
caller();
}
#ifdef __cplusplus
}
#endif
TestPass.cpp (a custom pass)
#include "llvm/IR/Module.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/Type.h"
#include "llvm/IR/InstIterator.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/GlobalValue.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/Support/SourceMgr.h"
#include "llvm/IRReader/IRReader.h"
#include "llvm/Transforms/Utils/Cloning.h"
#include "llvm/IR/LegacyPassManager.h"
#include "llvm/Transforms/IPO/PassManagerBuilder.h"
using namespace llvm;
namespace {
struct TestPass : public ModulePass {
static char ID;
TestPass() : ModulePass(ID) {}
virtual bool runOnModule(Module &M)
{
for (Module::iterator F = M.begin(), E = M.end(); F!= E; ++F)
{
if (F->getName() == "caller")
{
inst_iterator I = inst_begin(F);
Instruction *inst = &*I;
SMDiagnostic error;
LLVMContext context;
std::unique_ptr<Module> m = parseIRFile("callee.bc", error, context);
for (Module::iterator f = m->getFunctionList().begin(); f != m->getFunctionList().end(); f++)
{
if (f->getName() == "callee")
{
// Copy callee.bc callee function to current module and set AlwaysInline
ValueToValueMapTy VMap;
Function* callee_clone = CloneFunction(f, VMap, false);
/*
callee_clone->setName("callee_clone");
callee_clone->addFnAttr(Attribute::AlwaysInline);
M.getFunctionList().push_back(callee_clone);
// Add call to copied callee function
GlobalVariable* str = new GlobalVariable(M,
ArrayType::get(IntegerType::get(M.getContext(), 8), 8),
false,
GlobalValue::ExternalLinkage,
0,
"str");
str->setAlignment(1);
Constant* str_init = ConstantDataArray::getString(M.getContext(), "abcdefgh", false);
str->setInitializer(str_init);
ConstantInt* param1_indice1 = ConstantInt::get(M.getContext(), APInt(32, StringRef("0"), 10));
ConstantInt* param1_indice2 = ConstantInt::get(M.getContext(), APInt(32, StringRef("0"), 10));
std::vector<Constant*> param1_indices;
param1_indices.push_back(param1_indice1);
param1_indices.push_back(param1_indice2);
Constant* param1 = ConstantExpr::getGetElementPtr(str, param1_indices);
GlobalVariable* num = new GlobalVariable(M,
IntegerType::get(M.getContext(), 32),
false,
GlobalValue::ExternalLinkage,
0,
"num");
num->setAlignment(4);
ConstantInt* num_init = ConstantInt::get(M.getContext(), APInt(32, StringRef("12345"), 10));
num->setInitializer(num_init);
LoadInst* param2 = new LoadInst(num, "", false, inst);
param2->setAlignment(4);
std::vector<Value*> params;
params.push_back(param1);
params.push_back(param2);
CallInst* ci = CallInst::Create(callee_clone, params, "", inst);
ci->setCallingConv(CallingConv::C);
ci->setTailCall(false);
*/
}
}
}
}
return true;
}
};
}
char TestPass::ID = 0;
static RegisterPass<TestPass> X("TestPass", "TestPass", false, false);
static void registerTestPass(const PassManagerBuilder &, legacy::PassManagerBase &PM)
{
PM.add(new TestPass());
}
static RegisterStandardPasses RegisterTestPass(PassManagerBuilder::EP_ModuleOptimizerEarly, registerTestPass);
Error applying custom pass
[...snip...]
While deleting: [7 x i8]* %.str
Use still stuck around after Def is destroyed:#.str = private unnamed_addr constant [7 x i8] <null operand!>, align 1
Assertion failed: (use_empty() && "Uses remain when a value is destroyed!"), function ~Value, file /private/tmp/llvm/llvm-3.6.0.src/lib/IR/Value.cpp, line 81.
0 clang++ 0x000000011154d909 llvm::sys::PrintStackTrace(__sFILE*) + 57
1 clang++ 0x000000011154e07b SignalHandler(int) + 555
2 libsystem_platform.dylib 0x00007fffb7debb3a _sigtramp + 26
3 libsystem_platform.dylib 0x00000000509ff1d0 _sigtramp + 2562799280
4 clang++ 0x000000011154de36 abort + 22
5 clang++ 0x000000011154de11 __assert_rtn + 81
6 clang++ 0x00000001114f2104 llvm::Value::~Value() + 660
7 clang++ 0x00000001114a63d1 llvm::GlobalVariable::~GlobalVariable() + 97
8 clang++ 0x00000001114e3ba9 llvm::Module::~Module() + 105
9 TestPass.dylib 0x000000011254d7dc (anonymous namespace)::TestPass::runOnModule(llvm::Module&) + 524
10 clang++ 0x00000001114cdc4e llvm::legacy::PassManagerImpl::run(llvm::Module&) + 1054
11 clang++ 0x000000010f43f49a clang::EmitBackendOutput(clang::DiagnosticsEngine&, clang::CodeGenOptions const&, clang::TargetOptions const&, clang::LangOptions const&, llvm::StringRef, llvm::Module*, clang::BackendAction, llvm::raw_ostream*) + 7098
12 clang++ 0x000000010f57433d clang::BackendConsumer::HandleTranslationUnit(clang::ASTContext&) + 509
13 clang++ 0x000000010f609954 clang::ParseAST(clang::Sema&, bool, bool) + 468
14 clang++ 0x000000010f27aa53 clang::FrontendAction::Execute() + 67
15 clang++ 0x000000010f24cb2c clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) + 956
16 clang++ 0x000000010f20763a clang::ExecuteCompilerInvocation(clang::CompilerInstance*) + 4154
17 clang++ 0x000000010f1fe65c cc1_main(llvm::ArrayRef<char const*>, char const*, void*) + 1068
18 clang++ 0x000000010f2057ec main + 11660
19 libdyld.dylib 0x00007fffb7bdc235 start + 1
20 libdyld.dylib 0x0000000000000066 start + 1212300850
Stack dump:
0. Program arguments: /opt/llvm-3.6.0/build/Release+Asserts/bin/clang++ -cc1 -triple thumbv7-none-linux-androideabi -S -disable-free -main-file-name test.cpp -mrelocation-model pic -pic-level 1 -mthread-model posix -relaxed-aliasing -fmath-errno -masm-verbose -no-integrated-as -mconstructor-aliases -munwind-tables -fuse-init-array -target-cpu cortex-a8 -target-feature +soft-float-abi -target-feature +vfp3 -target-feature +d16 -target-feature -neon -target-abi aapcs-linux -mfloat-abi soft -target-linker-version 253.3 -g -dwarf-column-info -ffunction-sections -coverage-file /opt/test/obj/local/armeabi-v7a/objs/test/test.o -resource-dir /opt/llvm-3.6.0/build/Release+Asserts/bin/../lib/clang/3.6.0 -dependency-file /opt/test/obj/local/armeabi-v7a/objs/test/test.o.d -MT /opt/test/obj/local/armeabi-v7a/objs/test/test.o -MP -D NDEBUG -D ANDROID -I /Applications/android-ndk-r10e/sources/cxx-stl/llvm-libc++/libcxx/include -I /Applications/android-ndk-r10e/sources/cxx-stl/llvm-libc++/../llvm-libc++abi/libcxxabi/include -I /Applications/android-ndk-r10e/sources/cxx-stl/llvm-libc++/../../android/support/include -I /opt/test/jni -I /Applications/android-ndk-r10e/platforms/android-21/arch-arm/usr/include -internal-isystem /usr/local/include -internal-isystem /opt/llvm-3.6.0/build/Release+Asserts/bin/../lib/clang/3.6.0/include -internal-externc-isystem /include -internal-externc-isystem /usr/include -O1 -Wno-invalid-command-line-argument -Wno-unused-command-line-argument -Wformat -Werror=format-security -std=c++11 -fdeprecated-macro -fno-dwarf-directory-asm -fdebug-compilation-dir /opt/test/jni -ferror-limit 19 -fmessage-length 152 -stack-protector 2 -mstackrealign -fno-rtti -fno-signed-char -fobjc-runtime=gcc -fdiagnostics-show-option -fcolor-diagnostics -load /opt/test/TestPass.dylib -mllvm -debug-pass=Structure -o /var/folders/vq/53lxh89j7ts1rtc9gfg_s7f80000gq/T/test-f2326e.s -x c++ /opt/test/jni/test.cpp
1. <eof> parser at end of file
2. Per-module optimization passes
3. Running pass 'TestPass' on module '/opt/test/jni/test.cpp'.
clang++: error: unable to execute command: Illegal instruction: 4
clang++: error: clang frontend command failed due to signal (use -v to see invocation)
clang version 3.6.0 (tags/RELEASE_360/final)
Target: armv7-none-linux-androideabi
Thread model: posix
clang++: note: diagnostic msg: PLEASE submit a bug report to http://llvm.org/bugs/ and include the crash backtrace, preprocessed source, and associated run script.
clang++: note: diagnostic msg:
********************
PLEASE ATTACH THE FOLLOWING FILES TO THE BUG REPORT:
Preprocessed source(s) and associated run script(s) are located at:
clang++: note: diagnostic msg: /var/folders/vq/53lxh89j7ts1rtc9gfg_s7f80000gq/T/test-773901.cpp
clang++: note: diagnostic msg: /var/folders/vq/53lxh89j7ts1rtc9gfg_s7f80000gq/T/test-773901.sh
clang++: note: diagnostic msg:
********************
make: *** [/opt/test/obj/local/armeabi-v7a/objs/test/test.o] Error 254

sqlite c++ Undefined symbols for architecture x86_64 [duplicate]

This question already has answers here:
Error: undefined reference to `sqlite3_open'
(5 answers)
Closed 8 years ago.
I am running a sqlite c++ demo on Mac osx. I copied the code from the web page shown below.
reference: http://www.tutorialspoint.com/sqlite/sqlite_c_cpp.htm
The source code is
//
#include <stdio.h>
#include <stdlib.h>
#include <sqlite3.h>
#include <fstream>
#include <string>
using namespace std;
static int callback(void *NotUsed, int argc, char **argv, char **azColName){
int i;
for(i=0; i<argc; i++){
printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
}
printf("\n");
return 0;
}
int main(int argc, char* argv[])
{
sqlite3 *db;
char *zErrMsg = 0;
int rc;
char *sql;
string str;
/* Open database */
rc = sqlite3_open("test.db", &db);
if( rc ){
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
exit(0);
}else{
fprintf(stdout, "Opened database successfully\n");
}
/* Create SQL statement */
str = "CREATE TABLE location(country text, state text, city text );CREATE TABLE weather(temp real, tempunit text);";
strcpy(sql,str.c_str());
/* Execute SQL statement */
rc = sqlite3_exec(db, sql, callback, 0, &zErrMsg);
if( rc != SQLITE_OK ){
fprintf(stderr, "SQL error: %s\n", zErrMsg);
sqlite3_free(zErrMsg);
}else{
fprintf(stdout, "Table created successfully\n");
}
sqlite3_close(db);
return 0;
}
The output is
g++ sqlite.cpp -v
Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn)
Target: x86_64-apple-darwin13.3.0
Thread model: posix
"/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang" -cc1 -triple x86_64-apple-macosx10.9.0 -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -main-file-name sqlite.cpp -mrelocation-model pic -pic-level 2 -mdisable-fp-elim -masm-verbose -munwind-tables -target-cpu core2 -target-linker-version 236.3 -v -resource-dir /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/5.1 -stdlib=libc++ -fdeprecated-macro -fdebug-compilation-dir /Users/zerocraft/KuaiPan/Course/ECEN489/test -ferror-limit 19 -fmessage-length 166 -stack-protector 1 -mstackrealign -fblocks -fobjc-runtime=macosx-10.9.0 -fencode-extended-block-signature -fcxx-exceptions -fexceptions -fdiagnostics-show-option -fcolor-diagnostics -vectorize-slp -o /var/folders/1d/dbmt5pd519bcp0dqr5vv76_c0000gn/T/sqlite-742d60.o -x c++ sqlite.cpp
clang -cc1 version 5.1 based upon LLVM 3.4svn default target x86_64-apple-darwin13.3.0
ignoring nonexistent directory "/usr/include/c++/v1"
ignoring nonexistent directory "/usr/local/include"
#include "..." search starts here:
#include <...> search starts here:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/5.1/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 -arch x86_64 -macosx_version_min 10.9.0 -o a.out /var/folders/1d/dbmt5pd519bcp0dqr5vv76_c0000gn/T/sqlite-742d60.o -lc++ -lSystem /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/5.1/lib/darwin/libclang_rt.osx.a
Undefined symbols for architecture x86_64:
"_sqlite3_close", referenced from:
_main in sqlite-742d60.o
"_sqlite3_errmsg", referenced from:
_main in sqlite-742d60.o
"_sqlite3_exec", referenced from:
_main in sqlite-742d60.o
"_sqlite3_free", referenced from:
_main in sqlite-742d60.o
"_sqlite3_open", referenced from:
_main in sqlite-742d60.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I have installed the sqlite library on my laptop. Can anyone cast some light on this? Thanks!
You did not link against the sqlite library. The tutorial you were following said to do this:
g++ test.c -lsqlite3
But you did this:
g++ sqlite.cpp -v
The option '-lsqlite3' tells the linker to link in the sqlite3 library. Without this, it cannot find the symbols that are defined in this library.