Compile leveldb c++ program in linux error? - c++

I have install leveldb in my home directory ~/local like this.
[~/temp/leveldb-1.15.0] $ make
[~/temp/leveldb-1.15.0] $ cp -av libleveldb.* $HOME/local/lib/
[~/temp/leveldb-1.15.0] $ cp -av include/leveldb $HOME/local/include/
My c++ program like this:
#include <assert.h>
#include <iostream>
#include "leveldb/db.h"
using namespace std;
int main(int argc,char * argv[])
{
leveldb::DB* db;
leveldb::Options options;
options.create_if_missing = true;
std::string dbpath = "tdb";
leveldb::Status status = leveldb::DB::Open(options, dbpath, &db);
assert(status.ok());
std::string key1 = "grz";
std::string key2 = "grz-rt#63.com";
cout<<"Open db OK"<<std::endl;
std::string value;
leveldb::Status s ;
s = db->Put(leveldb::WriteOptions(), key1, key2);/*key1和key2作为一对key-value对插入*/
s = db->Get(leveldb::ReadOptions(), key1, &value);/*根据key返回对应的value值*/
cout<<value<<std::endl;
delete db;/*删除数据库*/
return 0;
}
I compile this C++ program like this:
g++ -o Main Main.cpp ~/local/lib/libleveldb.a -lpthread -I ~/local/include/
But I get the error like this:
/public/home/kli/local/lib/libleveldb.a(table_builder.o): In function `leveldb::TableBuilder::WriteBlock(leveldb::BlockBuilder*, leveldb::BlockHandle*)':
table_builder.cc:(.text+0x678): undefined reference to `snappy::MaxCompressedLength(unsigned long)'
table_builder.cc:(.text+0x6b2): undefined reference to `snappy::RawCompress(char const*, unsigned long, char*, unsigned long*)'
/public/home/kli/local/lib/libleveldb.a(format.o): In function `leveldb::ReadBlock(leveldb::RandomAccessFile*, leveldb::ReadOptions const&, leveldb::BlockHandle const&, leveldb::BlockContents*)':
format.cc:(.text+0x5de): undefined reference to `snappy::GetUncompressedLength(char const*, unsigned long, unsigned long*)'
format.cc:(.text+0x64e): undefined reference to `snappy::RawUncompress(char const*, unsigned long, char*)'
collect2: ld returned 1 exit status
I don't know what's wrong.
I am new to Linux. Thank you very much!

libleveldb.a misses Snappy when being linked which would be probably in libsnappy.a in the same directory.

Looks like the Makefile is incomplete.
With the current install, you need to edit the Makefile to link against snappy and to include -L/usr/local/lib instead of -L/usr/local/include.
(Will post pull request later)

Related

error while using occi for writing a udf for oracle db

