Can't link libpqxx in MinGW - c++

Using MSYS, I compiled libpq (from compiling postgres). I then compiled libpqxx. Now, I want to create a client that will use libpqxx. libpq seemed to work fine. And, I can compile code with libpqxx. However, linking the libpq client application fails.
Here's my code:
#include <pqxx/pqxx>
#include <iostream>
using namespace std;
using namespace pqxx;
int main() {
connection Conn("dbname=test");
cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
return 0;
}
I added a bunch of libs to the link in a vain hope it would suddenly work. Here's what I have so far:
g++ -IC:\msys\1.0\local\pgsql\include -IC:\msys\1.0\local\include -O0 -g3 -Wall -c -fmessage-length=0 -osrc\Controller.o ..\src\Controller.cpp
g++ -LC:\MinGW\lib -LC:\msys\1.0\local\pgsql\lib -LC:\msys\1.0\local\lib -oController.exe src\Controller.o -lws2_32 -lole32 -lpqxx -lpq -loleaut32 -luuid
C:\msys\1.0\local\lib/libpqxx.a(connection_base.o): In function `ZN45_GLOBAL__N__ZN4pqxx16encrypt_passwordERKSsS1_7wait_fdEibP7timeval':
C:/msys/1.0/home/rsolomon/libpqxx/libpqxx-3.0.2/src/connection_base.cxx:1434: undefined reference to `select#20'
C:\msys\1.0\local\lib/libpqxx.a(connection_base.o): In function `ZN4pqxx15connection_base12check_resultERKNS_6resultE':
C:/msys/1.0/home/rsolomon/libpqxx/libpqxx-3.0.2/src/connection_base.cxx:420: undefined reference to `select#20'
collect2: ld returned 1 exit status
Build error occurred, build is stopped
Time consumed: 1770 ms.
I'm thinking the -lws2_32 should've gave me the "select#20". Why is the linker so uppity?

The Unix linker traditionally processes libraries from left to right. So it first considers ws2_32, finds that it has not much use, then goes on to pqxx, and sees that select is undefined and doesn't get defined by any of the later libraries. IOW, try moving ws2_32 to the end of the command line.

Related

Undefined reference to __dso_handle_ - compiling C++ on cygwin

