leveled::DB::Open() undefined after I installed leveldb and snappy, macOS - c++

I want to verify the leveldb installation. I have a main function only containing levelDB::DB::Open() function. I installed both snappy and leveldb by using brew install. I have boost 1.67 installed too. I have GCC 8.1. I am running macOS 10.13.5.
my source file looks like this:
int main(void) {
leveldb::DB *db;
leveldb::Options options;
options.create_if_missing = true;
auto err = leveldb::DB::Open(options, "../tmpDB", &db);
if (err.ok()) {
std::cout << "success" << std::endl;
}else {
std::cout << "failed" << std::endl;
}
delete db;
return 0;
}
I compile my code I use g++ main.cpp -lleveldb -lsnappy -o test. The compiler generate the error like:
Undefined symbols for architecture x86_64:
"leveldb::DB::Open(leveldb::Options const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, leveldb::DB**)", referenced from:
_main in ccYcksfh.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status
How can I solve this link error?

You can install leveldb with compiler version.
brew reinstall leveldb --cc=gcc-4.8
Can't link against leveldb on OSX

Related

linker command failed with exit code 1 (MAC MPI)

I am trying to run a simple MPI code as I am learning MPI. I am using ECLIPSE PTP platform for c++ for compiling. In linux it is working fine. Here is the code.
MPI_Init(&argc,&argv);
int num_procs;
MPI_Comm_size(MPI_COMM_WORLD,&num_procs);
int rank;
MPI_Comm_rank(MPI_COMM_WORLD,&rank);
if(rank == 0){
cout << "Hello world" << endl;
}
MPI_Finalize();
return 0;
I'm getting this error:
Undefined symbols for architecture x86_64:
"_MPI_Comm_rank", referenced from:
_main 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)
make: *** [mpi_test] Error 1

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

Xcode gives Mach-O linker error: "_JNI_CreateJavaVM", referenced from: _main in main.o

