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?
Related
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).
I built the latest stable version of allegro5's source code following these steps
I have the following code (main.cpp):
#include <stdint.h>
#include <allegro5/allegro.h>
#include <allegro5/allegro_primitives.h>
int main()
{
al_init();
al_install_keyboard();
al_install_mouse();
al_init_primitives_addon();
al_set_new_display_flags(ALLEGRO_RESIZABLE);
ALLEGRO_DISPLAY* display = al_create_display(1280, 720);
al_set_window_title(display, "Allegro5 Window");
ALLEGRO_EVENT_QUEUE* queue = al_create_event_queue();
al_register_event_source(queue, al_get_display_event_source(display));
al_register_event_source(queue, al_get_keyboard_event_source());
al_register_event_source(queue, al_get_mouse_event_source());
bool running = true;
while (running)
{
ALLEGRO_EVENT ev;
while (al_get_next_event(queue, &ev))
{
if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
running = false;
if (ev.type == ALLEGRO_EVENT_DISPLAY_RESIZE)
{
al_acknowledge_resize(display);
}
}
al_clear_to_color(al_map_rgba_f(0.25, 0.25, 0.25, 1));
al_flip_display();
}
al_destroy_event_queue(queue);
al_destroy_display(display);
return 0;
}
... And I am trying to build this source file using the following command:
g++ -g $(pkg-config --libs --static allegro-static-5 allegro_primitives-static-5) main.cpp -o main
Even though pkg-config detects everything, it still doesn't work. The output is:
/usr/bin/ld: /tmp/ccmxycjk.o: in function `main':
pwd/main.cpp:7: undefined reference to `al_install_system'
/usr/bin/ld: pwd/main.cpp:8: undefined reference to `al_install_keyboard'
/usr/bin/ld: pwd/main.cpp:9: undefined reference to `al_install_mouse'
/usr/bin/ld: pwd/main.cpp:10: undefined reference to `al_init_primitives_addon'
/usr/bin/ld: pwd/main.cpp:11: undefined reference to `al_set_new_display_flags'
/usr/bin/ld: pwd/main.cpp:12: undefined reference to `al_create_display'
/usr/bin/ld: pwd/main.cpp:13: undefined reference to `al_set_window_title'
/usr/bin/ld: pwd/main.cpp:14: undefined reference to `al_create_event_queue'
/usr/bin/ld: pwd/main.cpp:15: undefined reference to `al_get_display_event_source'
/usr/bin/ld: pwd/main.cpp:15: undefined reference to `al_register_event_source'
/usr/bin/ld: pwd/main.cpp:16: undefined reference to `al_get_keyboard_event_source'
/usr/bin/ld: pwd/main.cpp:16: undefined reference to `al_register_event_source'
/usr/bin/ld: pwd/main.cpp:17: undefined reference to `al_get_mouse_event_source'
/usr/bin/ld: pwd/main.cpp:17: undefined reference to `al_register_event_source'
/usr/bin/ld: pwd/main.cpp:23: undefined reference to `al_get_next_event'
/usr/bin/ld: pwd/main.cpp:29: undefined reference to `al_acknowledge_resize'
/usr/bin/ld: pwd/main.cpp:33: undefined reference to `al_map_rgba_f'
/usr/bin/ld: pwd/main.cpp:33: undefined reference to `al_clear_to_color'
/usr/bin/ld: pwd/main.cpp:34: undefined reference to `al_flip_display'
/usr/bin/ld: pwd/main.cpp:37: undefined reference to `al_destroy_event_queue'
/usr/bin/ld: pwd/main.cpp:38: undefined reference to `al_destroy_display'
OBS: PWD = project path. Running up-to-date Debian 10
I've tried compiling with '-l:', adding all the libs, etc. Nothing would work. Any ideas?
Before you #include <allegro5/...>, add:
#define ALLEGRO_STATICLINK
You may need to link with additional libraries used by Allegro as well, such as OpenGL32.lib (on Windows), to resolve OpenGL functions.
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.
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
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`