I have a basic Hello World C++ program that I am trying to compile and run on cygwin with g++. The code is:
#include <iostream>
using namespace std;
int main() {
cout << "Hello world." << endl;
return 0;
}
Compiling it with: g++ helloWorld.cpp -o h results in the following error:
/tmp/ccDO1S4J.o:helloWorld.cpp:(.rdata$.refptr.__dso_handle[.refptr.__dso_handle]+0x0): undefined reference to__dso_handle'
collect2: error: ld returned 1 exit status`
I have been reading up on some other threads that indicate that it might be a linker problem and that invoking the linker separately or with verbose output might lead to some more clues.
So, I did:
1. g++ -c helloWorld.cpp -o helloWorld.o (this works - no errors).
2. ld -o h helloWorld.o causes a lot of undefined reference to __main or std::cout etc. errors.
I think this is a linking issue and that I need to link another library perhaps. Any pointers on how to solve this are most welcome.
Re-installing g++ via the installer application on cygwin worked.

Cannot call XInitThreads

I have written an SFML C++ game, and tried to start using threads, but after a while everything crashes. After searching I found out the fix seems to be to call XInitThreads();
but this does not work somehow.
simplified code:
#include <X11/Xlib.h>
int main() {
XInitThreads();
//other stuff
return 1337;
}
The error message I get when i try to compile is "undefined reference to symbol 'XInitThreads'. Could it be that the header file is working but there is no file where that method is implemented?
"undefined reference to symbol" is a linker error, not a compiler error. If you get this message, the compiler has already finished compiled the file into an object file, but is unable to find the shared library which contains the function to link the object file into an executable.
If you're using gcc, it generally means you have to add some -l flags, like so:
$ gcc prog.c -lX11
note that the order of -lX11 in the compiler argument matters, you would get an error if you do this:
$ gcc -lX11 prog.c
/tmp/ccBCxiFT.o: In function `main':
:(.text+0x5): undefined reference to `XInitThreads'
collect2: error: ld returned 1 exit status
You should add link X11 library setting -lX11 to your project. If you are using Eclipse navigate to projectproperties->C/C++ Build->Settings->Tool Settings->GCC Linker->Libraries and add "X11"
Add header-
#include<X11/Xlib.h>
Compile your source code using-
gcc <filename.extension> -lX11
Tested in Ubuntu 16.04 LTS

Boost.Asio linking error

I'm trying access an extern device via a serial port and want to use Boost.Asio for this propose.
I have build the boost libraries for MinGw and compiled the regex example successful.
But I have problems to compile my code if I include something from Boost.Asio:
#include <boost/asio/serial_port.hpp>
int main() {
return 0;
}
g++ -D _WIN32_WINNT=0x0501 -O0 -g3 -Wall -c -fmessage-length=0 -osrc\SerialPortTest.o ..\src\SerialPortTest.cpp
g++ -LC:\boost-libs\boost\bin.v2\libs\thread\build\gcc-mingw-4.5.2\release\link-static\threading-multi -LC:\boost-libs\boost\bin.v2\libs\system\build\gcc-mingw-4.5.2\release\link-static\threading-multi -oSerialPortTest.exe src\SerialPortTest.o -lboost_thread-mgw45-mt-1_48 -lboost_system-mgw45-mt-1_48
src\SerialPortTest.o: In function `ZN5boost4asio6detail17winsock_init_base7startupERNS2_4dataEhh':
c:/mingw/bin/../lib/gcc/mingw32/4.5.2/../../../../include/boost/asio/detail/impl/winsock_init.ipp:39: undefined reference to `WSAStartup#8'
src\SerialPortTest.o: In function `ZN5boost4asio6detail17winsock_init_base7cleanupERNS2_4dataE':
c:/mingw/bin/../lib/gcc/mingw32/4.5.2/../../../../include/boost/asio/detail/impl/winsock_init.ipp:48: undefined reference to `WSACleanup#0'
collect2: ld returned 1 exit status
For me it seems to be a linking problem, but I don't get it.
Add -lws2_32 flag to link against WinSockets library.
Also, this might be useful: MinGW linker error: winsock
You miss wsock32 library. Add this to your dependencies and it should work.

Compiling parts of Festival code written in C++ in a stand-alone C++ program

I am trying to use selective parts of the Festival code (written in C++) and trying to use them in my own C++ programs. Note that this question is not about using the Festival API but about functions within Festival that can be used directly.
The program I wrote takes in a C++ style string and tries to initialize an object of type EST_String (an internal implementation of the String class in Festival). I then try to print the object.
The code I have:
/*EST_String is a string implementation for the festival project.
* This program simply takes in a C++-style string and returns an EST_String.
* This program is being used to test for makefiles etc.
*/
#include <iostream>
#include <EST_String.h>
#include <string>
#include <cstdlib>
using namespace std;
int main(int argc, char *argv[]) {
if(argc != 2) {
cout << "Correct usage: <binary> <string>" << endl;
exit(5);
}
string word(argv[1]);
EST_String e(word.c_str()); //init EST_String.
cout << "C++ String = " << word << endl;
cout << "EST_String = ";
cout << e;
return 0;
}
I am trying to compile it (from the command line directly (no makefile at present)) like so:
g++ -I../../speech_tools/include -I../../speech_tools/base_class/string -L../../speech_tools/lib/ -lestbase -lncurses -lasound -leststring -lestools usingESTString.C -o usingESTString
The error I get is:
/tmp/cczyGTfm.o: In function `main':
usingESTString.C:(.text+0x91): undefined reference to `EST_String::EST_String(char const*)'
/tmp/cczyGTfm.o: In function `EST_Chunk::operator--()':
usingESTString.C:(.text._ZN9EST_ChunkmmEv[EST_Chunk::operator--()]+0x3e): undefined reference to `EST_Chunk::~EST_Chunk()'
usingESTString.C:(.text._ZN9EST_ChunkmmEv[EST_Chunk::operator--()]+0x49): undefined reference to `EST_Chunk::operator delete(void*)'
collect2: ld returned 1 exit status
How can I get the code to compile properly? Where am I going wrong?
Try putting the libraries you link with last on the line.
The linker often resolves references kind of "backwards", meaning that the order of files presented on the command line is important: It wants files containing references first, then the libraries containing those references.
Try adding this to end of your g++ link command: -I/usr/include/festival
-I/usr/include/speech_tools -I/usr/include/boost -lFestival -lestools
-lestbase -leststring
Making sure that the festival and speech_tools headers directory lives at: /usr/include
cd /usr/include
ls festival
ls speech_tools
I am trying to rebuild cogita with festival support, and my program linked successfully after compiling the object files using this line
g++ -Wall -fPIC -Wno-variadic-macros -fopenmp -std=gnu++0x -O2 -g -fstack-protector cogitaconfig.o go-irc.o irc.o whirr-sockets.o -o cogIRCProgram
-rdynamic /usr/local/lib/libcogutil.so -Wl,-rpath,/usr/local/lib
-I/usr/include/festival -I/usr/include/speech_tools -I/usr/include/boost
-lFestival -lestools -lestbase -leststring
I've been trying to link to festival's API too, and the Makefile I wrote executes the following link command
g++ -lpthread build/fetch/festival/src/lib/libFestival.a build/fetch/speech_tools/lib/libestools.a build/fetch/speech_tools/lib/libestbase.a build/fetch/speech_tools/lib/libeststring.a -lcurses -ldl -lm -lstdc++ build/test/speaker.o build/common/message-queue.o build/speaker/speaker-public.o build/fetch/festival/src/lib/libFestival.a -o build/bin/speaker-test
and I get a huge (25k lines) linker error full of undefined references (a part of which is here: http://pastebin.com/PCyV8xAH). I can assert that the *.a files exist (though I'm not sure if they've been built correctly or not). I compile speech_tools with make -j7 and festival with make.
Any suggestions?
I'm running Debian wheezy.

OpenCV, eclipse compile problem

I have a compile problem I can't figure out for OpenCV2.1 in c++.
Here is a simple test code I am trying to compile:
#include <iostream>
#include "cv.h"
using namespace std;
int main() {
cout << "Hello World" << endl; // prints !!!Hello World!!!
cv::Mat mtx;
return 0;
}
I a compile error with an undefined reference as follows
**** Build of configuration Debug for project CJMVideo ****
**** Internal Builder is used for build ****
g++ -IC:\OpenCV2.1\include\opencv -IC:\Program Files\Point Grey Research\FlyCapture2\include -O0 -g3 -Wall -c -fmessage-length=0 -osrc\CJMVideo.o ..\src\CJMVideo.cpp
g++ -LC:\OpenCV2.1\lib -LC:\Program Files\Point Grey Research\FlyCapture2\lib64 -Xlinker --enable-auto-import -oCJMVideo.exe src\CJMVideo.o -lcxcore210 -lcv210 -lhighgui210 -lml210 -lFlyCapture2
src\CJMVideo.o:C:/OpenCV2.1/include/opencv/cxmat.hpp:378: undefined reference to `cv::fastFree(void*)'
collect2: ld returned 1 exit status
Build error occurred, build is stopped
Time consumed: 1438 ms.
The error is C:/OpenCV2.1/include/opencv/cxmat.hpp:378: undefined reference to `cv::fastFree(void*)'
I believe I have compiled all the libraries correctly from the above command...What is the problem?
Thanks
Even though the message suggests it has not found that symbol on the OpenCV libraries, I must point out that from the command line pasted above, it seems you are trying to link your application against 64-bit compiled libraries, as indicated by -LC:\Program Files\Point Grey Research\FlyCapture2\lib64. That means you must compile OpenCV to be 64-bit too, or compile both to be 32 bits.
You are probably missing one library. On Windows, my OpenCV projects usually adds cv210.lib cvaux210.lib cxcore210.lib cxts210.lib highgui210.lib, but I use Visual Studio 2005 most of the times.
I have had problems linking cv::fastfree when the OpenCV lib was built with the intel TBB parallel library, building without TBB worked