In boost::filesystem, the path class always tries to dereference symlinks. A lot of the API is catered towards trying to make symlinks seem invisible. I'm guessing a lot of their syscalls underneath are stat related instead of lstat related. This is problematic for me, as I'm trying to get the symlink itself.
For example, doing fs::exists("some_symlink") because, although some_symlink exists, its referent does not exist. I want this to give me back true, and in the older versions of boost it supports it: https://www.boost.org/doc/libs/1_32_0/libs/filesystem/doc/operations.htm#symbolic_link_exists
However, now it doesnt. Is there any good way of doing this?
You can use boost::filesystem::symlink_status to get a file_status, and then check its type().
#include <boost/filesystem.hpp>
#include <iostream>
#include <iomanip>
int main(){
auto a = boost::filesystem::symlink_status("a_symlink");
auto b = boost::filesystem::symlink_status("main.cpp");
auto c = boost::filesystem::symlink_status("not_existing");
auto d = boost::filesystem::symlink_status("a_broken_symlink");
std::cout << std::boolalpha
<< (a.type() == boost::filesystem::file_not_found) << " "
<< (b.type() == boost::filesystem::file_not_found) << " "
<< (c.type() == boost::filesystem::file_not_found) << " "
<< (d.type() == boost::filesystem::file_not_found) << "\n";
}
Execution:
$ touch f
$ ln -s main.cpp a_symlink
$ ln -s f a_broken_symlink
$ rm f
$ ls -l
total 4
lrwxrwxrwx 1 2001 2000 1 Jun 3 23:33 a_broken_symlink -> f
lrwxrwxrwx 1 2001 2000 8 Jun 3 23:33 a_symlink -> main.cpp
-rw-rw-rw- 1 2001 2000 637 Jun 3 23:33 main.cpp
$ g++ -std=c++14 -Wall -Wextra -pedantic -lboost_filesystem -lboost_system main.cpp && ./a.out
false false true false
Live on Coliru
Related
This question already has answers here:
Change current process environment's LD_LIBRARY_PATH
(5 answers)
Closed 2 months ago.
int main(int argc, char** argv) {
std::string r = ::file::RLocation("tensor_rt/lib");
setenv("LD_LIBRARY_PATH", r.c_str(), 1);
std::string filename = "libnvinfer.so.8";
CHECK(std::filesystem::exists(r+"/"+filename));
void * handle = dlopen(filename.c_str(), RTLD_NOW | RTLD_LOCAL);
CHECK(handle);
filename = "libnvinfer.so.8.5.1";
CHECK(std::filesystem::exists(r+"/"+filename));
handle = dlopen(filename.c_str(), RTLD_NOW | RTLD_LOCAL);
CHECK(handle) << dlerror();
}
This program fails at the last CHECK
F20221212 15:23:07.725180 2924123 test.cc:24] Check failed: handle libnvinfer.so.8.5.1: cannot open shared object file: No such file or directory
Why does it work with the first dlopen but not with the second one? The two files should be the same.
Edit:
ls -l gives
lrwxrwxrwx 1 shshao shshao 19 Dec 12 15:20 libnvinfer.so -> libnvinfer.so.8.5.1
lrwxrwxrwx 1 shshao shshao 19 Dec 12 15:20 libnvinfer.so.8 -> libnvinfer.so.8.5.1
-rwxr-xr-x 1 shshao shshao 487512744 Oct 27 15:37 libnvinfer.so.8.5.1
It's because unlike other env variables, LD_LIBRARY_PATH can't be modified within a program: https://groups.google.com/g/comp.lang.java.programmer/c/LOu18-OWAVM/m/b0YBJhoKS04J
I am trying to understand glog and therefore trying to run the example code on their github page.
I have installed glog (version - 0.6.0) and its dependency gflags (version - 2.2) on my mac OS (10.15.7)
I compile the example below
#include <glog/logging.h>
int main(int argc, char* argv[]) {
// Initialize Google’s logging library.
google::InitGoogleLogging(argv[0]);
// test with setting a value for num_cookies
int num_cookies = 3;
// ...
LOG(INFO) << "Found " << num_cookies << " cookies";
}
using the following command (and it compiles without any errors or warnings).
g++ glog-test.cpp -I/usr/local/include -L/usr/local/lib -lglog -lgflags -o glog-test.o
When I run the example using the following command,
./glog-test.o --logtostderr=1 --stderrthreshold=0
I expect to see the message Found 3 cookies on my terminal, but I see nothing being printed.
I have also experimented with different values for logtostderr (0, 1) and stderrthreshold (0, 1, 2, 3) and nothing gets written to the directory or gets printed on the terminal.
Any help in understanding what I am doing wrong here would be much appreciated, thank you!
You have to parse the command line flags manually through gflags::ParseCommandLineFlags
#include <glog/logging.h>
#incldue <gflags/gflags.h>
int main(int argc, char* argv[]) {
// Initialize Google’s logging library.
google::InitGoogleLogging(argv[0]);
gflags::ParseCommandLineFlags(&argc, &argv, true);
// test with setting a value for num_cookies
int num_cookies = 3;
// ...
LOG(INFO) << "Found " << num_cookies << " cookies";
}
Just checked, the following works..
GLOG_stderrthreshold=0 GLOG_logtostderr=1 ./glog-test.o
which gives
Found 3 cookies
I do have gflags and installed, as seen by
cd /usr/local/lib
ls -l | grep "libgflags"
which gives
-lrwxr-xr-x 1 sn admin 48 Jun 5 2020 libgflags.2.2.2.dylib -> ../Cellar/gflags/2.2.2/lib/libgflags.2.2.2.dylib
lrwxr-xr-x 1 sn admin 46 Jun 5 2020 libgflags.2.2.dylib -> ../Cellar/gflags/2.2.2/lib/libgflags.2.2.dylib
-rw-r--r-- 1 sn admin 170520 Jun 27 00:35 libgflags.a
lrwxr-xr-x 1 sn admin 42 Jun 5 2020 libgflags.dylib -> ../Cellar/gflags/2.2.2/lib/libgflags.dylib
lrwxr-xr-x 1 sn admin 58 Jun 5 2020 libgflags_nothreads.2.2.2.dylib -> ../Cellar/gflags/2.2.2/lib/libgflags_nothreads.2.2.2.dylib
lrwxr-xr-x 1 sn admin 56 Jun 5 2020 libgflags_nothreads.2.2.dylib -> ../Cellar/gflags/2.2.2/lib/libgflags_nothreads.2.2.dylib
-rw-r--r-- 1 sn admin 168096 Jun 27 00:35 libgflags_nothreads.a
lrwxr-xr-x 1 sn admin 52 Jun 5 2020 libgflags_nothreads.dylib -> ../Cellar/gflags/2.2.2/lib/libgflags_nothreads.dylib
so I am not sure why
./glog-test.o --logtostderr=1 --stderrthreshold=0
didn't work.
HI I'm trying to log a sensor data to CSV file. I want the first row to be hh:mm:ss:ms.
The Code I have is
#include <chrono>
#include <ctime>
eyeDataLog << std::ctime(&today_time) << ","
<< gazeleft[0] << ","
<< gazeleft[1] << ","
|Timestamp2 |left gaze X |left gaze Y |left gaze Z |right gaze X
|Tue Mar 1 18:19:31 2022 |0 |0 |0 |0
how to add milliseconds to the logged time
regared thanks
You may try strftime or other library like fmt which is included in c++20.
Update: even you don't use C++20, fmt library itself is compatible with c++11 and above.
Since you have formatting requirement in logging, fmt might fit well.
Demo for fmt
#include <fmt/chrono.h>
int main() {
using namespace std::literals::chrono_literals;
fmt::print("Default format: {} {}\n", 42s, 100ms);
// %S prints milliseconds
fmt::print("strftime-like format: {:%H:%M:%S}\n", 3h + 15min + 30s + 15ms);
}
I'm running into a problem using RInside vs the console. This is all run on ubuntu 14.04 using R 3.2.4 installed via apt-get from CRAN. Here is the c++ and R code:
#include <RInside.h> // for the embedded R via RInside
int main(int argc, char *argv[]) {
RInside R(argc, argv); // create an embedded R instance
R.parseEval("source('abline.R')");
}
abline.R
bp <- data.frame(
age = c(28, 23, 52, 42, 27, 29, 43, 34, 40, 28),
systolic = c(70, 68, 90, 75, 68, 80, 78, 70, 80, 72))
str(bp)
attach(bp)
bp.lm <- lm(systolic ~ age)
plot(age, systolic)
abline(bp.lm)
lines(lowess(age, systolic, f=0.75), lty=2)
The R code works fine from the console, but errors when the program is run.
mlilback#rc2x:/tmp/abtest$ ./abtest
'data.frame': 10 obs. of 2 variables:
$ age : num 28 23 52 42 27 29 43 34 40 28
$ systolic: num 70 68 90 75 68 80 78 70 80 72
Error in if (noInt) { : argument is of length zero
terminate called after throwing an instance of 'std::runtime_error'
what(): Error evaluating: source('abline.R')
Aborted (core dumped)
The if (noInt) { is from the source of abline (line 18 in my version of R). I'm completely stumped as to why this only happens via RInside.
Any ideas?
Works for me without any issues. Ubuntu 16.04. Running out of the examples directory to get the GNUmakefile-based build for free:
~/git/rinside/inst/examples/standard(master)$ vi soquestion.cpp
~/git/rinside/inst/examples/standard(master)$ make soquestion
ccache g++ -I/usr/share/R/include -I/usr/local/lib/R/site-library/Rcpp/include \
-I/usr/local/lib/R/site-library/RInside/include -g -O3 -Wall -pipe \
-Wno-unused -Wall soquestion.cpp -Wl,--export-dynamic -fopenmp \
-L/usr/lib/R/lib -lR -lpcre -llzma -lbz2 -lz -lrt -ldl -lm -lblas -llapack \
-L/usr/local/lib/R/site-library/RInside/lib -lRInside \
-Wl,-rpath,/usr/local/lib/R/site-library/RInside/lib -o soquestion
~/git/rinside/inst/examples/standard(master)$ vi abline.R
~/git/rinside/inst/examples/standard(master)$ ./soquestion
'data.frame': 10 obs. of 2 variables:
$ age : num 28 23 52 42 27 29 43 34 40 28
$ systolic: num 70 68 90 75 68 80 78 70 80 72
~/git/rinside/inst/examples/standard(master)$
I literally just copied an pasted your two files. Also:
~/git/rinside/inst/examples/standard(master)$ ls -1tr | tail -4
soquestion.cpp
soquestion
abline.R
Rplots.pdf
~/git/rinside/inst/examples/standard(master)$
You probably want to open a device file via pdf() or png() ...
I am trying to report errors from my rcpp code. I am using the constructor exception (const char *message_, const char *file, int line) from http://dirk.eddelbuettel.com/code/rcpp/html/classRcpp_1_1exception.html. To isolate the problem, I wrote the following bar.cpp:
#include <Rcpp.h>
RcppExport SEXP bar( SEXP x){
throw(Rcpp::exception("My Error Message","bar.cpp",4));
return x ;
}
When I run it in R, this is what I get:
> dyn.load("bar.so")
> is.loaded("bar")
[1] TRUE
> .Call("bar",12)
Error: SET_VECTOR_ELT() can only be applied to a 'list', not a 'NULL'
>
You can either
use the inline package which puts a try/catch block into the function it generates for you (by using two simple macros)
or do it manually yourself as shown in a bunch of examples on my blog, or in the examples/ section of the Rcpp package,
but doing what you (ie: throwing outside of a try/catch block) can never work.
As an added bonus, here is a complete example (which essentially already existed in the unit tests):
R> library(inline)
R> f <- cxxfunction(signature(), plugin="Rcpp", body='
+ throw std::range_error("boom");
+ return R_NilValue;
+ ')
R> f()
Error in f() : boom
R>
Again, cxxfunction() puts a try/catch() block in here for you as you can see if you turn verbose on:
R> f <- cxxfunction(signature(), plugin="Rcpp", body='
+ throw std::range_error("boom");
+ return R_NilValue;
+ ', verbose=TRUE)
>> setting environment variables:
PKG_LIBS = -L/usr/local/lib/R/site-library/Rcpp/lib \
-lRcpp -Wl,-rpath,/usr/local/lib/R/site-library/Rcpp/lib
>> LinkingTo : Rcpp
CLINK_CPPFLAGS = -I"/usr/local/lib/R/site-library/Rcpp/include"
>> Program source :
1 :
2 : // includes from the plugin
3 :
4 : #include <Rcpp.h>
5 :
6 :
7 : #ifndef BEGIN_RCPP
8 : #define BEGIN_RCPP
9 : #endif
10 :
11 : #ifndef END_RCPP
12 : #define END_RCPP
13 : #endif
14 :
15 : using namespace Rcpp;
16 :
17 :
18 :
19 : // user includes
20 :
21 :
22 : // declarations
23 : extern "C" {
24 : SEXP file4cc53282( ) ;
25 : }
26 :
27 : // definition
28 :
29 : SEXP file4cc53282( ){
30 : BEGIN_RCPP
31 :
32 : throw std::range_error("boom");
33 : return R_NilValue;
34 :
35 : END_RCPP
36 : }
37 :
38 :
Compilation argument:
/usr/lib/R/bin/R CMD SHLIB file4cc53282.cpp 2> file4cc53282.cpp.err.txt
ccache g++-4.6 -I/usr/share/R/include \
-I"/usr/local/lib/R/site-library/Rcpp/include" \
-fpic -g0 -O3 -Wall -pipe -Wno-unused -pedantic -c file4cc53282.cpp \
-o file4cc53282.o
g++ -shared -o file4cc53282.so file4cc53282.o \
-L/usr/local/lib/R/site-library/Rcpp/lib \
-lRcpp -Wl,-rpath,/usr/local/lib/R/site-library/Rcpp/lib \
-L/usr/lib/R/lib -lR
R>
The BEGIN_RCPP and END_CPP add the magic you need here.
Please do move your questions to rcpp-devel.
Just wrap your code inside BEGIN_RCPP/END_RCPP:
RcppExport SEXP bar( SEXP x){
BEGIN_RCPP
throw(Rcpp::exception("My Error Message","bar.cpp",4));
return x ;
END_RCPP
}
Note that you can throw normal std exceptions too:
throw std::invalid_argument("'x' is too short");