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.
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
everyone. I am using aarch64-linux-android-g++ to compile a binary.
The code is this:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int diag_wakelock_destroy();
int main(int argc, char **argv){
printf("hello world");
char mystring[] = "hi";
int result = diag_wakelock_destroy();
printf("finish! return: %d\n", result);
return 0;
}
The function diag_wakelock_destroy is defined in libdiag.so as we can see from IDA or just use "nm -D libdiag.so". But when I compiled it with this command
aarch64-linux-android-g++ --sysroot=$SYSROOT -g -fpie -pie -o exe_fine hi_diag.cpp libdiag.so liblog.so libcutils.so libc++.so libm.so libc.so ld-android.so -ldl -L.
I got this error.
/tmp/ccUb4ImK.o: In function `main':
/home/sam/Documents/test/lib-diag/hi_diag.cpp:13: undefined reference to
`diag_wakelock_destroy()'
collect2: error: ld returned 1 exit status
So it can't find that exported function! This is weird. Can anyone help me ? Thanks!
I am trying to compile SFML simple code:
#include <iostream>
#include <string>
#include <SFML/System.hpp>
#include <SFML/Network.hpp>
using namespace std;
int main(int argc, char* argv[])
{
sf::TcpSocket socket;
sf::Socket::Status status = socket.connect("127.0.0.1", 6000);
if (status != sf::Socket::Done)
{
// error...
}
return 0;
}
All libs and dependencies are installed:
sudo apt-get install libsfml-dev
sudo apt-get build-dep libsfml
I am using two methods:
g++ -c main.cpp
g++ -o main main.o -std=c++11 -lsfml-graphics -lsfml-window -lsfml-system
g++ -c main.cpp -I/home/x64joxer/SFML-2.4.0/include
g++ -o main main.o -L/home/x64joxer/SFML-2.4.0/lib -std=c++11 -lsfml-graphics -lsfml-window -lsfml-system
But I still have the same problem:
main.o: In function `main':
main.cpp:(.text+0x27): undefined reference to `sf::TcpSocket::TcpSocket()'
main.cpp:(.text+0x38): undefined reference to `sf::IpAddress::IpAddress(char const*)'
main.cpp:(.text+0x54): undefined reference to `sf::TcpSocket::connect(sf::IpAddress const&, unsigned short, sf::Time)'
main.o: In function `sf::TcpSocket::~TcpSocket()':
main.cpp:(.text._ZN2sf9TcpSocketD2Ev[_ZN2sf9TcpSocketD5Ev]+0x30): undefined reference to `sf::Socket::~Socket()'
main.cpp:(.text._ZN2sf9TcpSocketD2Ev[_ZN2sf9TcpSocketD5Ev]+0x56): undefined reference to `sf::Socket::~Socket()'
main.o:(.rodata._ZTIN2sf9TcpSocketE[_ZTIN2sf9TcpSocketE]+0x10): undefined reference to `typeinfo for sf::Socket'
collect2: error: ld returned 1 exit status
I read many tutorials and topics at the forum but I still do not know how too fix it.
My system is Kubntu 15.
Can anyone know how to fix it?
You are linking with graphics, system and window but not with network. Did you try adding -lsfml-network ?
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/.
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.