I downloaded the ffmpeg from git, and make the libs by sources. Created main.c as below and put the ffmpeg libs in the same folder as main.c, (my system is ubuntu 15.10, gcc version 5.2.1)
#include <stdio.h>
void av_register_all(void);
int main() {
printf("abc\n");
av_register_all();
return 0;
}
After I issued gcc main.c -L. -lavformat -lswscale -lavcodec -lswscale -lavutil -lavdevice -lavfilter, I got a lot of (nearly 1000) undefined reference errors:
...
/home/arton/sources/ffmpeg/libavcodec/vorbisdec.c:868: undefined reference to `atan'
/home/arton/sources/ffmpeg/libavcodec/vorbisdec.c:868: undefined reference to `atan'
/home/arton/sources/ffmpeg/libavcodec/vorbisdec.c:869: undefined reference to `atan'
/home/arton/sources/ffmpeg/libavcodec/vorbisdec.c:869: undefined reference to `atan'
/home/arton/sources/ffmpeg/libavcodec/vorbisdec.c:869: undefined reference to `floor'
...
/home/arton/sources is where the ffmpeg sources at, I have no idea why it report the sources path of ffmpeg and why the link fails. Any hint is appreciated. Thanks!
atan and floor are included in lm.
Do you link with lm?
You might want to read this.
Related
I'm using the following CMake file to build an opencv project using the command cmake followed by make.
cmake_minimum_required(VERSION 2.8)
project(t)
#set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} /home/keiths/opencv/opencv-2.4.11_build/build)
SET(CMAKE_C_COMPILER mpicc)
SET(CMAKE_CXX_COMPILER mpicxx)
#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
include_directories(${OpenCV_INCLUDE_DIRS})
find_package(OpenCV REQUIRED)
find_package(MPI REQUIRED)
add_executable(t t.cpp)
target_link_libraries(t ${OpenCV_LIBS} ${OpenCV_LIBRARIES} opencv_core opencv_highgui opencv_calib3d opencv_contrib opencv_core opencv_features2d opencv_flann opencv_gpu opencv_highgui opencv_imgproc opencv_legacy opencv_ml opencv_nonfree opencv_objdetect opencv_ocl opencv_photo opencv_stitching opencv_superres opencv_ts opencv_video opencv_videostab rt pthread m dl)
MESSAGE(${OpenCV_LIBS})
MESSAGE(${OpenCV_INCLUDE_DIRS})
cmake completes fine, but make gives me the following error:
CMakeFiles/t.dir/t.cpp.o: In function main': t.cpp:(.text+0x56):
undefined reference to
cv::namedWindow(std::__cxx11::basic_string, std::allocator > const&, int)' collect2:
error: ld returned 1 exit status make[2]: * [t] Error 1 make[1]: *
[CMakeFiles/t.dir/all] Error 2 make: *** [all] Error 2
I've tried running the following command
g++ t.cpp $(pkg-config --libs opencv --cflags)
but get the following error
/home/keiths/tmp/ccWFcaXH.o: In function main': t.cpp:(.text+0x56):
undefined reference to
cv::namedWindow(std::__cxx11::basic_string, std::allocator > const&, int)' collect2:
error: ld returned 1 exit status
I'm running the following simple code
#include "opencv2/opencv.hpp"
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
//#include <mpi.h>
using namespace cv;
int main(int argc, char **argv) {
Mat image;
namedWindow( "Display window", WINDOW_AUTOSIZE );
return 0;
}
I've tried as many recommendations as I can without success. Platform is CentOS and I'm a limited user (non-root) with my locally installed (more recent) versions of g++/gcc. I get the correct list of libraries for the pkg-config command and have configured library paths as well as the PATH to my local opencv folder
Just to add a very important point, I tried compiling the same code while explicitly specifying the stock g++ compiler (/usr/bin/g++) and all went well. So it seems that the problem would be with my local g++ compiler at '/home/keiths/lbin'. I need this latest version however for the c++11 capability (the old is 4.4.7 and it would take ages for the Sysadmin to upgrade it)
I had built my OpenCV library using the old g++ compiler (in the system bin folder), prior to building the latest g++. Rebuilding the OpenCV library using the new g++ compiler did it for me. Out of curiosity, I tried building the OpenCV example using the old compiler and it returned many 'undefined reference' lines, but I'm happy to swap problems since I don't need it anyway!
I am using XAMPP and tried with a clean install of Ubuntu 12.04 in a VM, the results are the same.
The program bellow compile and link easily with:
gcc c_mysql.c -l mysqlclient -o c_mysql
That is, produces the default dynamically liked program.
But getting a statically linked program is not so easy:
I tried successively the following ways after extensive googling. Any help will be
really appreciated:
1) gcc c_mysql1.c libmysqlclient.a -o c_mysql
[Library libmysqlclient.a not found]
2) gcc c_mysql1.c /usr/lib/i386-linux-gnu/libmysqlclient.a -o c_mysql
[Many undefined references]
3) gcc c_mysql1.c /usr/lib/i386-linux-gnu/libmysqlclient.a /usr/lib/i386-linux-gnu/libdl.a
[Many but less undefined references]
4) gcc c_mysql1.c /usr/lib/i386-linux-gnu/libmysqlclient.a /usr/lib/i386-linux-gnu/libdl.a -lpthread -lz -o c_mysql
[The minimum undefined references I could get]
/usr/lib/i386-linux-gnu/libmysqlclient.a(client_plugin.c.o): In function `mysql_load_plugin_v':
(.text+0x524): warning: Using 'dlopen' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/usr/lib/i386-linux-gnu/libmysqlclient.a(dh.cpp.o): In function `TaoCrypt::DH::GeneratePrivate(TaoCrypt::RandomNumberGenerator&, unsigned char*)':
(.text+0x42): undefined reference to `pow'
/usr/lib/i386-linux-gnu/libmysqlclient.a(dh.cpp.o): In function `TaoCrypt::DH::GeneratePrivate(TaoCrypt::RandomNumberGenerator&, unsigned char*)':
(.text+0x50): undefined reference to `log'
/usr/lib/i386-linux-gnu/libmysqlclient.a(dh.cpp.o): In function `TaoCrypt::DH::GeneratePrivate(TaoCrypt::RandomNumberGenerator&, unsigned char*)':
(.text+0x62): undefined reference to `pow'
/usr/lib/i386-linux-gnu/libdl.a(dlopen.o): In function `dlopen':
(.text+0x1b): undefined reference to `__dlopen'
/usr/lib/i386-linux-gnu/libdl.a(dlclose.o): In function `dlclose':
(.text+0x1): undefined reference to `__dlclose'
/usr/lib/i386-linux-gnu/libdl.a(dlsym.o): In function `dlsym':
(.text+0x1b): undefined reference to `__dlsym'
/usr/lib/i386-linux-gnu/libdl.a(dlerror.o): In function `dlerror':
(.text+0x1): undefined reference to `__dlerror'
collect2: ld returned 1 exit status
-static makes many more undefined references (-static-libgcc is even worst).
/usr/lib/i386-linux-gnu/libmysqlclient.a(client_plugin.c.o): In function `mysql_load_plugin_v':
(.text+0x524): warning: Using 'dlopen' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/usr/lib/i386-linux-gnu/libmysqlclient.a(mf_pack.c.o): In function `unpack_dirname':
(.text+0x653): warning: Using 'getpwnam' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/usr/lib/i386-linux-gnu/libmysqlclient.a(libmysql.c.o): In function `read_user_name':
(.text+0x2a91): warning: Using 'getpwuid' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/usr/lib/i386-linux-gnu/libmysqlclient.a(mf_pack.c.o): In function `unpack_dirname':
(.text+0x667): warning: Using 'endpwent' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/usr/lib/i386-linux-gnu/libmysqlclient.a(client.c.o): In function `mysql_real_connect':
(.text+0x47b6): warning: Using 'getaddrinfo' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/usr/lib/i386-linux-gnu/libmysqlclient.a(libmysql.c.o): In function `mysql_server_init':
(.text+0x27fa): warning: Using 'getservbyname' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/usr/lib/i386-linux-gnu/libmysqlclient.a(dh.cpp.o): In function `TaoCrypt::DH::GeneratePrivate(TaoCrypt::RandomNumberGenerator&, unsigned char*)':
(.text+0x42): undefined reference to `pow'
/usr/lib/i386-linux-gnu/libmysqlclient.a(dh.cpp.o): In function `TaoCrypt::DH::GeneratePrivate(TaoCrypt::RandomNumberGenerator&, unsigned char*)':
(.text+0x50): undefined reference to `log'
/usr/lib/i386-linux-gnu/libmysqlclient.a(dh.cpp.o): In function `TaoCrypt::DH::GeneratePrivate(TaoCrypt::RandomNumberGenerator&, unsigned char*)':
(.text+0x62): undefined reference to `pow'
/usr/lib/i386-linux-gnu/libmysqlclient.a(my_compress.c.o): In function `my_compress_alloc':
(.text+0x68): undefined reference to `compress'
/usr/lib/i386-linux-gnu/libmysqlclient.a(my_compress.c.o): In function `my_uncompress':
(.text+0x1cf): undefined reference to `uncompress'
collect2: ld returned 1 exit status
#include <mysql/mysql.h>
#include <stdio.h>
#include <string.h>
#include <pthread.h>
int main()
{
MYSQL mysql;
MYSQL_RES *res;
MYSQL_ROW row;
char *query = "select * from dbtablename;";
int t,r;
mysql_init(&mysql);
//if(!mysql_real_connect(&mysql,"localhost", "dbusername", "dbpassword", "dbname", port, NULL, 0)) The connection data is fake. If you, for a big miracle make it compile statically, the program will not connect.
if(!mysql_real_connect(&mysql,"192.231.182.73","PinData","YULYU7M", "DB084", 0, NULL, 0)){
printf("Error connecting to database:%s\n",mysql_error(&mysql));
}
else{
printf("Connected to the remote database........");
}
t=mysql_query(&mysql,query);
if(t)
{
printf("Error making query:%s\n",mysql_error(&mysql));
}
else
{
printf("Query made ....\n");
res = mysql_use_result(&mysql);
if(res)
{
for(r=0;r<=mysql_field_count(&mysql);r++)
{
row = mysql_fetch_row(res);
if(row<0) break;
for(t=0;t<mysql_num_fields(res);t++)
printf("%s ",row[t]);
printf("\n");
}
}
mysql_free_result(res);
}
mysql_close(&mysql);
return 0;
}
After many trial and error and google search, the answer is: To statically link libmysqlclient you have to use the following options:
-lpthread -lm -lz -ldl
This makes the linker find on some libraries.
That is:
gcc -static-libgcc c_mysql1.c /usr/lib/i386-linux-gnu/libmysqlclient.a -lpthread -lm -lz -ldl -o c_mysql
However the general question is still open: Given a static library StaticLib, how to know in advance which options and libraries to use.
I can not believe the answer would be " trial and error and google search"
I am trying to compile a single c++ source file test.cpp, which has a very simple code which demonstrates pthread_create(); pthread_cond_signal/pthread_cond_wait() functionality.
I have installed Mingw / Ansys on Windows XP where I am working. In the MingW prompt I do:
g++ -IC:/MinGW/include/ -lpthread test.cpp
//-IC:/MinGW/include to get pthread.h
//-LC:/MinGW/bin to get pthreadGC2.dll
The cpp includes pthread.h as:
#include <pthread.h>
But this gave me multiple linker undefined reference errors to all pthread library functions.
What am I doing wrong here. Is it possible to build a pthread code on MingW environment on windows or not?
How to resolve this errors?
C:\DOCUME~1\ADESHP~1\LOCALS~1\Temp\ccUQhu7D.o:pthread_cond.c:(.text+0x23): undefined reference to `_imp__pthread_mutex_lock'
C:\DOCUME~1\ADESHP~1\LOCALS~1\Temp\ccUQhu7D.o:pthread_cond.c:(.text+0x67): undefined reference to `_imp__pthread_cond_signal'
C:\DOCUME~1\ADESHP~1\LOCALS~1\Temp\ccUQhu7D.o:pthread_cond.c:(.text+0x75): undefined reference to `_imp__pthread_mutex_unlock'
C:\DOCUME~1\ADESHP~1\LOCALS~1\Temp\ccUQhu7D.o:pthread_cond.c:(.text+0x98): undefined reference to `_imp__pthread_exit'
C:\DOCUME~1\ADESHP~1\LOCALS~1\Temp\ccUQhu7D.o:pthread_cond.c:(.text+0xbc): undefined reference to `_imp__pthread_mutex_lock'
C:\DOCUME~1\ADESHP~1\LOCALS~1\Temp\ccUQhu7D.o:pthread_cond.c:(.text+0xe8): undefined reference to `_imp__pthread_cond_wait'
C:\DOCUME~1\ADESHP~1\LOCALS~1\Temp\ccUQhu7D.o:pthread_cond.c:(.text+0x10f): undefined reference to `_imp__pthread_mutex_unlock'
C:\DOCUME~1\ADESHP~1\LOCALS~1\Temp\ccUQhu7D.o:pthread_cond.c:(.text+0x135): undefined reference to `_imp__pthread_exit'
C:\DOCUME~1\ADESHP~1\LOCALS~1\Temp\ccUQhu7D.o:pthread_cond.c:(.text+0x153): undefined reference to `_imp__pthread_attr_init'
C:\DOCUME~1\ADESHP~1\LOCALS~1\Temp\ccUQhu7D.o:pthread_cond.c:(.text+0x169): undefined reference to `_imp__pthread_mutex_init'
C:\DOCUME~1\ADESHP~1\LOCALS~1\Temp\ccUQhu7D.o:pthread_cond.c:(.text+0x17f): undefined reference to `_imp__pthread_attr_setdetachstate'
C:\DOCUME~1\ADESHP~1\LOCALS~1\Temp\ccUQhu7D.o:pthread_cond.c:(.text+0x195): undefined reference to `_imp__pthread_cond_init'
C:\DOCUME~1\ADESHP~1\LOCALS~1\Temp\ccUQhu7D.o:pthread_cond.c:(.text+0x1bc): undefined reference to `_imp__pthread_create'
C:\DOCUME~1\ADESHP~1\LOCALS~1\Temp\ccUQhu7D.o:pthread_cond.c:(.text+0x1e3): undefined reference to `_imp__pthread_create'
C:\DOCUME~1\ADESHP~1\LOCALS~1\Temp\ccUQhu7D.o:pthread_cond.c:(.text+0x201): undefined reference to `_imp__pthread_join'
C:\DOCUME~1\ADESHP~1\LOCALS~1\Temp\ccUQhu7D.o:pthread_cond.c:(.text+0x21f): undefined reference to `_imp__pthread_join'
C:\DOCUME~1\ADESHP~1\LOCALS~1\Temp\ccUQhu7D.o:pthread_cond.c:(.text+0x239): undefined reference to `_imp__pthread_mutex_destroy'
C:\DOCUME~1\ADESHP~1\LOCALS~1\Temp\ccUQhu7D.o:pthread_cond.c:(.text+0x247): undefined reference to `_imp__pthread_cond_destroy'
C:\DOCUME~1\ADESHP~1\LOCALS~1\Temp\ccUQhu7D.o:pthread_cond.c:(.text+0x255): undefined reference to `_imp__pthread_attr_destroy'
C:\DOCUME~1\ADESHP~1\LOCALS~1\Temp\ccUQhu7D.o:pthread_cond.c:(.text+0x263): undefined reference to `_imp__pthread_exit'
collect2: ld returned 1 exit status
You need to specify the library on the gcc/g++ command line after the files that depend on the library. So try:
g++ -IC:/MinGW/include/ test.cpp -lpthread
I kicked myself when I stumbled on the answer (it's kind of a FAQ for libraries and gcc). For most gcc options order doesn't matter, but for libraries it's critical.
You should not have to specify the library path if the pthread library came with your MinGW distribution (as it seems is the case for you). Also, remember that the command line above will produce an a.exe executable; pass -o test.exe to avoid that.
If this is not out of place here, would like to know about the source and fix for an error I get when I am building OpenCV under Cygwin (On Windows 7 64 bit).
I have Cygwin installed with MingW- pthread pacgae installed in that cygwin installation.
I have downloaded OpenCV ver. 2.3.1 source package.
EDIT:
After some tweaks, and changes to build process OpenCV builds fine now.
But when I compile a simple test openCV code(C source code), as below it gives lot of linker errors of undefined references to many basic openCV functions. I tried setting LD_LIBRARY_PATH correctly but still errors persit:
cc -L/usr/local/lib -I/usr/local/include/opencv2 -/usr/local/include/opencv -lop encv_imgproc -lopencv_highgui -lopencv_video -lopencv_calib3d test1.c -o test_op encv
cc: unrecognized option '-/usr/local/include/opencv'
/tmp/ccfEqFK0.o:test1.c:(.text+0x9dd): undefined reference to `_cvFree_'
/tmp/ccfEqFK0.o:test1.c:(.text+0xa4e): undefined reference to `_cvFree_'
/tmp/ccfEqFK0.o:test1.c:(.text+0xb44): undefined reference to `_cvGetRows'
/tmp/ccfEqFK0.o:test1.c:(.text+0xb6f): undefined reference to `_cvGetCols'
/tmp/ccfEqFK0.o:test1.c:(.text+0xb82): undefined reference to `_cvReleaseMat'
/tmp/ccfEqFK0.o:test1.c:(.text+0xcd2): undefined reference to `_cvAddS'
/tmp/ccfEqFK0.o:test1.c:(.text+0x1137): undefined reference to `_cvRead'
/tmp/ccfEqFK0.o:test1.c:(.text+0x11e1): undefined reference to `_cvLoadImage'
/tmp/ccfEqFK0.o:test1.c:(.text+0x129d): undefined reference to `_cvCreateImage'
/tmp/ccfEqFK0.o:test1.c:(.text+0x12dd): undefined reference to `_cvResize'
/tmp/ccfEqFK0.o:test1.c:(.text+0x12f1): undefined reference to `_cvNamedWindow'
/tmp/ccfEqFK0.o:test1.c:(.text+0x1305): undefined reference to `_cvNamedWindow'
'
/tmp/ccfEqFK0.o:test1.c:(.text+0x13ad): undefined reference to `_cvDestroyWindow
... ... Many more undefined references.
'
collect2: ld returned 1 exit status
Makefile:10: recipe for target `test_opencv' failed
make: *** [test_opencv] Error 1
EDIT
Below message is still there, but it is not an error but a message during build process.
*Then while building OpenCV under cygwin, when I do make , it gives a error message
c++: unrecognized option '-pthread'
[ 36%] Building CXX object modules/highgui/CMakeFiles/opencv_highgui.dir/src/grfmt_sunras.o*
and for many other files i see the same error c++: unrecognized option '-pthread'
I would like to know if anyone has experience in building openCV under Cygwin , how to get OpenCV going under cygwin? . Now I know there is windows setup for OpenCV but that is not what is an option to me.
Your cmd line arguments has a few problems. It seems that it was not fully pasted in your question (some indentation problems suggests so).
One obvious mistake can be easily identified since CC is telling you about it: cc: unrecognized option '-/usr/local/include/opencv':
When adding headers directories in the command line, you must specify each of them with -I. Notice how you failed to do so with: -/usr/local/include/opencv. It should be -I/usr/local/include/opencv
You also seem to be missing -lopencv_core in the cmd-line
In the future, use pkg-config (if its properly configured) to help you include opencv headers and libraries:
cc test.c -o test_op `pkg-config --cflags --libs opencv` -lop
Thanks for pointers:
What I found was the problem was as follows:
In my Makefile of my openCV test app I was linking with the import libraries (*.a). Which was the problem.
Problematic Makefile was linking against files
-l:/usr/local/lib/libopencv_core.dll.a ...etc...
I changed that to link with actual dll libs, then it worked(linked) fine, w/o any errors as above.
Changes Makefile looks:
LIBS = -l:/usr/local/bin/cygopencv_core-2.3.dll -l:/usr/local/bin/cygopencv_imgproc-2.3.dll -l:/usr/local/bin/cygopencv_highgui-2.3.dll -l:/usr/local/bin/cygopencv_video-2.3.dll -l:/usr/local/bin/cygopencv_calib3d-2.3.dll
I have been coding in python before and included the library of openCV without any problem. Now, I want to code in C++ so I downloaded eclipse and openCV libraries and included their path in the includes from eclipse ..
I have simple example of openCV and I am trying to run it, but I get this error -->
**** Build of configuration Debug for project Example ****
make all
Building file: ../Test.cpp
Invoking: GCC C++ Compiler
g++ -I/usr/local/include/opencv -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"Test.d" -MT"Test.d" -o "Test.o" "../Test.cpp"
Finished building: ../Test.cpp
Building target: Example
Invoking: GCC C++ Linker
g++ -o "Example" ./Test.o
/usr/lib/x86_64-linux-gnu/gcc/x86_64-linux-gnu/4.5.2/../../../crt1.o: In function _start':
(.text+0x20): undefined reference tomain'
./Test.o: In function gh::main()':
/home/shamma/workspace/Example/Debug/../Test.cpp:16: undefined reference tocvCreateImage'
/home/shamma/workspace/Example/Debug/../Test.cpp:21: undefined reference to cvNamedWindow'
/home/shamma/workspace/Example/Debug/../Test.cpp:22: undefined reference tocvShowImage'
/home/shamma/workspace/Example/Debug/../Test.cpp:23: undefined reference to cvWaitKey'
/home/shamma/workspace/Example/Debug/../Test.cpp:24: undefined reference tocvDestroyWindow'
/home/shamma/workspace/Example/Debug/../Test.cpp:25: undefined reference to `cvReleaseImage'
collect2: ld returned 1 exit status
make: * [Example] Error 1enter code here
any idea what might be the cause of the problem, I have tried lots of things without use
thanks in advance
It seems you have not properly configured OpenCV libraries. I recommend you to follow the OpenCV tutorial to start using it with Eclipse.
In eclipse go to project->properties->settings and under GCC C++ linker-> libraries and assuming you're just testing, add "opencv_core" and "opencv_highgui" there are many more and for each library you need to manually include them so that eclipse can tell the compiler to link these libraries when you run your program.
Also as a note generally "cvE______" is opencv for C where as for C++ it would be cv::E____ , its really confusing that sometime you can get away with both, but not always, so stick the the function in the namespace without cv prefixes.