Another "undefined reference" error when linking boost libraries - c++

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.

Related

undefined reference to `__gcov_dump()

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();

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:

GCC fails to compile OpenMP offloading to GPU

I'm using GCC 9.3 on Ubuntu 20.04. I want to offload the famous SAXPY example to GPU using OpenMP. I installed GCC's offloading capabilities by sudo apt install gcc-9-offload-nvptx . Then compiled the following code by g++ -fopenmp main.cpp:
int main()
{
const size_t kNumel = 999999;
float x[kNumel];
float y[kNumel];
for (size_t i=0 ;i <kNumel; i++)
{
x[i] = i;
y[i] = i;
}
const float kCoef = 1.23f;
#pragma omp target teams distribute parallel for
for (size_t i=0; i < kNumel; i++)
{
y[i] = kCoef*x[i] + y[i];
}
return 0;
}
BUT it doesn't compile and shows this error:
to1: error: ‘-fcf-protection=full’ is not supported for this target
mkoffload: fatal error: x86_64-linux-gnu-accel-nvptx-none-gcc-9 returned 1 exit status
compilation terminated.
lto-wrapper: fatal error: /usr/lib/gcc/x86_64-linux-gnu/9//accel/nvptx-none/mkoffload
returned 1 exit status
compilation terminated.
/usr/bin/ld: error: lto-wrapper failed
collect2: error: ld returned 1 exit status
I added -fno-stack-protector but the same error is reproduced.

collect2.exe: error: ld returned 1 exit status no DLL was created

Hello I am trying to make a package in R using Rcpp.
When I have just a single file, every thing is ok and my package(foad2) is compiled completely.
But, when I have more than one .cpp files (they do not depend on each other and each one contains only one function), I will be exposed to the following error
C:/RBuildTools/3.5/mingw_64/bin/g++ -std=gnu++11 -I"C:/PROGRA~1/R/R-36~1.3/include" -DNDEBUG -I"C:/Users/Foad/Documents/R/win-library/3.6/Rcpp/include" -O2 -Wall -mtune=core2 -c myFunc.cpp -o myFunc.o
C:/RBuildTools/3.5/mingw_64/bin/g++ -std=gnu++11 -I"C:/PROGRA~1/R/R-36~1.3/include" -DNDEBUG -I"C:/Users/Foad/Documents/R/win-library/3.6/Rcpp/include" -O2 -Wall -mtune=core2 -c rcpp_hello_world.cpp -o rcpp_hello_world.o
C:/RBuildTools/3.5/mingw_64/bin/g++ -std=gnu++11 -shared -s -static-libgcc -o foad2.dll tmp.def RcppExports.o myFunc.o rcpp_hello_world.o -LC:/PROGRA~1/R/R-36~1.3/bin/x64 -lR
RcppExports.o:RcppExports.cpp:(.rdata+0x6f8): undefined reference to `_foad2_rcpp_myFunc'
collect2.exe: error: ld returned 1 exit status
no DLL was created
ERROR: compilation failed for package 'foad2'
* removing 'C:/Users/Foad/Documents/R/win-library/3.6/foad2'
Exited with status 1.
my Cpp Codes are:
my 1st cpp file:
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
List rcpp_hello_world() {
CharacterVector x = CharacterVector::create( "foo", "bar" ) ;
NumericVector y = NumericVector::create( 0.0, 1.0 ) ;
List z = List::create( x, y ) ;
return z ;
}
my 2nd cpp file:
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
NumericVector myFunc(NumericVector x) {
return x * 2;
}
I would be greatful if anybody tell me how I can fix this problem?