I am trying to compile a cpp code using the header occi.h the code i am using is
`
#include <iostream>
#include <occi.h>
using namespace std;
int main(){
oracle::occi::Environment* environment;
oracle::occi::Connection *con;
oracle::occi::Statement* stmt;
oracle::occi::ResultSet* res;
try
{
environment = oracle::occi::Environment::createEnvironment(oracle::occi::Environment::DEFAULT);
con = environment->createConnection("sys", "root","xe");
stmt = con->createStatement("select * from customers");
res = stmt->executeQuery();
while (res->next())
std::cout<<res->getInt(1)<<" "<<res->getString(2)<<std::endl;
stmt->closeResultSet(res);
con->terminateStatement(stmt);
environment->terminateConnection(con);
}catch(oracle::occi::SQLException &e){
std::cout<<e.what();
}
return 0;
}`
i am getting the following errors when i run the following command :-
g++ -I /root/instantclient_11_2/sdk/include -L $ORACLE_HOME/lib:/root/Shared/instantclient_11_2_lib/ -L $ORACLE_HOME/rdbms/lib cpp_db_conn.cpp
error:-
/tmp/ccjcKsxz.o: In function main':
udf.cpp:(.text+0x26): undefined reference tooracle::occi::Environment::createEnvironment(oracle::occi::Environment::Mode, void*, void* ()(void, unsigned long), void* ()(void, void*, unsigned long), void ()(void, void*))'
/tmp/ccjcKsxz.o:(.gcc_except_table+0x80): undefined reference to `typeinfo for oracle::occi::SQLException'
collect2: ld returned 1 exit status
oracle::occi::Connection|why do you have a space here???|*|and not here???|con;
I can tell you my case.
Obviously, It is a bit-mismatch problem.
My environment is a 'Microsoft SDK v7.0' with 64bit compiler as a default
Unfortunately, my oracle client was a 32-bit, which version is instantclient-basic-nt-12.1.0.2.0.
I've got the same error with yours.
So, I change my compiler to 32-bit.
setenv /x86
Then, all problem was gone.
Have a good day.

problems with adding library to my project

I have created a project with intention of using hunspell functions in it. I am working in Ubuntu. I installed and compiled hunspell library and linked it with g++ -o wc.exe -lhunspell-1.6 wordcheck.cxx and everything seemed ok. But when I tried to compile and launch my project I got some errors.
#include <iostream>
#include "hunspell/hunspell.hxx"
using namespace std;
int main(int argc,char** argv)
{
FILE* lst=fopen("wordlist.txt","r");
if(!lst)
{
cerr<<"Can not open file\n";
return 1;
}
Hunspell* hs=new Hunspell(argv[1],argv[2]);
delete hs;
return 0;
}
and the errors were:
/home/alex2/Документы/bO/wordcheck.cxx:14: undefined reference to Hunspell::Hunspell(char const*, char const*, char const*)'
/home/alex2/Документы/bO/wordcheck.cxx:15: undefined reference toHunspell::~Hunspell()'
collect2: error: ld returned 1 exit status
atus
I have no idea what is wrong. I tried using
Hunspell* hs=new Hunspell();
and got that there is a candidate for it that requires three parameters:
/usr/local/include/hunspell/hunspell.hxx:115:3: note: candidate: Hunspell::Hunspell(const char*, const char*, const char*)
Hunspell(const char* affpath, const char* dpath, const char* key = NULL);
The only difference is char const* and const char* but I always thought it is the same thing. The entire project is similar to the example file hunspell provides and I have no idea what am I doing wrong and why my thing is not working.

g++ undefined reference to libscrypt_scrypt (linker parameterization not the issue?)

Here's my includes:
#include <iostream>
#include <string>
#include <cstring>
#include <stdio.h> //for snprintf
#include <libscrypt.h>
#include <sqlite3.h>
#include "generator.h" //has no dependencies
And here's my g++ 4.2.1 command with compilation errors:
# g++ -I/usr/local/include -L /usr/local/lib test.cpp generator.cpp -o test -lscrypt -lsqlite3
/tmp//cctxmw4C.o: In function `insertUser()':
test.cpp:(.text+0x607): undefined reference to `libscrypt_salt_gen(unsigned char*, unsigned long)'
test.cpp:(.text+0x6c3): undefined reference to `libscrypt_scrypt(unsigned char const*, unsigned long, unsigned char const*, unsigned long, unsigned long long, unsigned int, unsigned int, unsigned char*, unsigned long)'
collect2: ld returned 1 exit status
Similar C code compiles fine with GCC. I do not believe that the order of the parameters is the problem -- I have tried multiple variations, including moving the -I, -L, and both -l flags. Both scrypt and sqlite3 are C libraries, and sqlite3 throws no errors despite having the header and shared library in -I and -L respectively.
Look at the solution here.
Using C Libraries for C++ Programs
extern "C"{
void c_function_prototype();
}
or
extern "C" void c_function_prototype();
If you have it see "The C++ Programming Language (Stoustrup) 4th Edition" p429

Linking error with boost process (and other boost libs)

Here's my code. I'm just testing Boost::process so I'll be able to use it if/when I need to. I don't know why I'm getting the linking error that I am getting. I'm a rather novice C++ programmer. I know the concepts, but I make frequent errors in practice and am bad at debugging. I appreciate any help I can get with this.
#include<iostream>
#include<boost/process.hpp>
#include<boost/iostreams/device/file_descriptor.hpp>
namespace bp = ::boost::process;
namespace bpi = ::boost::process::initializers;
namespace bio = ::boost::iostreams;
int main(int argc, char *argv[])
{
bp::pipe p = bp::create_pipe();
bio::file_descriptor_sink sink(p.sink, bio::close_handle);
bp::execute(
bpi::run_exe("/usr/bin/ls"),
bpi::bind_stdout(sink)
);
return(0);
}
And here is my error…
/tmp/cc7cmrV8.o: In function `main':
test.cpp:(.text+0x2b): undefined reference to `boost::iostreams::file_descriptor_sink::file_descriptor_sink(int, boost::iostreams::file_descriptor_flags)'
/tmp/cc7cmrV8.o: In function `boost::process::posix::initializers::bind_stdout::bind_stdout(boost::iostreams::file_descriptor_sink const&)':
test.cpp:(.text._ZN5boost7process5posix12initializers11bind_stdoutC2ERKNS_9iostreams20file_descriptor_sinkE[_ZN5boost7process5posix12initializers11bind_stdoutC5ERKNS_9iostreams20file_descriptor_sinkE]+0x2b): undefined reference to `boost::iostreams::file_descriptor_sink::file_descriptor_sink(boost::iostreams::file_descriptor_sink const&)'
/tmp/cc7cmrV8.o: In function `void boost::process::posix::initializers::bind_stdout::on_exec_setup<boost::process::posix::executor>(boost::process::posix::executor&) const':
test.cpp:(.text._ZNK5boost7process5posix12initializers11bind_stdout13on_exec_setupINS1_8executorEEEvRT_[_ZNK5boost7process5posix12initializers11bind_stdout13on_exec_setupINS1_8executorEEEvRT_]+0x18): undefined reference to `boost::iostreams::file_descriptor::handle() const'
collect2: error: ld returned 1 exit status
Platform: Linux 64-bit
Boost: 1.55 (installed via pacman)
Boost::process: 0.5
Compile command: g++ -Wall test.cpp -o spegh.elf -lboost_system
A simple search threw me at -This-.
Seeing you posted your compile command, I'm guessing you are simply missing -lboost_iostreams in your linker settings.

