Cannot make a successful pass in travis-ci for my C++ app - c++

I am working on this project an it compiles fine in my desktop using g++ (Ubuntu 4.8.4-2ubuntu1~14.04) 4.8.4 and cmake version 2.8.12.2.
I am new in travis-ci thing, I have wrote a .travis.yml script as follow
language: cpp
compiler:
- g++
addons:
apt:
sources:
- ubuntu-toolchain-r-test
packages:
- gcc-4.8
- g++-4.8
- libboost-all-dev
- cmake
install:
- if [ "$CXX" = "g++" ]; then export CXX="g++-4.8 -std=c++11 -I/usr/include/boost -DENABLE_REINFORCEMENT_LEARNING" CC="gcc-4.8"; fi
before_script:
- mkdir build
- cd build
- cmake ..
script: make
My project has boost library dependency.
My problem is the travis-ci indicates compiler error in my timer variable.
#include "stdafx.hpp"
#include <list>
#include <mutex>
#include <atomic>
#include <thread>
#include <fstream>
#include <sstream>
#include <signal.h>
#include <iostream>
#include <functional>
#include "timer.hpp"
#include "configs.hpp"
#include "incurses.hpp"
#include "quadrotor.hpp"
#include "main.helper.hpp"
#include "main.output.hpp"
#ifdef ENABLE_REINFORCEMENT_LEARNING
# include "RLearner.Sarsa.hpp"
#endif
volatile bool
sig_ctrl_c = false;
std::mutex log_lock;
> timer screener; <
// /home/travis/build/noise2/quadrotor-sim/main.cpp:27:1: error: ‘timer’ does not name a type
// timer screener;
scalar iter_simulation = 0;
const size_t max_iter_simulation = 1e+5;
You can see the travis-ci result here.
Quesions
1) Why on earth this is an error when I have a successful compile in my desktop? [i.e, what am i doing wrong?]
2) How to make my project pass the travis-ci?
Edit
Note that class timer has been included already.

Thanks to #nfranklin I have noticed what was the problem. For what it worth to solve the problem I had to install one of the latest boost version, I came up with following travis.yml script.
language: cpp
compiler:
- g++
addons:
apt:
sources:
- ubuntu-toolchain-r-test
- boost-latest
packages:
- gcc-4.8
- g++-4.8
- libboost1.55-all-dev
- cmake
install:
- if [ "$CXX" = "g++" ]; then export CXX="g++-4.8 -std=c++11 -DENABLE_REINFORCEMENT_LEARNING" CC="gcc-4.8"; fi
before_script:
- mkdir build
- cd build
- cmake ..
script: make
Now it is all good :)

