I get an error when trying to compile a single .cpp file
Main.cpp:
#include "opencv2/core/core_c.h"
#include "opencv2/core/core.hpp"
#include "opencv2/flann/miniflann.hpp"
#include "opencv2/imgproc/imgproc_c.h"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/video/video.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/calib3d/calib3d.hpp"
#include "opencv2/ml/ml.hpp"
#include "opencv2/highgui/highgui_c.h"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/contrib/contrib.hpp"
#include <opencv2/gpu/gpu.hpp>
#include <iostream>
int main(int argc, char* argv[])
{
cv::VideoCapture capture = cv::VideoCapture(argv[1]);
cv::gpu::GpuMat gpu_frame,gpu_frame_binary;
cv::Mat frame;
int threshold_value = 80;
int threshold_type = 0;
while (1)
{
capture >> frame;
gpu_frame.upload(frame);
if (!frame.data)
break;
cv::gpu::threshold(gpu_frame, gpu_frame_binary, threshold_value, 255, threshold_type);
}
return 0;
}
OpenCV built with these options:
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_V4L=ON -D WITH_GSTREAMER=ON -D WITH_OPENEXR=ON -D WITH_UNICAP=ON -D BUILD_PYTHON_SUPPORT=ON -D INSTALL_C_EXAMPLES=ON -D BUILD_EXAMPLES=ON -D ENABLE_SSE2=ON -D WITH_CUDA=ON ..
Command used to compile:
g++ -ggdb `pkg-config --cflags opencv` -o main main.cpp `pkg-config --libs opencv` -lopencv_gpu
And the error I get:
/usr/local/lib/libopencv_gpu.so: undefined reference to `cv::gpu::convertTo(cv::gpu::GpuMat const&, cv::gpu::GpuMat&, double, double, CUstream_st*)'
/usr/local/lib/libopencv_gpu.so: undefined reference to `cv::gpu::setTo(cv::gpu::GpuMat&, cv::Scalar_<double>, cv::gpu::GpuMat const&, CUstream_st*)'
/usr/local/lib/libopencv_gpu.so: undefined reference to `cv::gpu::setTo(cv::gpu::GpuMat&, cv::Scalar_<double>, CUstream_st*)'
collect2: error: ld returned 1 exit status
When I try to compile without the -lopencv_gpu option I get errors like:
main.cpp:61: undefined reference to `cv::gpu::threshold(cv::gpu::GpuMat const&, cv::gpu::GpuMat&, double, double, int, cv::gpu::Stream&)'
Any ideas what might be wrong?
P.S It may be important to note that I first built opencv without cuda, then decided that I wanted to try cuda and built it with cuda option on, and did sudo make install. I thought it would overwrite what was previously installed, but maybe something went wrong there?
Running on Fedora 20, with Nvidia card.
I encountered the same problem.
To solve it I just deleted libopencv_gpu.so.2.4 and libopencv_gpu.so.2.4.10 in /usr/local/lib/.
Related
I'm trying to compile my C++ program that uses the libraries HElib, OpenCV and PyTorch. I'm on Ubuntu 20.04. The entire code is:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <cstdint>
#include <memory>
#include <stdio.h>
#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <helib/helib.h>
#include <torch/torch.h>
#include "include/mnist/mnist_reader.hpp"
using namespace cv;
using namespace torch;
using namespace std;
using namespace mnist;
using namespace helib;
int main(int argc, char* argv[]) {
Tensor tensor = torch::rand({2, 3});
cout << tensor << endl;
Mat imageMat = Mat(image, true).reshape(0, 1).t();
return 0;
}
(where image is a 28x28 matrix).
I'm compiling it with the command (I know I should be using cmake but I'm new to C++ and for now I'd like to learn how to link libraries properly just from the command line):
g++ -g -O2 -std=c++17 -pthread -march=native prova.cpp -lopencv_core -lopencv_highgui -lopencv_imgcodecs -o prova -I/home/lulu/helib_install/helib_pack/include -I/usr/include/opencv4 -I/home/lulu/libtorch/include -I/home/lulu/libtorch/include/torch/csrc/api/include -I/home/lulu/libtorch/include/torch -L/home/lulu/helib_install/helib_pack/lib -L/usr/include/opencv4 -L/home/lulu/libtorch/lib -lhelib -lntl -lgmp -lm -ltorch -ltorch_cpu -lc10 -D_GLIBCXX_USE_CXX11_ABI=0
The error I get is the following:
/usr/bin/ld: /tmp/cc3mP2mc.o: in function `cv::Mat::Mat(int, int, int, void*, unsigned long)':
/usr/include/opencv4/opencv2/core/mat.inl.hpp:548: undefined reference to `cv::error(int, std::string const&, char const*, char const*, int)'
collect2: error: ld returned 1 exit status
Deleting the flag -D_GLIBCXX_USE_CXX11_ABI=0 doesn't help, I tried.
I also tried setting the variable LD_LIBRARY_PATH to /home/lulu/libtorch/lib, but neither that helps.
I think I'm linking all the libraries I need, what am I missing?
Thanks in advance for the help.
I found the answer, but I can't really explain with my little experience what I've done, I'll just illustrate the passages.
I've re-downloaded PyTorch from its website, selecting the libtorch-cxx11-abi-shared-with-deps version (the one compiled with -D_GLIBCXX_USE_CXX11_ABI=1).
Then I had to add to the compilation command the flag -Wl,-rpath,/path/to/pytorch/lib, because for some reason the compiler didn't find libc10 and libtorch_cpu, so the final command was:
g++ -g -O2 -std=c++17 \
-pthread \
-march=native \
-I/home/lulu/helib_install/helib_pack/include \
-I/usr/include/opencv4 \
-I/home/lulu/libtorch/include \
-I/home/lulu/libtorch/include/torch/csrc/api/include \
-/home/lulu/libtorch/include/torch \
-L/home/lulu/helib_install/helib_pack/lib \
-L/usr/include/opencv4 \
-L/home/lulu/libtorch/lib \
-Wl,-rpath,/home/lulu/libtorch/lib \
prova.cpp \
-lopencv_core -lopencv_highgui -lopencv_imgcodecs \
-lhelib -lntl -lgmp -lm \
-ltorch -ltorch_cpu -lc10 \
-o prova
I'm having this problem when I'm trying to start my computer camera.
I'm using this code
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
#include "opencv2/videoio.hpp"
using namespace cv;
using namespace std;
int main(int argc, char* argv[])
{
VideoCapture cap(0); // open the video camera no. 0
}
and it shows this error
enter code here
make all
Building target: video
Invoking: GCC C++ Linker
g++ -L/usr/local/lib -o "video" ./yes.o -lopencv_video
./yes.o: In function `main':
/home/allofthepower/eclipse-workspace/video/Debug/../yes.cpp:10: undefined reference to `cv::VideoCapture::VideoCapture(int)'
makefile:44: recipe for target 'video' failed
/home/allofthepower/eclipse-workspace/video/Debug/../yes.cpp:10: undefined reference to `cv::VideoCapture::~VideoCapture()'
collect2: error: ld returned 1 exit status
make: *** [video] Error 1
I'm new to Ubuntu and OpenCV, please help.
In my case I could fix this by adding -lopencv_videoio to the g++ compiler.
I Am on a Mac and I tried to compile OpenGL and GLUT but it does not work, why ?
Here is the command line that try to build (I use netbeans but this is the command line output window):
g++ -o dist/Debug/GNU-MacOSX/cppapplication_1 build/Debug/GNU-MacOSX/main.o -L/System/Library/Frameworks/OpenGL.framework/Libraries -lGL -lGLU
Undefined symbols for architecture x86_64:
"_glutInitWindowPosition", 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 my code:
#include <cstdlib>
#include <iostream>
#include <glut.h>
using namespace std;
/*
*
*/
int main(int argc, char** argv) {
glutInitWindowPosition(1,1);
return 0;
}
Here is my netbeans config
For information, there is no lib directory in /System/Library/Framework/GLUT.framework...
Try with these -framework GLUT -framework OpenGL
and possibly:
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
#include <OpenGL/glext.h>
#include <GLUT/glut.h>
I'm trying to build simple programm using ffmpeg
#include <stdio.h>
#include <stdlib.h>
#define __STDC_CONSTANT_MACROS
extern "C" {
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libavformat/avio.h>
#include <libavdevice/avdevice.h>
#include <libavutil/time.h>
}
#include <iostream>
int main( int argc, char* argv[] )
{
AVCodec *icodec;
AVFormatContext *ifcx = NULL;
AVInputFormat *ifmt;
AVCodecContext *iccx;
AVStream *ist;
AVStream *ost;
AVPacket pkt;
int i_index;
int64_t timenow, timestart;
int got_key_frame = 0;
AVFormatContext *ofcx;
const char *sProg = argv[ 0 ];
const char *sFileInput;
const char *sFileOutput;
int64_t bRunTime;
bRunTime = atoi( argv[ 2 ] ) * 1000000;
// Initialize library
av_log_set_level( AV_LOG_DEBUG );
av_register_all();
avcodec_register_all();
avformat_network_init();
avdevice_register_all();
And i get these errors
g++ -o rtsp3 -I/usr/include -I/usr/local/include rtsp3.cpp -lavformat -lavcodec -lavutil -lm -lz -lva -lpthread
/tmp/ccAXDgvi.o: In function main':
rtsp3.cpp:(.text+0x115): undefined reference toavdevice_register_all'
/usr/local/lib/libavformat.a(matroskadec.o): In function matroska_decode_buffer':
/home/user/projects/ffmpeg-git/ffmpeg/libavformat/matroskadec.c:1242: undefined reference toBZ2_bzDecompressInit'
/home/user/projects/ffmpeg-git/ffmpeg/libavformat/matroskadec.c:1257: undefined reference to BZ2_bzDecompress'
/home/user/projects/ffmpeg-git/ffmpeg/libavformat/matroskadec.c:1250: undefined reference toBZ2_bzDecompressEnd'
/home/user/projects/ffmpeg-git/ffmpeg/libavformat/matroskadec.c:1262: undefined reference to `BZ2_bzDecompressEnd'
collect2: error: ld returned 1 exit status
I have git version of ffmpeg and successfully compiled it and make install.
Try this, it worked for me:
gcc -o main.o main.c `pkg-config --cflags --libs libavformat libavutil`
You need to link with libavdevice, e.g. -lavdevice
Also, apparently, libbz2, e.g. -lbz2
I solved this by installing libbz2-dev
On Ubuntu: sudo apt-get install libbz2-dev
check if you have object files for libav format added in the path. You have added librarians properly in the makefile so problem shouldn't have come.
If you are using ubuntu, check if this is preset
/usr/lib/x86_64-linux-gnu/libavformat.so
If not, you need to install libavformat-dev
you can install it using "sudo apt-get install libavformat-dev"
I was facing the same issues and it got resolved after this.
I am trying to read in a file. I attempt to use ifstream in read() but I get the following error.
undefined reference to std::basic_ifstream<char,
std::char_traits<char> >::basic_ifstream()'
/home/ameya/Documents/computer_science/cs130B/prog2/prog2.cpp:24:
undefined reference tostd::basic_ifstream >::~basic_ifstream()' prog2.o:(.eh_frame+0x6b):
undefined reference to `__gxx_personality_v0' collect2: error: ld
returned 1 exit status make: * [prog2] Error 1
It says undefined reference to ifstream but I included that at the top so, why am I getting that error? Thanks in advance
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ifstream>
using namespace std;
class DepthMap{
public:
int merge(int numbers[]);
int mergehelper(int left[], int right[]);
void read();
};
int DepthMap::merge(int numbers[]){
return -43;
}
int DepthMap::mergehelper(int left[], int right[]){
return -43;
}
void DepthMap::read(){
ifstream inputFile;
}
int main(int argc, char* argv[])
{
DepthMap depth;
printf("Here");
return 0;
}
Here is my Makefile
CXX = g++
CXXFLAGS = -Wall
all: prog2
prog2: prog2.o
clean:
rm -f prog2
#include <fstream> as it should be.
Your g++ seems to be broken. Why do you not install clang?
Here are some suggested corrections for your makefile:
CXX = g++
CXXFLAGS = -Wall
prog2: prog2.o
g++ $(CXXFLAGS) prog2.o -o prog2
prog2.o: prog2.cpp
g++ $(CXXFLAGS) prog2.cpp -o prog2.o
clean:
rm -f prog2
I believe what you're looking for is
#include <fstream>
You are using gcc to compile and link rather than g++. By using the latter it will make sure you link against libstdc++.so without having to explicitly add it.
Seeing your Makefile confirms the above for linking.
Although you define CXX to be g++ that is only used for the implicit rule that compiles the source file. The implicit rule for linking falls back to CC which will probably be gcc. See the Catalogue of Implicit Rules for GNU make.