How to link a SunriseDD C library with a C++ app?

I was looking for a lock-free hash table implementation in C or C++ and found one: SunriseDD. That was the good news :) For a few days I am trying to get this up and running but with no luck. I can compile downloaded sources as a static library using GCC C compiler. And I get libSunriseDD.a archive.
So far so good :D
I have created a simple C++ application to wrap the SunriseDD C implementation of the hash table. Here is the main.cpp:
#include <iostream>
#include <dd_data_dictionary.h>
using namespace std;
struct Node{
int oid;
int x, y;
};
struct HashTableEntry{
Node *node;
int idx;
};
void copyFunction(void * source, void * target){
target = source;
}
template<typename T>class HashTableDD{
private:
dd_dictionary objects;
public:
HashTableDD() {
objects = dd_new_dictionary(); // creating dictionary
dd_set_object_copy_function_for_dictionary(objects, copyFunction); // setting copy function
}
~HashTableDD() {
dd_dispose_dictionary(objects);
}
bool insert(T obj, long key){
return dd_add_object_for_key(objects, (char *)key, (void*)obj);
}
bool remove(long key){
return dd_remove_object_for_key(objects, (char *)key);
}
T find(long key) {
return (T)dd_object_for_key(objects, (char *)key, false);
}
};
int main(int argc, char ** argv){
HashTableDD<HashTableEntry*> *ht = new HashTableDD<HashTableEntry*>();
HashTableEntry* hte = new HashTableEntry;
hte->idx = 1;
hte->node = NULL;
ht->insert(hte, 1);
HashTableEntry* hte2 = new HashTableEntry;
hte2->idx = 2;
Node n;
n.oid = 10; n.x = 10; n.y = 10;
hte2->node = &n;
ht->insert(hte2, 2);
HashTableEntry* ret = ht->find(1);
if(ret != NULL){
cout << "hte. idx: " << ret->idx << " node: " << ret->node << endl;
}
ht->remove(1);
ht->remove(2);
delete hte; delete hte2; delete ht;
return 0;
}
But the linker is not happy about that:
:~/Desktop/HashTable$ make
Building file: main.cpp
Invoking: GCC C++ Compiler
g++ -ISunriseDD/build/../ -O0 -g3 -m64 -c -o"build/main.o" "main.cpp"
Finished building: main.cpp
Building target: build/HashTableDD
Invoking: GCC C++ Linker
g++ -LSunriseDD/build/ -lSunriseDD -o build/HashTableDD build/main.o
build/main.o: In function `HashTableDD':
/home/robertas/Desktop/HashTable/main.cpp:32: undefined reference to `dd_new_dictionary()'
/home/robertas/Desktop/HashTable/main.cpp:33: undefined reference to `dd_set_object_copy_function_for_dictionary(void*, void (*)(void*, void*))'
build/main.o: In function `HashTableDD<HashTableEntry*>::insert(HashTableEntry*, long)':
/home/robertas/Desktop/HashTable/main.cpp:40: undefined reference to `dd_add_object_for_key(void*, char const*, void*)'
build/main.o: In function `HashTableDD<HashTableEntry*>::find(long)':
/home/robertas/Desktop/HashTable/main.cpp:47: undefined reference to `dd_object_for_key(void*, char const*, bool)'
build/main.o: In function `HashTableDD<HashTableEntry*>::remove(long)':
/home/robertas/Desktop/HashTable/main.cpp:44: undefined reference to `dd_remove_object_for_key(void*, char const*)'
build/main.o: In function `~HashTableDD':
/home/robertas/Desktop/HashTable/main.cpp:36: undefined reference to `dd_dispose_dictionary(void*)'
collect2: ld returned 1 exit status
make: *** [build/HashTableDD] Error 1
Any ideas what am I doing wrong here? Do I link the SunriseDD library incorrectly?
BTW I have the following directory listing where my main.cpp resides:
+HashTable
|--+build
| |--main.o
|---main.cpp
|--+SunriseDD
|--+build
| |--libSunriseDD.a
| |--other object files
|--headers and source files of SunriseDD
Thanks for the help!
Put -lSunriseDD last on the linker line. The linker processes arguments left to right, and searches libraries for currently undefined symbols when it processes a static library.
Also, unless the library is C++-aware, wrap includes in extern "C".
extern "C" {
#include <dd_data_dictionary.h>
}