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

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.

Related

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?

main function not being detected when compiled

I am trying to run a program that opens a window. the purpose is to get the program started opening a window is the start of all programs right?
But when I run my code for some reason I get this error:
Undefined symbols for architecture x86_64:
"_main", referenced from:
implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
However, in my code I do have the main() function so why am I getting this error?
This is my code:
#include <SDL2/SDL.h>
#include <SDL2_image/SDL_image.h>
#include <SDL2_ttf/SDL_ttf.h>
#include <stdio.h>
int main(){
if(SDL_Init( SDL_INIT_EVERYTHING ) < 0){
std::cout << "error 1\n";
std::cout << SDL_GetError();
std::cout << "\n";
return -1;
}
if(TTF_Init() < 0){
std::cout << "error 2\n";
std::cout << TTF_GetError();
std::cout << "\n";
return -1;
}
SDL_Window* window = SDL_CreateWindow("test", 0, 0, 500, 500, 0);
if(!window){
std::cout << "error 3\n";
std::cout << SDL_GetError();
std::cout << "\n";
return -1;
}
int windowid = SDL_GetWindowID(window);
SDL_Renderer* Renderer = SDL_CreateRenderer(window, -1, 0);
running = true;
SDL_Event event;
while(running){
while(SDL_PollEvent(&event)){
if(event.type == SDL_WindowEvent){
if(event.window.windowID == windowid){
if(event.window.type == SDL_WindowClose){
Destroywindow(window);
running = false;
}
}
}
}
}
return 0;
}
my make file looks like this:
#!/bin/bash
brew update
brew install sdl2
g++ -o /Users/mikahshattuck/noneproject/none2019-05-0909-22-
14:2:/none.app/Contents/MacOS/mainrun.cpp -I /Library/Frameworks -l
SDL2
exit 0
this is the full out:
Already up-to-date.
Warning: sdl2 2.0.9_1 is already installed and up-to-date
To reinstall 2.0.9_1, run `brew reinstall sdl2`
Undefined symbols for architecture x86_64:
"_main", referenced from:
implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see
invocation)
logout
Saving session...
...copying shared history...
...saving history...truncating history files...
...completed.
[Process completed]
thank you in advance
When using SDL on macOS or Windows, you need to add -Dmain=SDL_main to your compile flags and -lSDL2main to your link flags. Since you're using Homebrew, you can make it easier and just use pkg-config to get the correct flags. Use this compiler command as a template and adapt it to your needs:
g++ $(pkg-config --cflags sdl2) -I /Library/Frameworks source.cpp -o output_executable $(pkg-config --libs sdl2)
However, it seems you are also using SDL_ttf, not just plain SDL. In this case, you should probably use SDL2_ttf instead of sdl2 as the package argument of pkg-config:
g++ $(pkg-config --cflags SDL2_ttf) -I /Library/Frameworks source.cpp -o output_executable $(pkg-config --libs SDL2_ttf)
The SDL2_ttf package depends on the sdl2 package, so using SDL2_ttf will also emit the needed flags for sdl2.
The names of the pkg-config packages correspond to *.pc files installed by Homebrew into /usr/local/lib/pkgconfig.

MacOS ld: symbols not found (x86_64)

I have the following error:
Undefined symbols for architecture x86_64:
"_inflateEnd", referenced from:
uWS::Hub::~Hub() in main.o
"_inflateInit2_", referenced from:
uWS::Hub::Hub(int, bool, unsigned int) in main.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 checked all the similar questions and their solutions but nothing worked for me.
I am trying to test uWebSockets (https://github.com/uNetworking/uWebSockets) and have the following file structure:
my_app (which has main.cpp)
ext/uWebSockets (which is a clone of the repository above)
So, I am doing the following:
~/ext/uWebSockets$ make
make `(uname -s)`
c++ -std=c++11 -O3 -I src -shared -fPIC src/Extensions.cpp src/Group.cpp src/Networking.cpp src/Hub.cpp src/Node.cpp src/WebSocket.cpp src/HTTPSocket.cpp src/Socket.cpp src/Epoll.cpp -stdlib=libc++ -mmacosx-version-min=10.7 -undefined dynamic_lookup -L/usr/local/opt/openssl/lib -I/usr/local/opt/openssl/include -o libuWS.dylib
and obtain libuWS.dylib in /ext/uWebSockets
Then, I do the following:
~/my_app$ g++ -c main.cpp -o main.o -I../ext/uWebSockets/src -I/usr/local/opt/openssl/include -std=c++11
So now I have main.o in /my_app. But when I'm trying to:
~/my_app$ g++ -o start main.o -L../ext/uWebSockets -luWS
I receive the aforementioned error:
Undefined symbols for architecture x86_64:
"_inflateEnd", referenced from:
uWS::Hub::~Hub() in main.o
"_inflateInit2_", referenced from:
uWS::Hub::Hub(int, bool, unsigned int) in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Here is the same command with -v:
~/my_app$ g++ -o start main.o -L../ext/uWebSockets -luWS -v
Apple LLVM version 9.0.0 (clang-900.0.39.2)
Target: x86_64-apple-darwin17.3.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
"/Library/Developer/CommandLineTools/usr/bin/ld" -demangle -lto_library /Library/Developer/CommandLineTools/usr/lib/libLTO.dylib -dynamic -arch x86_64 -macosx_version_min 10.13.0 -o start -L../ext/uWebSockets main.o -luWS -lc++ -lSystem /Library/Developer/CommandLineTools/usr/lib/clang/9.0.0/lib/darwin/libclang_rt.osx.a
Undefined symbols for architecture x86_64:
"_inflateEnd", referenced from:
uWS::Hub::~Hub() in main.o
"_inflateInit2_", referenced from:
uWS::Hub::Hub(int, bool, unsigned int) in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
How can I link them correctly?
main.cpp:
#include <iostream>
#include "uWS.h"
int main() {
uWS::Hub h;
h.onError([](void *user) {
std::cout << "WebSocket: Error has occured" << std::endl;
});
h.onConnection([](uWS::WebSocket<uWS::CLIENT> *ws, uWS::HttpRequest req) {
std::cout << "Client established a remote connection over non-SSL" << std::endl;
});
h.onDisconnection([](uWS::WebSocket<uWS::CLIENT> *ws, int code, char *message, size_t length) {
std::cout << "Client got disconnected with data: " << ws->getUserData() << ", code: " << code << ", message: <" << std::string(message, length) << ">" << std::endl;
});
// url, user, headers, timeout, group client
h.connect("wss://www.test.com/", (void *) 0, {}, 5000);
h.run();
std::cout << "Falling through testConnections" << std::endl;
return 0;
}
Finally, I was able to fix it. The problem was in zlib, which I didn't include.
So the correct way of running this is g++ -o start main.o -luWS -lz

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

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