I think the problem lies within the following step in your travis-ci config file:
install:
- if [ "$CXX" = "g++" ]; then export CXX="g++-4.8 -std=c++11 -I/usr/include/boost -DENABLE_REINFORCEMENT_LEARNING" CC="gcc-4.8"; fi
timer.hpp is being included from boost (i.e. boost/timer.hpp) due to -I/usr/include/boost instead ofthe timer.hpp that you wrote.
You can remove the -I/usr/include/boost from that step . /usr/include is automatically searched for headers by gcc and and so the boost headers that you include (e.g. #include ) will be found.

Related

C++ writing JSON with JSONCPP

I was trying to use the JSONCPP library to work with JSON documents in C++.
Reading another question here, I saw this example and tried to implement:
#include "json/json.h"
int main(){
Json::Value event;
Json::Value vec(Json::arrayValue);
vec.append(Json::Value(1));
vec.append(Json::Value(2));
vec.append(Json::Value(3));
event["competitors"]["home"]["name"] = "Liverpool";
event["competitors"]["away"]["code"] = 89223;
event["competitors"]["away"]["name"] = "Aston Villa";
event["competitors"]["away"]["code"] = vec;
std::cout << event << std::endl;
}
But I keep getting this error:
json2.cpp:(.text+0x1c): referência não definida para "Json::Value::Value(Json::ValueType)"
/usr/bin/ld: json2.cpp:(.text+0x30): referência não definida para "Json::Value::Value(Json::ValueType)"
json2.cpp is the file's name, as it looks like.
Being a /usr/bin/ld a link error, I tried to add as parameter the directory path to the compile command but the error persists
Does anyone know how to fix it?
It’s all about how you build/install the library and specify the headers in the source. Since you didn’t mention what method you took to build the library, I’ll demonstrate three solutions I got to work on Ubuntu 20.04 LTS.
Install the Package
Most straightforward way is to install via apt:
$ sudo apt-get install libjsoncpp-dev
The header files will be installed to /usr /include/jsoncpp/json and can be included in json2.cpp as:
#include <jsoncpp/json/json.h>
And compile by adding the linker flag as:
$ g++ json2.cpp -o json2 -ljsoncpp
Amalgamated Source
Inside of the top-level directory of the jsoncpp repository, you can amalgamate the source by:
$ python amalgamate.py
By default, this will generate all necessary source and header files in the dist/ directory, and there is no need to link the library. Include in json2.cpp as:
#include <json/json.h>
As indicated in the build instructions, jsoncpp.cpp will have to be incorporated into your project:
$ g++ json2.cpp dist/jsoncpp.cpp -Idist -o json2
Cmake
From the top-level directory of the jsoncpp repository:
$ mkdir build
$ cd build
$ cmake -DCMAKE_BUILD_TYPE=release -DBUILD_STATIC_LIBS=ON -DBUILD_SHARED_LIBS=OFF -DARCHIVE_INSTALL_DIR=. -G "Unix Makefiles" ..
$ make; make install
This will install the header files to /usr/local/include/json. Include in json2.cpp as:
#include <json/json.h>
And compile by adding the linker flag as:
$ g++ json2.cpp -o json2 -ljsoncpp

Tensorflow c++ api

I recently tensorflow cpp api from a way below(on MacOS Catalina ):
1. I first Install Bazel 0.23.0
2. download tensorflow:
$ git clone https://github.com/tensorflow/tensorflow
cd tensorflow # cd to the top-level directory created
$ ./configure
bazel build //tensorflow:libtensorflow_cc.so
# build C library
$ bazel build //tensorflow:libtensorflow.so
After install I run the code below:
#include "tensorflow/core/public/session.h"
#include "tensorflow/core/framework/tensor.h"
#include "tensorflow/core/platform/env.h"
int main(){
return 0;
}
with. command below:
g++ -I/usr/local/Cellar/libtensorflow/2.1.0_1/include -L/usr/local/Cellar/libtensorflow/2.1.0_1/lib -ltensorflow -ltensorflow_cc -ltensorflow_framework -std=c++11 main.cc -o main.o
but I get Error below:
/usr/local/Cellar/libtensorflow/2.1.0_1/include/tensorflow/core/framework/device_attributes.pb.h:17:2: error:
This file was generated by an older version of protoc which is
#error This file was generated by an older version of protoc which is
^
/usr/local/Cellar/libtensorflow/2.1.0_1/include/tensorflow/core/framework/device_attributes.pb.h:18:2: error:
incompatible with your Protocol Buffer headers. Please
#error incompatible with your Protocol Buffer headers. Please
I used libprotoc 3.11.4.
what should I do? what is wrong?

How to use OpenBlas Lapacke together with Rcpp

I have some running c++ code using the Lapacke version that comes with OpenBlas. I would like to include this code into an R package and transfer data between that function and R using the Rcpp package. But somehow the two seem not to like each other. As soon as I have #include <lapacke.h> and #include <Rcpp.h> in one source file it isn't compiling anymore. Both separately work fine. There is a whole bunch off error messages which as far as I can tell say that Rcpp is broken (e.g /home/Alex/R/x86_64-pc-linux-gnu-library/3.4/Rcpp/include/Rcpp/traits/traits.h:32:15: error: expected ‘)’ before ‘__extension__’).
I have no idea why this happens. Is there a way to use both at the same time?
Or should I do something completely different?
Here is a minimal example that gives me the error:
I created a package using
Rcpp::Rcpp.package.skeleton("LT", example_code = FALSE)
I added a .cpp file to /src containing
#include <lapacke.h>
#include <Rcpp.h>
int test_LAPACK(){
return(1);
}
I added a Makvars file to /src containing
PKG_CXXFLAGS = -I/opt/OpenBLAS/include
PKG_LIBS = -L/opt/OpenBLAS/lib -lopenblas -lpthread -lgfortran
CXX_STD = CXX11
Compile and install
Rcpp::compileAttributes("LT")
devtools::install("LT")
It actually works on my system following a standard sudo apt install liblapacke-dev provided I also change the include order.
Witness:
Source
rob:/tmp/lapacke/LT$ cat src/lt.cpp
#include <Rcpp.h>
#include <lapacke.h>
int test_LAPACK(){
return(1);
}
rob:/tmp/lapacke/LT$ ls src/ ## no Makevars needed
lt.cpp
rob:/tmp/lapacke/LT$
Build
rob:/tmp/lapacke/LT$ build.r
* checking for file ‘./DESCRIPTION’ ... OK
* preparing ‘LT’:
* checking DESCRIPTION meta-information ... OK
* cleaning src
* installing the package to process help pages
* saving partial Rd database
* cleaning src
* checking for LF line-endings in source and make files and shell scripts
* checking for empty or unneeded directories
Removed empty directory ‘LT/R’
* building ‘LT_1.0.tar.gz’
rob:/tmp/lapacke/LT$
Install
rob:/tmp/lapacke/LT$ install.r LT_1.0.tar.gz
* installing *source* package ‘LT’ ...
** libs
ccache g++ -I"/usr/share/R/include" -DNDEBUG -I"/usr/local/lib/R/site-library/Rcpp/include" -fpic -g -O3 -Wall -pipe -march=native -c lt.cpp -o lt.o
ccache g++ -Wl,-S -shared -L/usr/lib/R/lib -Wl,-Bsymbolic-functions -Wl,-z,relro -o LT.so lt.o -L/usr/lib/R/lib -lR
installing to /usr/local/lib/R/site-library/LT/libs
** help
*** installing help indices
** building package indices
** testing if installed package can be loaded
* DONE (LT)
rob:/tmp/lapacke/LT$
Run
(After I added a line // [[Rcpp::export]], ran compileAtttributes() and rebuilt and installed.)
rob:/tmp/lapacke/LT$ r -lLT -p -e'test_LAPACK()'
[1] 1
rob:/tmp/lapacke/LT$
Summary
Check your compiler. There is no fundamental reason this should not work, and it works here (Ubuntu 18.04).

Compiler unable to link/find GLFW [duplicate]

This question already has answers here:
How do I link GLFW
(3 answers)
Closed 4 years ago.
I am trying to run a modified program that can be found at cpp-pointcloud.cpp. I am having difficulties getting it to build.
As it is now my file structure is:
-pointcloud.cpp
-librealsense/
-examples
-example.hpp
And I am building it with the command:
g++ -std=c++11 pointcloud.cpp -lrealsense -lopencv_core -lopencv_imgproc -lopencv_highgui -o ir
The imports of the pointcloud.cpp file are as follows:
// License: Apache 2.0. See LICENSE file in root directory.
// Copyright(c) 2015 Intel Corporation. All Rights Reserved.
#include <librealsense/rs.hpp>
#include <librealsense2/rs.hpp>
#include "librealsense/examples/example.hpp"
#include <chrono>
#include <vector>
#include <sstream>
#include <iostream>
#include <algorithm>
And they are:
#pragma once
#define GLFW_INCLUDE_GLU
#include <GLFW/glfw3.h>
#include <vector>
#include <string>
#include <sstream>
#include <iostream>
#include <algorithm>
In the example.hpp file.
My issue is that it cannot seem to find the glfw files. I get a bunch of errors like:
pointcloud.cpp:(.text+0xdf5): undefined reference to `glPopMatrix'
Which is bizarre to me as I'm confident that i've installed the GLFW3 package. And I believed that includes that use the angle brackets search for installed packages on your operating system.
I've ran this command successfully:
sudo apt-get install libglfw3 libglfw3-dev
I tried adding
#include <GLFW/glfw3.h>
to the pointcloud.cpp file but it actually gave me more errors!
I miss be misunderstanding something conceptually here, so if any of you could point me in the right direction that would be great thanks.
You have to link GLFW with pkg-config cf How do I link GLFW
$ gcc `pkg-config --cflags glfw3` -o foo foo.c `pkg-config --static --libs glfw3`
you can locate the libraries using locate or whereis ( update database before with sudo updatedb , https://unix.stackexchange.com/questions/194088/how-to-check-if-a-shared-library-is-installed ) or find ( /www.lifewire.com/uses-of-linux-command-find-2201100 ) i.e.
sudo updatedb ( may take some time ... )
locate libglfw
or
sudo find /usr/lib -name '*libglfw*'
possibly also check all dependencies required for libglfw using ldd

How to (easily) wrap a c++ function unto an easy to install R 'package'

Suppose I have an c++ code (see below for a simple example).
I want to make it easy for a journal referee to install/run
it.
So i figured the easiest way is to warp it unto a simplified
R package-like tar.gz file so the referee could install it
by simply invoking install.packages to a local .tar.gz file.
The reason for this is that i do not know what machine the
referee is using, but i'm pretty sure the referee would
know how to install a R packages so it's much easier for
me to warp my code as a R 'package' --or at any rate,
something sufficiently similar to it that it could be
installed by a simple call to install.package().
An answer to a earlier question seems to suggest this
is indeed possible. I followed the suggestions therein and
created a /src directory with my cpp code (the one shown
below) and a Makevars.win file containing:
## This assume that we can call Rscript to ask Rcpp about its locations
## Use the R_HOME indirection to support installations of multiple R version
#PKG_LIBS = $(shell $(R_HOME)/bin/Rscript.exe -e "Rcpp:::LdFlags()")
PKG_CPPFLAGS = -I../inst/include -I.
PKG_CXXFLAGS = -DEIGEN_DONT_PARALLELIZE $(SHLIB_OPENMP_CXXFLAGS)
PKG_LIBS = $(shell $(R_HOME)/bin/Rscript.exe -e "Rcpp:::LdFlags()") $(SHLIB_OPENMP_CXXFLAGS)
and a Makevars file containing:
## Use the R_HOME indirection to support installations of multiple R version
#PKG_LIBS = `$(R_HOME)/bin/Rscript -e "Rcpp:::LdFlags()"`
# This was created by RcppEigen.package.skeleton, but the R script that is
# called creates error message:
# PKG_CPPFLAGS = `$(R_HOME)/bin/Rscript -e "RcppEigen:::RcppEigenCxxFlags()"`
PKG_CPPFLAGS = -I../inst/include
PKG_CXXFLAGS = -DEIGEN_DONT_PARALLELIZE $(SHLIB_OPENMP_CXXFLAGS)
PKG_LIBS = `$(R_HOME)/bin/Rscript -e "Rcpp:::LdFlags()"` $(SHLIB_OPENMP_CXXFLAGS)
e.g. I simply followed the answer in the SO post to look around for how
this is done in other packages (I also add RcppEigen to the list of dependencies because
this guarantees that Eigen is installed on the target machine). I also created a /R directory containing the file MSE.R which contains:
fx01<-function(x){
x<-as.matrix(x)
Dp<-rep(0,ncol(x))
fit<-.C("mse",as.integer(nrow(x)),as.integer(ncol(x)),as.single(x),as.single(Dp))
as.numeric(fit[[4]])
}
and an empty /inst/include and a /man directory containing a minimal (but valid) .Rd file.
I've added a NAMESPACE file containing:
import(Rcpp)
import(RcppEigen)
useDynLib(MySmallExample)
Here is the question:
the c++ function otherwise compiles/runs fine. Is there a way to warp it unto a R package so as to make it easy to install/run by a third person.
Here is the c++ code used for this example.
#include <algorithm>
#include <cstdlib>
#include <ctime>
#include <functional>
#include <fstream>
#include <iostream>
#include <math.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <sstream>
#include <vector>
#include <Eigen/Dense>
#include <Eigen/LU>
#include <Eigen/SVD>
using namespace std;
using namespace Eigen;
using Eigen::MatrixXf;
using Eigen::VectorXf;
float median(VectorXf& x) {
int n=x.rows();
int half=(n+1)/2;
half--;
float med;
nth_element(x.data(),x.data()+half,x.data()+x.size());
if((n%2)==1){
med=x(half);
} else {
float tmp0=x(half);
float tmp1=x.segment(half+1,half-1).minCoeff();
med=0.5*(tmp0+tmp1);
}
return med;
}
VectorXf fx01(MatrixXf& x){
int p=x.cols();
int n=x.rows();
VectorXf Recept(n);
VectorXf Result(p);
for(int i=0;i<p;i++){
Recept=x.col(i);
Result(i)=median(Recept);
}
return Result;
}
extern "C"{
void mse(int* n,int* p,float* x,float* medsout){
MatrixXf x_cen=Map<MatrixXf>(x,*n,*p);
VectorXf MedsOut=fx01(x_cen);
Map<VectorXf>(medsout,*p)=MedsOut.array();
}
}
Have you read the `Writing R Extensions' manual about how to interface with R?
You are of course free to do this without Rcpp, but we wrote Rcpp for our use as we find that it makes these very interchanges easier. 94 packages on CRAN which use Rcpp appear to agree...
You are using Eigen, and you want this packaged for a third party ("a referee"). Now, if you were to use RcppEigen you would assured that Eigen is present as it is inside RcppEigen. With what you do, you're not...
Plus, .C() is a much more restrictive interface that .Call().
It's very important to have a proper DESCRIPTION file.
I used this one:
Package: MySmallExample
Type: Package
Title: MysmallExample
Version: 0.0.0
Date: 2012-12-24
Depends: Rcpp (>= 0.9.10)
Imports: RcppEigen (>= 0.2.0)
Suggests: mvtnorm
LinkingTo: Rcpp, RcppEigen
Description: A small minimal Package.
License: GPL (>= 2)
LazyLoad: yes
Authors#R: person("joe", "programer", email =
"joe.programer#joe_inc.com", role = c("aut", "cre"))
Collate: 'MSE.R'
Packaged: 2012-12-24 12:34:56 UTC; andi
Author: joe programer [aut, cre]
Maintainer: joe programer <joe.programer#joe_inc.com>
Repository: CRAN
Date/Publication: 2012-12-24 12:34:56
The most important thing seems to be the 'Collate:' field: it should properly list
all the .R files in the /R directory. The Depends & Imports field should also be consistent with the NAMESPACE file.
It is also very important that the file NAMESPACE contains a line
export("fx01","fx02")
where "fx01","fx02" are the name of all the R functions in the
/R/*.R files (in this case, just fx01).
Then, I wrapped the whole thing unto a .tar.gz. and ran
install.packages("MySmallExample.tar.gz",repos=NULL,type="source")
> install.packages("/MySmallExample.tar.gz",repos=NULL,type="source")
Installing package(s) into ‘/R/x86_64-pc-linux-gnu-library/2.15’
(as ‘lib’ is unspecified)
* installing *source* package ‘MySmallExample’ ...
** libs
g++ -I/usr/share/R/include -DNDEBUG -I../inst/include -I"/R/x86_64-pc-linux-gnu-library/2.15/Rcpp/include" -I"/R/x86_64-pc-linux-gnu-library/2.15/RcppEigen/include" -DEIGEN_DONT_PARALLELIZE -fopenmp -fpic -O3 -pipe -g -c MSE.cpp -o MSE.o
g++ -shared -o MySmallExample.so MSE.o -L/R/x86_64-pc-linux-gnu-library/2.15/Rcpp/lib -lRcpp -Wl,-rpath,/R/x86_64-pc-linux-gnu-library/2.15/Rcpp/lib -fopenmp -L/usr/lib/R/lib -lR
installing to /R/x86_64-pc-linux-gnu-library/2.15/MySmallExample/libs
** R
** inst
** preparing package for lazy loading
** help
*** installing help indices
** building package indices
** testing if installed package can be loaded
* DONE (MySmallExample)