Undefined reference to boost::timer::auto_cpu_timer - c++

I try to compile small .cpp file with boost library on remote server on Debian using g++ 4.4. I use Netbeans for this purpose. My home machine is on windows 7. After resolving some issues with linking next code
#include <boost/timer/timer.hpp>
#include <iostream>
#include <string>
int main()
{
boost::timer::auto_cpu_timer ac; //line 5
return 0; //line 6
}
Produces 2 errors:
line 5: undefined reference to boost::timer::auto_cpu_timer::auto_cpu_timer(short)'
line 6: undefined reference to boost::timer::auto_cpu_timer::~auto_cpu_timer()'
Same result if i use header boost/thread.hpp but for thread constructor/destructor.
But for example boost/shared_ptr compiles without any problem.
Result compile command in neatbeans is
g++ -m64 -I/usr/include/boost/boost_1_49_0 -lboost_system -o dist/Debug/GNU-Linux-x86/test build/Debug/GNU-Linux-x86/main.o
-L/usr/include/boost/boost_1_49_0/stage/lib -Wl,-rpath /usr/include/boost/boost_1_49_0/stage/lib build/Debug/GNU-Linux-x86/main.o
What i missed?

You need to link against boost_timer. Add -lboost_timer to the gcc command line. Consult the Netbeans documentation on how to add libraries to a project.

Related

Unable to statically link boost-locale due to libicu linking errors on Ubuntu 18.04

The following code:
#include <boost/locale.hpp>
int main()
{
using namespace boost::locale;
generator gen;
}
Built with the following flags:
g++ -static -static-libstdc++ -pthread main.cpp -lboost_locale -lboost_system -licuuc -licudata -licui18n
Fails to link with the following errors:
(.text+0x16b8): undefined reference to `icu_60::CacheKeyBase::~CacheKeyBase()'
(.text+0x1c8d): undefined reference to `icu_60::UVector64::~UVector64()'
(.text+0x7dc): undefined reference to `icu_60::UCharsTrie::Iterator::~Iterator()'
...hundreds of other undefined symbols
...(the sample code also has boost linking errors, but let's ignore them, as they only appear in my attempt of reproduction; the real code I have issues with has only icu-related link errors)
I'm using Ubuntu 18.04, with both libboost-all-dev and libicu-dev installed from the Ubuntu repositories.
I'm probably missing some trivial component/flag/library needed to make it link together, but am unable to find it.

Cannot include standard libraries in C++ file

#include <iostream>
using namespace std;
int main(){
std::cout << "Hello World\n";
return 0;
}
command 1 (works)
clang hello.cc -o hello -lc++
command 2 (don't works)
/path/to/custom/clang hello.cc -o hello -lc++
main.cc:2:10: fatal error: 'iostream' file not found
#include <iostream>
^
1 error generated.
Why I can't compile with command 2 ?
It looks like you're trying to compile C++ with a C compiler. Try running clang++ instead.
clang++ hello.cc -o hello
Without running clang as a C++ compiler it won't have the C++ standard library headers available for you to include. Using clang++ the C++ standard library headers are available and the C++ standard library is linked for you automatically.
That is a known Ubuntu issue. Their clang just isn't set up right. I complained about it here -- and this remained unfixed for years.
But the good news is that it now works with the most recent 16.10 release.
Edit: Based on your updated question I would say that "custom clang" does not know about its include files.

Undefined reference to boost::random::random_device constructor and destructor on MinGW-w64 gcc

My OS is Windows 7 64-bit and C++ compiler I'm using is:
g++ (i686-posix-dwarf-rev0, Built by MinGW-W64 project) 5.3.0
And I installed Boost version 1.60 using:
bootstrap.bat mingw
b2 install target=gcc
Then I tested is it working, using examples from Boost.Random tutorial.
With the first two everything was fine, but the third one gave linker errors about boost::random::random_device. I minimized the code to have only this:
// Compiled with:
// g++ -IC:/Boost/include/boost-1_60
// -LC:/Boost/lib -lboost_random-mgw53-mt-1_60
// main.cpp
#include "boost/random/random_device.hpp"
int main() {
boost::random::random_device rng;
}
And I get the following errors:
C:\Users\Daniel\AppData\Local\Temp\cc5DfdjZ.o:main.cpp:(.text+0x15):
undefined reference to `boost::random::random_device::random_device()'
C:\Users\Daniel\AppData\Local\Temp\cc5DfdjZ.o:main.cpp:(.text+0x20):
undefined reference to `boost::random::random_device::~random_device()'
collect2.exe: error: ld returned 1 exit status
Here, on SO, I found that someone with similar problem added -lboost_system to flags, but for me it didn't helped.
Does anyone have any idea, why it isn't working? I checked, and I have random_device.hpp header in my Boost folder, with declarations of random_device() and ~random_device() in it.
I found what was wrong - the g++ command syntax, that I wanted to use to compile and link my code.
As I wrote in my question, I do this that way:
g++ -IC:/Boost/include/boost-1_60 -LC:/Boost/lib -lboost_random-mgw53-mt-1_60 main.cpp
While the correct one is with main.cpp (or any other source code file(s), that we want to include in compiling process) before the -L and -l flags.
For example:
g++ -IC:/Boost/include/boost-1_60 main.cpp -LC:/Boost/lib -lboost_random-mgw53-mt-1_60
or even
g++ main.cpp -IC:/Boost/include/boost-1_60 -LC:/Boost/lib -lboost_random-mgw53-mt-1_60
Hope it will help anyone, who will make such silly mistake too.

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.

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.