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"
Related
I have the following C++ code. Here I am OpenCV library for some image processing-related operations.
#include <opencv2/core.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
#include <opencv/cv.hpp>
using namespace cv;
using namespace std;
int main()
{
try
{
Mat frame, g_frame, r_frame; //, reduced_frame;
int REDUCTION = 3;
//--- INITIALIZE VIDEO_CAPTURE
VideoCapture cap;
// open selected camera using selected API
cap.open(cv::CAP_DSHOW);
// check if we succeeded
if (!cap.isOpened()) {
cerr << "ERROR! Unable to open camera\n";
return -1;
}
//--- GRAB AND WRITE LOOP
cout << "Start grabbing" << endl
<< "Press any key to terminate" << endl;
// image_window win;
while(true)
{
// wait for a new frame from camera and store it into 'frame'
cap.read(frame);
// check if we succeeded
if (frame.empty()) {
cerr << "ERROR! blank frame grabbed\n";
break;
}
// flipping the image
cv::flip(frame, frame, 1);
cv::resize(frame, frame, cv::Size(), 2.0f, 2.0f, cv::INTER_CUBIC);
// converting image to grey scale image
cv::cvtColor(frame, g_frame, CV_BGR2GRAY);
// reducing size of image
cv::resize(g_frame, r_frame, cv::Size(), 1.0/REDUCTION, 1.0/REDUCTION, cv::INTER_CUBIC);
cv::imshow("Live", frame);
if( waitKey(10) == 27 ) break; // stop capturing by pressing ESC
}
}
catch (exception& e)
{
cout << "\nexception thrown!" << endl;
cout << e.what() << endl;
}
return 0;
}
when I am trying to compile this code to wasm using the following command:
emcc -msse3 -msimd128 -std=c++11 -O3 -I ../opencv/build/include main.cpp -lpthread -s USE_PTHREADS=1 -s PTHREAD_POOL_SIZE=4 -s TOTAL_MEMORY=1024MB -s "EXTRA_EXPORTED_RUNTIME_METHODS=['ccall', 'cwrap']" -s WASM=1 -o main.js
Then I am getting the following errors:
error: undefined symbol: _ZN2cv12VideoCapture4openEi (referenced by top-level compiled C/C++ code)
warning: Link with `-s LLD_REPORT_UNDEFINED` to get more information on undefined symbols
warning: To disable errors for undefined symbols use `-s ERROR_ON_UNDEFINED_SYMBOLS=0`
warning: __ZN2cv12VideoCapture4openEi may need to be added to EXPORTED_FUNCTIONS if it arrives from a system library
error: undefined symbol: _ZN2cv12VideoCapture4readERKNS_12_OutputArrayE (referenced by top-level compiled C/C++ code)
warning: __ZN2cv12VideoCapture4readERKNS_12_OutputArrayE may need to be added to EXPORTED_FUNCTIONS if it arrives from a system library
error: undefined symbol: _ZN2cv12VideoCaptureC1Ev (referenced by top-level compiled C/C++ code)
warning: __ZN2cv12VideoCaptureC1Ev may need to be added to EXPORTED_FUNCTIONS if it arrives from a system library
error: undefined symbol: _ZN2cv12VideoCaptureD1Ev (referenced by top-level compiled C/C++ code)
warning: __ZN2cv12VideoCaptureD1Ev may need to be added to EXPORTED_FUNCTIONS if it arrives from a system library
error: undefined symbol: _ZN2cv3Mat10deallocateEv (referenced by top-level compiled C/C++ code)
warning: __ZN2cv3Mat10deallocateEv may need to be added to EXPORTED_FUNCTIONS if it arrives from a system library
error: undefined symbol: _ZN2cv4flipERKNS_11_InputArrayERKNS_12_OutputArrayEi (referenced by top-level compiled C/C++ code)
warning: __ZN2cv4flipERKNS_11_InputArrayERKNS_12_OutputArrayEi may need to be added to EXPORTED_FUNCTIONS if it arrives from a system library
error: undefined symbol: _ZN2cv6String10deallocateEv (referenced by top-level compiled C/C++ code)
warning: __ZN2cv6String10deallocateEv may need to be added to EXPORTED_FUNCTIONS if it arrives from a system library
error: undefined symbol: _ZN2cv6String8allocateEm (referenced by top-level compiled C/C++ code)
warning: __ZN2cv6String8allocateEm may need to be added to EXPORTED_FUNCTIONS if it arrives from a system library
error: undefined symbol: _ZN2cv6imshowERKNS_6StringERKNS_11_InputArrayE (referenced by top-level compiled C/C++ code)
warning: __ZN2cv6imshowERKNS_6StringERKNS_11_InputArrayE may need to be added to EXPORTED_FUNCTIONS if it arrives from a system library
error: undefined symbol: _ZN2cv6resizeERKNS_11_InputArrayERKNS_12_OutputArrayENS_5Size_IiEEddi (referenced by top-level compiled C/C++ code)
warning: __ZN2cv6resizeERKNS_11_InputArrayERKNS_12_OutputArrayENS_5Size_IiEEddi may need to be added to EXPORTED_FUNCTIONS if it arrives from a system library
error: undefined symbol: _ZN2cv7waitKeyEi (referenced by top-level compiled C/C++ code)
warning: __ZN2cv7waitKeyEi may need to be added to EXPORTED_FUNCTIONS if it arrives from a system library
error: undefined symbol: _ZN2cv8cvtColorERKNS_11_InputArrayERKNS_12_OutputArrayEii (referenced by top-level compiled C/C++ code)
warning: __ZN2cv8cvtColorERKNS_11_InputArrayERKNS_12_OutputArrayEii may need to be added to EXPORTED_FUNCTIONS if it arrives from a system library
error: undefined symbol: _ZN2cv8fastFreeEPv (referenced by top-level compiled C/C++ code)
warning: __ZN2cv8fastFreeEPv may need to be added to EXPORTED_FUNCTIONS if it arrives from a system library
error: undefined symbol: _ZNK2cv12VideoCapture8isOpenedEv (referenced by top-level compiled C/C++ code)
warning: __ZNK2cv12VideoCapture8isOpenedEv may need to be added to EXPORTED_FUNCTIONS if it arrives from a system library
Error: Aborting compilation due to previous errors
emcc: error: 'C:/emsdk/node/12.18.1_64bit/bin/node.exe C:\emsdk\upstream\emscripten\src\compiler.js C:\Users\Nitin\AppData\Local\Temp\tmppq_ad1ya.txt' failed (1)
Please help me.
The above problem is solved by using the suggestions provided in the error itself.
emcc -msse3 -msimd128 -std=c++11 -O3 -I ../opencv/build/include main.cpp -lpthread -s USE_PTHREADS=1 -s PTHREAD_POOL_SIZE=4 -s TOTAL_MEMORY=1024MB -s "EXTRA_EXPORTED_RUNTIME_METHODS=['ccall', 'cwrap']" -s WASM=1 -s LLD_REPORT_UNDEFINED -s ERROR_ON_UNDEFINED_SYMBOLS=0 -o main.html
However, while opening the main.html file in the browser. I am now getting the following error.
CompileError: WebAssembly.instantiate(): Compiling function #607 failed: invalid value type 'Simd128', enable with --experimental-wasm-simd #+105826
Please help me in solving this.
You have to run your browser with the option --experimental-wasm-simd, as SIMD isn't a part of the webassembly spec (yet).
You need to follow the suggestions given in the errors. Check line number 2 and 3 of your errors.
I'm currently having problem with simple project(or llvm-example for that matter). My assignment requires me to use llvm libraries, however this isn't as easy as I had hoped.
I've build LLVM using MinGW GCC and CMake. After build I can compile with clang fine. However if I create simple hello world type of program
#include "llvm-c/Core.h"
int main(int argc, char**argv)
{
return 0;
}
and try to compile it with
clang++ main.cpp
I get
In file included from main.cpp:1:
./llvm-c/Core.h:18:10: fatal error: 'llvm-c/ErrorHandling.h' file not found
#include "llvm-c/ErrorHandling.h"
For this example I have copied contents of include directory into directory with main.cpp. After getting this issue I tried to inspect those headers and all of them have position llvm/ or llvm-c/, instead of pure relative address. I figured these libs were used to build/make llvm and libs for me to use are actually in build directory, which is where I have built llvm, but include directory in build has only about 1/2 *.h files.
I can't seem to find anything in documentation related to this and even basic llvm examples are expecting to include libs in format like llvm/Core.h
EDIT
After solving inclusion problem, now I get several other problems which seems to be linked to mingw.
This is how new program looks.
#define __STDC_LIMIT_MACROS
#define __STDC_CONSTANT_MACROS
#include <llvm-c/Core.h>
using namespace std;
int main()
{
LLVMModuleRef rff = LLVMModuleCreateWithName("testname");
return 0;
}
this generates
d:\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.9.3\..\..\..\libLLVMSupport.a(CommandLine.cpp.obj):CommandLine.cpp|| undefined reference to `__mingw_strtod'|
d:\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.9.3\..\..\..\libLLVMSupport.a(regerror.c.obj):regerror.c|| undefined reference to `__ms_vsnprintf'|
d:\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.9.3\..\..\..\libLLVMSupport.a(Path.cpp.obj):Path.cpp|| undefined reference to `SHGetKnownFolderPath#16'|
d:\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.9.3\..\..\..\libLLVMSupport.a(Path.cpp.obj):Path.cpp|| undefined reference to `_imp__CoTaskMemFree#4'|
d:\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.9.3\..\..\..\libLLVMSupport.a(Path.cpp.obj):Path.cpp|| undefined reference to `_imp___chsize_s'|
d:\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.9.3\..\..\..\libLLVMSupport.a(Path.cpp.obj):Path.cpp|| undefined reference to `FOLDERID_LocalAppData'|
d:\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.9.3\..\..\..\libLLVMSupport.a(Path.cpp.obj):Path.cpp|| undefined reference to `FOLDERID_LocalAppData'|
d:\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.9.3\..\..\..\libLLVMSupport.a(Path.cpp.obj):Path.cpp|| undefined reference to `FOLDERID_LocalAppData'|
d:\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.9.3\..\..\..\libLLVMSupport.a(Path.cpp.obj):Path.cpp|| undefined reference to `FOLDERID_LocalAppData'|
d:\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.9.3\..\..\..\libLLVMSupport.a(Path.cpp.obj):Path.cpp|| undefined reference to `FOLDERID_Profile'|
d:\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.9.3\..\..\..\libLLVMSupport.a(Path.cpp.obj):Path.cpp|| undefined reference to `FOLDERID_Profile'|
d:\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.9.3\..\..\..\libLLVMSupport.a(Path.cpp.obj):Path.cpp|| undefined reference to `FOLDERID_Profile'|
d:\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.9.3\..\..\..\libLLVMSupport.a(Path.cpp.obj):Path.cpp|| undefined reference to `FOLDERID_Profile'|
d:\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.9.3\..\..\..\libLLVMSupport.a(Path.cpp.obj):Path.cpp|| undefined reference to `FOLDERID_LocalAppData'|
d:\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.9.3\..\..\..\libLLVMSupport.a(Path.cpp.obj):Path.cpp|| undefined reference to `FOLDERID_LocalAppData'|
d:\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.9.3\..\..\..\libLLVMSupport.a(Path.cpp.obj):Path.cpp|| undefined reference to `FOLDERID_LocalAppData'|
d:\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.9.3\..\..\..\libLLVMSupport.a(Path.cpp.obj):Path.cpp|| undefined reference to `FOLDERID_LocalAppData'|
d:\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.9.3\..\..\..\libLLVMSupport.a(TimeValue.cpp.obj):TimeValue.cpp|| undefined reference to `_localtime32'|
I used llvm-config --libs core for linker additions.
Tried commands:
g++ main.cpp -lLLVMCore -lLLVMSupport
clang++ main.cpp -lLLVMCore -lLLVMSupport
Those are WinAPI references. The easiest way is to find those are google for documentation for it or go to the mingw library directory ( ..\mingw530_32\i686-w64-mingw32\lib\ ) and find in files.
So
FOLDERID_LocalAppData is defined in libuuid.a so use the -luuid compile parameter
_imp__CoTaskMemFree#4 is defined in libole32.a so use the -lole32 compile parameter
For me g++ -std=c++11 -ID:\Devel\install\include main.cc -LD:\Devel\install\lib -lLLVMCore -lLLVMSupport -luuid -lole32
is working as I installed the llvm to D:\Devel\install
If you need the headers from your-llvm/include directory instead of copying the contents of the include directory you should just specify the include path to your-llvm/include with -I option or whatever the build system requires.
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.
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 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.