undefined reference to `__gcov_dump() - c++

I try to test the __gcov_dump function via a simple example like this:
tmp.c:
#include <stdio.h>
#include <gcov.h>
int main()
{
int i, total;
total = 0;
for (i = 0; i < 10; i++)
total += i;
if (total != 45)
printf ("Failure\n");
else
printf ("Success\n");
__gcov_dump();
}
~/Downloads/gcovtest ❯ g++ --coverage -lgcov tmp.c
/tmp/user/ccdEPxlm.o: In function main': tmp.c:(.text+0xa3): undefined reference to __gcov_dump()'
collect2: error: ld returned 1 exit status
Leaving out -lgcov makes not difference.
It's an Ubuntu 18.04 LTE with a
g++ (Ubuntu 9.4.0-1ubuntu1~18.04.1sav0) 9.4.0
/usr/lib/gcc/x86_64-linux-gnu/9/libgcov.a is present and the program tries to open it as strace says.
Any ideas what could be wrong?
Regards

Ok, it seems I cannot use the standard gcov.h header when using g++. I have to declare the method manually with c linkage:
extern "C" void __gcov_dump();

Related

C/C++ Threading in Linux (Raspbian) using VS2019 on Windows 10 -pthread - Can't compile

I'm trying to do my first bit of threading but no matter what I've tried I can't get this to compile.
I've gone back to trying to compile some demo code and I'm getting the same problem as in my program.
If I run a simple print hello world it compiles and deploys the program fine and I can simply navigate to and run it directly on the Pi4.
Threading demo code
#include<stdio.h>
#include<string.h>
#include<pthread.h>
#include<stdlib.h>
#include<unistd.h>
pthread_t tid[2];
void* doSomeThing(void* arg)
{
unsigned long i = 0;
pthread_t id = pthread_self();
if (pthread_equal(id, tid[0]))
{
printf("\n First thread processing\n");
}
else
{
printf("\n Second thread processing\n");
}
for (i = 0; i < (0xFFFFFFFF); i++);
return NULL;
}
int main(void)
{
int i = 0;
int err;
while (i < 2)
{
err = pthread_create(&(tid[i]), NULL, &doSomeThing, NULL);
if (err != 0)
printf("\ncan't create thread :[%s]", strerror(err));
else
printf("\n Thread created successfully\n");
i++;
}
sleep(5);
return 0;
}
When I compile I get
Error /home/pi/projects/cpp_raspbian_thread_101/obj/x64/Debug/main.o: in function `main':
Error undefined reference to `pthread_create'
Error ld returned 1 exit status
To resolve this I've tried to add -pthread or -lpthread to
Project > Properties > Configuration Properties > C/C++ > Command Line > Addiitional Options
That does nothing, I'm not really sure if this is the correct place to put this.
I'm building in VS2019 so I'm not building from the command line, I don't know where to add this argument.
I have also tried installing pthreads in NuGet but that doesn't help.
Other software like VSCode seem to have files that could add this to but I'm lost in VS2019
Any help is appreciated.
EDIT:
Thanks for responses
OK so as #Eljay suggested I'm trying to use std::thread (again) but have the same problem.
// thread example
#include <iostream>
#include <thread>
void foo()
{
// do stuff...
}
int main()
{
std::thread first(foo);
return 0;
}
Log file
Validating sources
Copying sources remotely to '10.0.0.2'
Validating architecture
Validating architecture
Starting remote build
Compiling sources:
main.cpp
Linking objects
/usr/bin/ld : error : /home/pi/projects/cpp_raspbian_thread_101/obj/ARM/Debug/main.o: in function `std::thread::thread<void (&)(), , void>(void (&)())':
/usr/include/c++/8/thread(135): error : undefined reference to `pthread_create'
collect2 : error : ld returned 1 exit status
So I'm back to the pthread_create problem again
OK both code examples now compile and run.
As I originally thought, I needed to add -pthread somewhere in VS2019 and I was putting it in the wrong section.
Go to
Project Properties > Configuration Properties > Linker > Command Line
Add -pthread to Additional Options box and Apply.
I hope that saves someone else the 3 days it took me to sort it!

Undefined reference to printf when using GCC LD cross compiler

I have an issue on gcc 9.3.1 version for ARM, my code have two file that object_1.c is empty file and for main.c as
unsigned char test = 100;
int main(void)
{
/*Write code here*/
test++;
printf("Test lib");
return 0;
}
my command when linking as the example:
<gcc_path>/bin/arm-none-eabi-ld.exe -L<gcc_path>/arm-none-eabi/lib -L<gcc_path>/lib/gcc/arm-none-eabi/9.3.1 -lc -lm -T linker.ld object_1.o main.o -o run.elf
I have tried some ideas but it is not work. Could anyone help me with some ideas?

g++ linker can not find library

I want to use this library in my c++/test.cpp file.
#include "omp/HandEvaluator.h"
#include <iostream>
using namespace omp;
int main()
{
HandEvaluator eval;
Hand h = Hand::empty(); // Final hand must include empty() exactly once!
h += Hand(51) + Hand(48) + Hand(0) + Hand(1) + Hand(2); // AdAs2s2h2c
std::cout << eval.evaluate(h) << std::endl; // 28684 = 7 * 4096 + 12
}
I downloaded the source code from github and placed into the OMPEval folder. After make the ompeval.a library appeared.
Here is the folder structure:
Now I try to build it:
projects/c++$ g++ -Wall -g -L /home/a/projects/c++/OMPEval/lib/ -l ompeval -I /home/a/projects/c++/OMPEval/ test.cpp -v
but the linker has error:
/usr/bin/ld: cannot find -lompeval
collect2: error: ld returned 1 exit status
Here is the whole build log:

Undifined reference to espeak-ng headers in Ubuntu

I have downloaded espeak-ng 1.1.49 and ./configure make make install it, and tested it by espeak --stdout "this is a test" | paplay successfully and it worked. Then I tried to use it inside my C++ code(testSpeak.cpp) that I found on the internet as you can see below:
#include <string.h>
#include <vector>
#include </usr/local/include/espeak-ng/speak_lib.h>
int samplerate; // determined by espeak, will be in Hertz (Hz)
const int buflength = 200; // passed to espeak, in milliseconds (ms)
std::vector<short> sounddata;
int SynthCallback(short *wav, int numsamples, espeak_EVENT *events) {
if (wav == NULL)
return 1; // NULL means done.
/* process your samples here, let's just gather them */
sounddata.insert(sounddata.end(), wav, wav + numsamples);
return 0; // 0 continues synthesis, 1 aborts
}
int main(int argc, char* argv[] ) {
char text[] = {"my name is espeak"};
samplerate = espeak_Initialize(AUDIO_OUTPUT_RETRIEVAL, buflength, NULL, 0);
espeak_SetSynthCallback(&SynthCallback);
espeak_SetVoiceByName("en");
unsigned int flags=espeakCHARS_AUTO | espeakENDPAUSE;
size_t size = strlen(text);
espeak_Synth(text, size + 1, 0, POS_CHARACTER, 0, flags, NULL, NULL);
espeak_Synchronize();
/* in theory sounddata holds your samples now... */
return 0;
}
But after trying to make executable by this command: g++ testSpeak.cpp -o speaks I got these error messages:
/tmp/ccR9O0vw.o: In function `main':
testSpeak.cpp:(.text+0x78): undefined reference to `espeak_Initialize'
testSpeak.cpp:(.text+0x90): undefined reference to `espeak_SetSynthCallback'
testSpeak.cpp:(.text+0x9c): undefined reference to `espeak_SetVoiceByName'
testSpeak.cpp:(.text+0xce): undefined reference to `espeak_Synth'
testSpeak.cpp:(.text+0xd2): undefined reference to `espeak_Synchronize'
collect2: error: ld returned 1 exit status
I know the problem is about linking but as I am new to Linux, don't know how can I fix it! Also I searched a lot but couldn't understand the solutions :(
I got it to compile correctly try installing
sudo apt-get install espeak-data libespeak-dev espeak-ng
your include is
#include </usr/local/include/espeak-ng/speak_lib.h>
make it
#include <espeak-ng/speak_lib.h>
your compile command is
g++ testSpeak.cpp -o speaks
try this one instead
g++ -W -o speaks myEspeak.cpp -lespeak
reference from i wouldn't compile it tough tried and it doesn't work probably an old version but with the code you provided and installing those programs and changing out your include your code will compile. I doesn't do much I would find a way to store that into a a .wav file.
http://apexlogic.net/code-bank/c-2/espeak-basic-usage-example/
when ever you compile from a shared library you need to link it with something that looks like
-lespeak

Another "undefined reference" error when linking boost libraries

I've seen several other posts that deal with this exact same issue. However, none of their solutions seem to work for me. I am compiling the following code:
#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/ublas/io.hpp>
#include <boost/timer/timer.hpp>
using namespace boost::numeric::ublas;
int main(){
matrix<double> mat1 (3,3);
matrix<double> mat2 (3,3);
matrix<double> mat3 (3,3);
unsigned k=0;
for(unsigned i = 0; i < mat1.size1(); ++i){
for(unsigned j = 0; j < mat1.size2(); ++j){
mat1(i,j) = k;
mat2(i,j) = 2*k++;
}
}
k=0;
if(1){
boost::timer::auto_cpu_timer t;
while(k<1000){
mat3 = prod(mat1,mat2);
k++;
}
}
return 0;
}
I am compiling from the command line using:
$ g++ matrix_test.cpp -o matrix_test -lboost_system -lboost_timer
and receiving the following error:
usr/lib/gcc/i686-redhat-linux/4.7.0/../../../libboost_timer.so:
undefined reference to `boost::chrono::steady_clock::now()'
collect2: error: ld returned 1 exit status
If I add -lboost_chrono when I compile, I get this error:
/usr/lib/gcc/i686-redhat-linux/4.7.0/../../../libboost_chrono.so:
undefined reference to `clock_gettime'
collect2: error: ld returned 1 exit status
I can trace clock_gettime to sys/time.h. Unfortunately, I cannot find a corresponding .so file to link to. What am I missing here?
You must add -lrt to your link libraries
g++ matrix_test.cpp -o matrix_test -lboost_system -lboost_timer -lboost_chrono -lrt
Update (2016-08-31)
This still seems to be an issue. When you lookup man clock_gettime, this leads to the solution (-lrt), but it also says
Link with -lrt (only for glibc versions before 2.17).
So when your glibc is newer, your problem might be something else.
Add -lrt to your g++ invocation – clock_gettime is in librt.so.