leveldb error: undefined reference - c++

I download and build leveldb according to this README, and then properly add libleveldb.a and the include/leveldb directory (header files) to my environment path.
My test code:
/*leveldb_test.cpp
*/
#include <iostream>
#include <sstream>
#include <string>
#include <leveldb/db.h>
using namespace std;
int main(int argc, char** argv)
{
leveldb::DB* db;
leveldb::Options options;
options.create_if_missing = true;
leveldb::Status status = leveldb::DB::Open(options, "./testdb", &db);
assert(status.ok());
return 0;
}
And I compile it as:
g++ leveldb_test.cpp -o leveldb_test -lpthread -lleveldb -std=c++11
It produces "undefined reference" errors:
leveldb_test.o: In function `main':
leveldb_test.cpp:(.text+0x79): undefined reference to `leveldb::DB::Open(leveldb::Options const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, leveldb::DB**)'
leveldb_test.cpp:(.text+0x29e): undefined reference to `leveldb::Status::ToString[abi:cxx11]() const'
collect2: error: ld returned 1 exit status
What is the problem?
My environment: Linux version 3.10.0-327.el7.x86_64 - gcc version 4.8.3 20140911 - Red Hat 4.8.3-9
Thanks for any advise.

Related

Code with Opencv4 compiles but not links- not finding Mat and imshow

I am trying to create executable of following code taken from here:
#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/imgcodecs.hpp>
using namespace cv;
using namespace std;
// Driver code
int main(int argc, char** argv)
{
// Read the image file as // imread("default.jpg");
Mat image = imread("barchart.png", IMREAD_GRAYSCALE);
// Error Handling
if (image.empty()) {
cout << "Image File Not Found" << endl;
// wait for any key press
cin.get();
return -1;
}
// Show Image inside a window with the name provided
imshow("Window Name", image);
// Wait for any keystroke
waitKey(0);
return 0;
}
It compiles all right with command g++ showimage.cpp -c -I /usr/include/opencv4 and creates an showimage.o file
However, following command to link it gives error:
g++ showimage.o -l:libopencv_imgcodecs.a
...
... (lot of output)
/usr/bin/ld: showimage.o: in function `main':
showimage.cpp:(.text+0x83): undefined reference to `cv::Mat::empty() const'
/usr/bin/ld: showimage.cpp:(.text+0x10e): undefined reference to `cv::imshow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::_InputArray const&)'
/usr/bin/ld: showimage.cpp:(.text+0x13c): undefined reference to `cv::waitKey(int)'
/usr/bin/ld: showimage.cpp:(.text+0x150): undefined reference to `cv::Mat::~Mat()'
/usr/bin/ld: showimage.cpp:(.text+0x1c2): undefined reference to `cv::Mat::~Mat()'
collect2: error: ld returned 1 exit status
I also tried following option:
g++ showimage.o -L /usr/lib/x86_64-linux-gnu
g++ showimage.o -L libopencv
g++ showimage.o -L opencv
But none is not working.
Where is the problem and how can it be solved?

Having problems while trying to generate a parser in antlr4

I am trying to compile my own ANTLR4 parser in C++
#include "nibble.h"
using namespace antlr4;
using namespace nibble;
int main(int argc, const char* argv[]){
const std::string file_name = argv[1];
ANTLRInputStream input(file_name);
return 0;
}
but when I compile it by typing g++ nibblec.cpp -o nibblec -I /usr/local/include/antlr4-runtime I get
/usr/bin/ld: /tmp/ccqG8KT4.o: warning: relocation against '_ZTVN6antlr416ANTLRInputStreamE' in read-only section '.text._ZN6antlr416ANTLRInputStreamD2Ev[_ZN6antlr416ANTLRInputStreamD5Ev]'
/usr/bin/ld: /tmp/ccqG8KT4.o: in function 'main':
nibblec.cpp:(.text+0x64): undefined reference to 'antlr4::ANTLRInputStream::ANTLRInputStream(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
/usr/bin/ld: /tmp/ccqG8KT4.o: in function 'antlr4::ANTLRInputStream::~ANTLRInputStream()':
nibblec.cpp:(.text._ZN6antlr416ANTLRInputStreamD2Ev[_ZN6antlr416ANTLRInputStreamD5Ev]+0xf): undefined reference to `vtable for antlr4::ANTLRInputStream'
/usr/bin/ld: nibblec.cpp:(.text._ZN6antlr416ANTLRInputStreamD2Ev[_ZN6antlr416ANTLRInputStreamD5Ev]+0x42): undefined reference to 'antlr4::CharStream::~CharStream()'
/usr/bin/ld: warning: creating DT_TEXTREL in a PIE
collect2: error: ld returned 1 exit status
What is ld? And why I get all this warnings? Am I doing something wrong with ANTLR4?
EDIT: I am using ANTLR 4.9.1
You are not linking to ANTLR. Likely you need to add -lantlr to your compile flags (although the exact spelling might be different, and you may need to specify a library directory containing ANTLR).

Fail compile when include String