I get this error from testing JNI:
Undefined symbols for architecture x86_64:
"_JNI_CreateJavaVM", referenced from:
_main 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 c++ code:
#include <jni.h>
#include <iostream>
using namespace std;
int main()
{
int res;
JavaVMInitArgs vm_args;
JavaVMOption options[3];
JavaVM *jvm;
JNIEnv *env;
jmethodID mid;
options[0].optionString = "-Djava.compiler=NONE";
options[1].optionString = "-Djava.class.path = /Users/stephen/course/test/Test";
options[2].optionString = "-verbose:NONE";
vm_args.version = JNI_VERSION_1_8;
vm_args.nOptions = 3;
vm_args.options = options;
vm_args.ignoreUnrecognized = JNI_TRUE;
res = JNI_CreateJavaVM(&jvm,(void**)&env,&vm_args);
if(res == JNI_ERR){
cout << "Error invoking the JVM";
return 1;
}
cout <<"create JVM successfully!"<<endl;
jclass cls = env->FindClass("/Users/stephen/course/Qt-project/test/Test");
if(cls != 0){
cout<<"find class successfully!" << endl;
}
mid = env->GetMethodID(cls,"sayHello","stephen");
if(mid != 0){
cout<<"Invoke method successfully!" << endl;
}
jvm->DestroyJavaVM();
return 0;
}
Here is java code:
public class Test
{
public static void sayHello(String s){
System.out.print("hello I am" + s + "\n");
}
}
I add the include path of " jdk/include; jdk/include/darwin" the project, also I add lib path of " jdk/jre/lib/server" to the project to get the libjvm.dylib. The c++ standard library of my project is libstdc++(gnu c++ standard library.
But I can't solve this problem as expected.
Take a look here for a sample code where JVM library is linked with your project:
https://github.com/mkowsiak/jnicookbook/tree/master/recipes/recipeNo028
Take a look at Makefile. Especially, here:
main: recipeNo028_main.o
ld -o lib/recipeNo028_main -L${JAVA_HOME}/jre/lib/server/ \
-ljvm \
$(MAC_OS_FLAGS) \
lib/recipeNo028_main.o
where jvm lib is linked with the code, and here:
CC=llvm-gcc
MAC_OS_FLAGS=-rpath ${JAVA_HOME}/jre/lib/server -L/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk -demangle -dynamic -arch x86_64 -macosx_version_min 10.12.0 -lSystem
where all required libs are added to your code as well. It should work. Try to compile sample code. You can find more samples here: http://jnicookbook.owsiak.org
Update
How to use arbitrary JDK version for compilation.
First, take a look at all installations you have
/usr/libexec/java_home -V
This will produce something like this
/usr/libexec/java_home -V
Matching Java Virtual Machines (4):
9, x86_64: "Java SE 9" /Library/Java/JavaVirtualMachines/jdk-9.jdk/Contents/Home
1.8.0_144, x86_64: "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home
1.8.0_111, x86_64: "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_111.jdk/Contents/Home
1.7.0_80, x86_64: "Java SE 7" /Library/Java/JavaVirtualMachines/jdk1.7.0_80.jdk/Contents/Home
Then, before running make, simply set JAVA_HOME to whatever you like
export JAVA_HOME=$(/usr/libexec/java_home -v 9)
Now, your code will use version that you have chosen.

First leveldb c++ sample code failed to link: what's the error indicating and how to fix it?

I've just installed level db on my mac with brew install leveldb, and I've got this sample code:
#include <assert.h>
#include <string.h>
#include <leveldb/db.h>
#include <iostream>
int main(){
leveldb::DB* db;
leveldb::Options options;
options.create_if_missing = true;
leveldb::Status status = leveldb::DB::Open(options,"/tmp/testdb", &db);
assert(status.ok());
//write key1,value1
std::string key="key";
std::string value = "value";
status = db->Put(leveldb::WriteOptions(), key,value);
assert(status.ok());
status = db->Get(leveldb::ReadOptions(), key, &value);
assert(status.ok());
std::cout<<value<<std::endl;
std::string key2 = "key2";
//move the value under key to key2
status = db->Put(leveldb::WriteOptions(),key2,value);
assert(status.ok());
status = db->Delete(leveldb::WriteOptions(), key);
assert(status.ok());
status = db->Get(leveldb::ReadOptions(),key2, &value);
assert(status.ok());
std::cout<<key2<<"==="<<value<<std::endl;
status = db->Get(leveldb::ReadOptions(),key, &value);
if(!status.ok()) std::cerr<<key<<" "<<status.ToString()<<std::endl;
else std::cout<<key<<"==="<<value<<std::endl;
delete db;
return 0;
}
I tried to compile and link it:
$ ls /usr/local/lib/libleveldb.a
/usr/local/lib/libleveldb.a
$ clang++ -o test test.cpp /usr/local/lib/libleveldb.a -lpthread -Iinclude
Undefined symbols for architecture x86_64:
"snappy::RawCompress(char const*, unsigned long, char*, unsigned long*)", referenced from:
leveldb::TableBuilder::WriteBlock(leveldb::BlockBuilder*, leveldb::BlockHandle*) in libleveldb.a(table_builder.o)
"snappy::RawUncompress(char const*, unsigned long, char*)", referenced from:
leveldb::ReadBlock(leveldb::RandomAccessFile*, leveldb::ReadOptions const&, leveldb::BlockHandle const&, leveldb::BlockContents*) in libleveldb.a(format.o)
"snappy::MaxCompressedLength(unsigned long)", referenced from:
leveldb::TableBuilder::WriteBlock(leveldb::BlockBuilder*, leveldb::BlockHandle*) in libleveldb.a(table_builder.o)
"snappy::GetUncompressedLength(char const*, unsigned long, unsigned long*)", referenced from:
leveldb::ReadBlock(leveldb::RandomAccessFile*, leveldb::ReadOptions const&, leveldb::BlockHandle const&, leveldb::BlockContents*) in libleveldb.a(format.o)
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Is there anything wrong with my installation and compilation?
It looks like you also need to be linking snappy. I assume this is because leveldb is also using that library. So you'll want to download that an install it as either a static or shared library and link it using -lsnappy. Make sure you place the linker flag to snappy after the linker flag for leveldb so it can fill in the symbols that leveldb is missing.

Compiling libzip on Mac: Undefined symbols for architecture x86_64

I've been learning C++ and have decided to try to create a simple file reader using libzip on archive files (e.g. Word).
I’ve recently installed libzip on my Macbook using brew but I seem to keep on getting the following issue whenever I try to compile a program that uses libzip:
Undefined symbols for architecture x86_64:
"_zip_fopen", referenced from:
_main in main-918bfa.o
"_zip_open", referenced from:
_main in main-918bfa.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [a.exe] Error 1
The command I use to compile:
g++ -g main.cpp -std=c++11 -I/usr/local/Cellar/libzip/0.11.2/include -I/usr/local/Cellar/libzip/0.11.2/lib/libzip/include -L/usr/local/Cellar/libzip/0.11.2/lib -o ../a.exe
main.cpp:
#include <iostream>
#include <fstream>
#include <zip.h>
#include <zlib.h>
using namespace std;
int numArgs = 2;
int main(int argc, char** argv){
// Parse command line arguments
if(argc != numArgs){
std::cout << "Incorrect number of arguments provided.\n";
std::cout << "Command line syntax: fileReader.exe inputFile" << endl;
exit(0);
}
// Try out libzip functionality
std::string inputDocument(argv[1]);
int err = 0;
zip* z = zip_open(inputDocument.c_str(), 0, &err);
if(z == NULL) {
printf("Could not read docx file. Error code: %d", err);
exit(-1);
}
zip_file* contentTypes = zip_fopen(z, "[Content_Types].xml", ZIP_FL_UNCHANGED);
exit(0);
}
Doesn't look like your including the libzip library in the compilation command. Try adding -lzip to your g++ command