Undefined reference to __dso_handle_ - compiling C++ on cygwin - c++

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.

Related

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

Trying to understand linking procedure for writing Python/C++ hybrid

I want to start learning more about using SWIG and other methods to interface Python and C++. To get started, I wanted to compile this simple program mentioned in another post:
#include <Python.h>
int main()
{
Py_Initialize();
PyRun_SimpleString ("import sys; sys.path.insert(0, '/home/ely/Desktop/Python/C-Python/')");
PyObject* pModule = NULL;
PyObject* pFunc = NULL;
pModule = PyImport_ImportModule("hello");
if(pModule == NULL){
printf("Error importing module.");
exit(-1);
}
pFunc = PyObject_GetAttrString(pModule, "Hello");
PyEval_CallObject(pFunc, NULL);
Py_Finalize();
return 0;
}
where the file "hello.py" just has the contents:
def Hello():
print "Hello world!"
Note: I already have python2.7-dev and python-dev and libboost-python-dev installed. But when I go to compile the code, I get errors that I believe are due to incorrectly linking to the Python libraries.
ely#AMDESK:~/Desktop/Python/C-Python$ gcc -I/usr/include/python2.7 test.cpp /tmp/ccVnzwDp.o: In function `main':
test.cpp:(.text+0x9): undefined reference to `Py_Initialize'
test.cpp:(.text+0x23): undefined reference to `PyImport_ImportModule'
test.cpp:(.text+0x58): undefined reference to `PyObject_GetAttrString'
test.cpp:(.text+0x72): undefined reference to `PyEval_CallObjectWithKeywords'
test.cpp:(.text+0x77): undefined reference to `Py_Finalize'
/tmp/ccVnzwDp.o:(.eh_frame+0x12): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status
I was fishing around for examples of this online, and I found the following syntax, which causes the code to compile into an object file, but then I am unable to actually execute the file.
ely#AMDESK:~/Desktop/Python/C-Python$ gcc -c -g -I/usr/include/python2.7 test.cpp
ely#AMDESK:~/Desktop/Python/C-Python$ ./test.o
bash: ./test.o: Permission denied
ely#AMDESK:~/Desktop/Python/C-Python$ chmod ug=rx ./test.o
ely#AMDESK:~/Desktop/Python/C-Python$ ./test.o
bash: ./test.o: cannot execute binary file
ely#AMDESK:~/Desktop/Python/C-Python$ sudo chmod ug=rx ./test.o
ely#AMDESK:~/Desktop/Python/C-Python$ ./test.o
bash: ./test.o: cannot execute binary file
The same behavior as above is still seen if I use g++ instead of gcc.
Help in understanding my error in linking would be great, and even better for any sort of explanation that helps me understand the "logic" behind the kind of linking I need to do, so that I'll remember better what possible things I am forgetting the next time. Thanks!
What you are seeing are linker errors. To fix those, you need to link python2.7 library.
Try next line :
gcc -I/usr/include/python2.7 test.c -lpython2.7
it should work.
First, do you compile C or C++ code?
Use gcc for the former, and g++ for the latter. C++ code needs some additional linking to be performed.
Second: you have to link your program to libpython2.7.so to embed the interpreter into it. To do this, add -lpython2.7 to gcc command line.
test.o is not your executable file, that's why you can't execute it.
The default name for your program is a.out, try running that. You can specify a name for your program using the -o option.

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.

undefined referance to LibSerial

So i'm writing a serial transmision program, and have just changed over to using C++, it been a while since I used C++
(I've been working with C recently, and before that java)
Now I need to use LibSerial,
(it seems much simpler to use than C's termios)
my code is:
//gen1.cpp
#include "string2num.h" // a custom header
#include <iostream>
#include <SerialStream.h>
using namespace LibSerial;
//using namespace std;
int main(int argc, char*argv[])
{
if (argc<2)
{
std::cout<<argv[0]<<"requires the device name eg \"dev/tty0\" as a parameter\nterminating.\n";
return 1;
}
SerialStream theSerialStream(argv[1]); //open the device
return 0;
}
When I compile the output:
g++ -Wall -o gen1 gen1.cpp string2num.o
/tmp/cchPBWgx.o: In function `main':
gen1.cpp:(.text+0x121): undefined reference to `LibSerial::SerialStream::SerialStream(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::_Ios_Openmode)'
/tmp/cchPBWgx.o:(.rodata._ZTVN9LibSerial15SerialStreamBufE[vtable for LibSerial::SerialStreamBuf]+0x24): undefined reference to `LibSerial::SerialStreamBuf::showmanyc()'
/tmp/cchPBWgx.o:(.rodata._ZTVN9LibSerial15SerialStreamBufE[vtable for LibSerial::SerialStreamBuf]+0x28): undefined reference to `LibSerial::SerialStreamBuf::xsgetn(char*, int)'
/tmp/cchPBWgx.o:(.rodata._ZTVN9LibSerial15SerialStreamBufE[vtable for LibSerial::SerialStreamBuf]+0x2c): undefined reference to `LibSerial::SerialStreamBuf::underflow()'
/tmp/cchPBWgx.o:(.rodata._ZTVN9LibSerial15SerialStreamBufE[vtable for LibSerial::SerialStreamBuf]+0x34): undefined reference to `LibSerial::SerialStreamBuf::pbackfail(int)'
/tmp/cchPBWgx.o:(.rodata._ZTVN9LibSerial15SerialStreamBufE[vtable for LibSerial::SerialStreamBuf]+0x38): undefined reference to `LibSerial::SerialStreamBuf::xsputn(char const*, int)'
/tmp/cchPBWgx.o:(.rodata._ZTVN9LibSerial15SerialStreamBufE[vtable for LibSerial::SerialStreamBuf]+0x3c): undefined reference to `LibSerial::SerialStreamBuf::overflow(int)'
collect2: ld returned 1 exit status
make: *** [gen1] Error 1
This is the linker complaining that it cannot find the functions referenced by the libserial header file.
If I look on my Linux system to see how the shared library is called:
$ dpkg -L libserial0
...
/usr/lib/libserial.so.0.0.0
/usr/lib/libserial.so.0
On my system this implies I would add -lserial as a g++ option (aka link with libserial.so) this would turn your compilation command into
g++ -Wall -lserial -o gen1 gen1.cpp string2num.o
Including the header file is not enough - you also need to link with the library that implements SerialStream. Assuming it is a static library called serstream.a (it is almost certainly actually called something else):
g++ -Wall -o gen1 gen1.cpp string2num.o serstream.a
old thread, but i still use Libserial. here the completed answer
My working setup.
Ubuntu 18.04
g++ 7.3.0
1) Install package for libserial
apt install libserial-dev
2) check for your headers(.h) and .so files
dpkg -l libserial0
dpkg -l libserial-dev
the first command give you the directory of shared library and the second gives you the headers location.
3) Your code.
I have to change a little your code, first i delete the custom header and modifing the constuctor call to this.
SerialStream theSerialStream;
4) compile with g++
Here my compiling command
g++ -o test -I/usr/include test.cpp -L/usr/lib/x86_64-linux-gnu -lserial -lpthread
check for the -lpthread linking option, beacuse Libserial uses mutex.
In Ubuntu/Debian make sure you have to libserial-dev package installed and use the '-lserial' flag for gcc.

Can't link libpqxx in MinGW

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.