Here is a simple C++ program and I believe it should work. My code is reference from here.
Convert command line argument to string
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
#include <iostream>
//using namespace std;
int main(int argc, char *argv[]){
std::string boss;
//printf("PROGRAM NAME: %s\n", argv[0]);
if (argc > 1){
//printf("%s\n",&argv[1][0]);
printf("%s\n",argv[1]);
boss = argv[1];
}
}
When Compile it come out the problem:
/tmp/cctYLBpB.o: In function main':
testarg.cpp:(.text+0x1c): undefined reference tostd::basic_string, std::allocator >::basic_string()'
testarg.cpp:(.text+0x58): undefined reference to std::string::operator=(char const*)'
testarg.cpp:(.text+0x64): undefined reference tostd::basic_string, std::allocator >::~basic_string()'
testarg.cpp:(.text+0x78): undefined reference to std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()'
testarg.cpp:(.text+0x7c): undefined reference to__cxa_end_cleanup'
/tmp/cctYLBpB.o: In function __static_initialization_and_destruction_0(int, int)':
testarg.cpp:(.text+0xc0): undefined reference tostd::ios_base::Init::Init()'
testarg.cpp:(.text+0xe4): undefined reference to std::ios_base::Init::~Init()'
/tmp/cctYLBpB.o:(.ARM.extab+0x0): undefined reference to__gxx_personality_v0'
collect2: error: ld returned 1 exit status
Did you guys expert had seen any problem on it ? I have no idea ...
When you use gcc to build your program, the standard C++ libraries are not used by the linker. Use g++ instead. Add -Wall for good measure. You will get useful warning messages that will help you find problems at compile time rather than find them at run time.
g++ filename.cpp -Wall -o testc

Compile opencv3.1 with mingw

I want to compile the following code with mingw-w64.
#include <iostream>
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
Mat im = imread("lena.jpg", 1);
if (im.empty())
{
cout << "Cannot open image!" << endl;
return -1;
}
imshow("image", im);
waitKey(0);
return 0;
}
after following Getting started with OpenCV 2.4 and MinGW on Windows 7
I compile the code with g++ -I D:\opencv\build\include -L D:\opencv\build\x64\vc14\lib -lopencv_world310 .\loadimg.cpp
but it returns undefined reference
C:\Users\1409021\AppData\Local\Temp\cc8ZRPJP.o:loadimg.cpp:(.text+0x40): undefined reference to `cv::imread(cv::String const&, int)'
C:\Users\1409021\AppData\Local\Temp\cc8ZRPJP.o:loadimg.cpp:(.text+0xb7): undefined reference to `cv::imshow(cv::String const&, cv::_InputArray const&)'
C:\Users\1409021\AppData\Local\Temp\cc8ZRPJP.o:loadimg.cpp:(.text+0xd9): undefined reference to `cv::waitKey(int)'
C:\Users\1409021\AppData\Local\Temp\cc8ZRPJP.o:loadimg.cpp:(.text$_ZN2cv6StringC1EPKc[_ZN2cv6StringC1EPKc]+0x4a): undefined reference to `cv::String::allocate(unsigned long long)'
C:\Users\1409021\AppData\Local\Temp\cc8ZRPJP.o:loadimg.cpp:(.text$_ZN2cv6StringD1Ev[_ZN2cv6StringD1Ev]+0x11): undefined reference to `cv::String::deallocate()'
C:\Users\1409021\AppData\Local\Temp\cc8ZRPJP.o:loadimg.cpp:(.text$_ZN2cv3MatD1Ev[_ZN2cv3MatD1Ev]+0x36): undefined reference to `cv::fastFree(void*)'
C:\Users\1409021\AppData\Local\Temp\cc8ZRPJP.o:loadimg.cpp:(.text$_ZN2cv3Mat7releaseEv[_ZN2cv3Mat7releaseEv]+0x48): undefined reference to `cv::Mat::deallocate()'
the library is opencv3.1, and there is only one library opencv_world310
Any advise?
Thank you.
g++ -I D:\opencv\build\include -L D:\opencv\build\x64\vc14\lib -lopencv_world310 .\loadimg.cpp
is wrong.
g++ -I D:\opencv\build\include -L D:\opencv\build\x64\vc14\lib .\loadimg.cpp -lopencv_world310
is right. Explained here

can not compile opencv programs in ubuntu

I have wrote a simple program like this
#include <opencv2/opencv.hpp>
using namespace cv;
int main(int argc, const char* argv[])
{
Mat input = imread(argv[1], 0); //Load as grayscale
//~ FAST detector;
//~ vector<KeyPoint> keypoints;
//~ FAST(input, keypoints, 0.2);
// Add results to image and save.
//~ Mat output;
//~ drawKeypoints(input, keypoints, output);
namedWindow ("Image", CV_WINDOW_FREERATIO);
imshow("Image", input);
//~ imwrite(argv[2], output);
return 0;
}
Then compile program like this:
g++ `pkg-config --libs opencv` main.cpp
And here is output of g++:
/tmp/ccK1Sbrw.o: In function `main':
main.cpp:(.text+0x66): undefined reference to `cv::imread(std::string const&, int)'
main.cpp:(.text+0xc2): undefined reference to `cv::namedWindow(std::string const&, int)'
main.cpp:(.text+0xf6): undefined reference to `cv::_InputArray::_InputArray(cv::Mat const&)'
main.cpp:(.text+0x139): undefined reference to `cv::imshow(std::string const&, cv::_InputArray const&)'
/tmp/ccK1Sbrw.o: In function `cv::Mat::~Mat()':
main.cpp:(.text._ZN2cv3MatD2Ev[_ZN2cv3MatD5Ev]+0x39): undefined reference to `cv::fastFree(void*)'
/tmp/ccK1Sbrw.o: In function `cv::Mat::release()':
main.cpp:(.text._ZN2cv3Mat7releaseEv[_ZN2cv3Mat7releaseEv]+0x47): undefined reference to `cv::Mat::deallocate()'
collect2: error: ld returned 1 exit status
I have the library installed, I can see *.so and *.hpp files in their folders, and ld find them, but what does it complain about? there is nothing in .so files?!
Also, I do not have nonfree modules installed (I uset apt-get to install opencv), how can I get them? I need SIFT which is inside that module. Do I have to compile opencv myself?
It appears to me that you forgot to specify a module in
`pkg-config --libs MODULENAMEGOESHERE`