Rcpp C++11 .Call issues under Windows - regex

I'm experiencing .Call issues when running functions built with Rcpp on Windows, if my c++ code uses C++11 std::regex and I have found no way out so far.
Unlike prior questions on similar issues, I have had neither building nor linking issues. The Rcpp package builds and links fine using the C++11 plugin, making usable packages on my platform. constexpr and C++11-specific functions like std::stoi cause no issue when std::regex is not used.
Using Windows boost libs, I experienced linking issues, even when specifying PKG_LIBS="-L/path/to/boost/libs -lboost_regex", so I'd rather stick to std::regex.
The same packages build, install and run fine under linux, using vanilla std::regex or boost::regex.
I unfortunately found no solution in the fine Rcpp gallery examples.
Windows platform is :
R version 3.2.3 (2015-12-10)
x86_64-w64-mingw32/x64 (64-bit)
Running under:
Windows >= 8 x64 (build 9200)
Rcpp_0.12.3
Rtools 3.3.0.1959 running g++ 4.9.3 (x86_64-posix-seh,
built by MinGW-W64 project), normally C++11-compatible.
PKG_CXXFLAGS="-std=c++11"
The linux platform is similar except for g++ (version 5.3).
Below is a simplified code chunk for duplication.
#include <Rcpp.h>
#if defined(__linux__) && ! defined(FORCE_STL_BUILD)
#include <boost/regex.hpp>
#define reglib boost
#else
#include <regex>
#define reglib std
#endif
#include <string>
using namespace Rcpp;
// [[Rcpp::plugins(cpp11)]]
constexpr int a[3]= {2, 10, 15};
// [[Rcpp::export]]
int my_test(int prop, const std::string& index)
{
#ifndef NO_REG
static const reglib::regex test {"H.*A", reglib::regex::icase};
#endif
int index_int = std::stoi(index) + a[1] + prop;
return index_int;
}
This code runs OK when built using -DNO_REG. Otherwise invoking test::my_test(1, "1000") returns:
`Error in .Call("test_my_test", PACKAGE = "test", prop, index) :
"test_my_test" not available for .Call() for package "test"`
EDIT:
1. The question focuses on std::regex. Boost issues are only incidental comments.
2. Issues only arise after packaging, not using Rcpp::source("cppfile")
3. Packaging code:
R console:
Rcpp::Rcpp.package.skeleton("test", attributes=TRUE, example_code=FALSE, cpp_files="test.cpp")
Rcpp::compileAttributes("test")
CMD console:
REM paths to R/bin/x64 and Rtools/bin, Rtools/mingw_64/bin added to PATH
set PKG_CXXFLAGS=-std=c++11
R CMD build test
R CMD INSTALL test_1.0.tar.gz
ADDITIONAL EDIT:
.Call issues arise as soon as a regex is declared in the C++ code. Using it or no (as in std::regex_match) makes no change.

Can you try disentangling this some more? You are mixing a lot of things here.
Try maybe 'just' C++ from R first, with the newer g++ 4.9.3 compiler and see if that lets you use Boost as you hope. Your use case there is local and non-standard, so you have to work this out. We generally just recommend using BH without linking.
I don't actually see an Rcpp issue here. You are simply pushing the (working, tested, trusted) Rcpp setup into a corner it has not been used in yet. So you may need to work some things out yourself.
Also note that g++ 4.9.3 for R is not really released yet.

Related

Linker hangs when compiling a simple eigen3 program on Win10

I am using the build tools provided by Rtools.
gcc version 8.3.0 x86_64-w64-mingw32 (Built by Jeronen for the R-project)
GNU ld version 2.33.1
eigen version 3.4.0
I have been testing various functions of the eigen package, and when I calculated the singular values using the BDCSVD object, the linker just hangs with full cpu usage. The following line is causing the trouble:
BDCSVD<MatrixXd> svd(m, ComputeThinU | ComputeThinV);
This line of code compiles fine. Just the particular linker does not return. I have waited a few minutes for the linker, but it just won't return.
On the other hand, when I used the latest gcc from the Windows Linux subsystem. Everything worked fine. So is this a known issue? Can it be easily fixed (but still using the build system provided by Rtools in Windows)?
PS: I have encounted this issue several times before, but the cpp files were much more complex, so I wasn't sure back then what caused the hanging linker.
Update:
A code sample is provided below. I configured my Windows copy of eigen3 using the "Unix Makefiles" option, since I do not have Visual Studio and don't want to download it.
Here is the cpp file:
#include <iostream>
#include <Eigen/Dense>
using namespace std;
using namespace Eigen;
int main()
{
double data[9];
for (int i = 0; i < 9; i++)
{
data[i] = i;
}
Map<MatrixXd> m(data, 3, 3);
BDCSVD<MatrixXd> svd(m, ComputeThinU | ComputeThinV);
auto v = svd.singularValues();
cout << "condition #: " << v.maxCoeff() / v.minCoeff() << endl;
}
I was trying to calculate the condition number of a singular matrix here.
Compiled using command line:
g++ -I"my eigen lib" t.cpp -o t.exe
where g++ comes from Rtools, "my eigen lib" is the eigen include directory, t.cpp the cpp file, and t.exe the build target.
Surprisingly or not, when I changed the algo from BDCSVD to JacobSVD, the compilation would succeed, even though BDCSVD defaults to JacobSVD for small matrices.
cc1plus and as both returned normally. ld keeps running forever, it seems.
Edits:
It's been awhile, but I am still looking for an answer.

Xcode 11.1: iostream' file not found

I just updated my MacBook Pro to macOS Catalina 10.15, and tried to compile and run a C++ command line program, but I had a problem which didn’t exist on previous versions;
This is simply the code:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello, World!\n";
return 0;
}
The code compiles and outputs the expected, but still the Xcode says:
fatal error: 'iostream' file not found
I tried changing the Build Settings/C++ Standard Library to libstdc++, but a warning says:
warning: include path for stdlibc++ headers not found; pass '-stdlib=libc++' on the command line to use the libc++ standard library instead
And the same iostream error still exists.
I'm compiling from the command line, and none of the answers listed here (or elsewhere) worked for me.
What does seem to work (so far) is to add the following to .profile or whatever script your terminal uses to start up: (zsh, csh, bash, etc.)
export C_INCLUDE_PATH=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk/usr/include
export CPLUS_INCLUDE_PATH=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk/usr/include
You will probably have to change MacOSX10.15.sdk whenever you upgrade your operating system.
C_INCLUDE_PATH and CPLUS_INCLUDE_PATH are options for the clang toolchain rather than MacOS environment, so hopefully this solution will work long-term, unlike xcode-select --install (which won't fix the include directories on an upgrade) or ln -s ... /usr/include (which is now forbidden by System Integrity Protection).
I had the same problem and used the following youtube video to fix it.
https://www.youtube.com/watch?v=hrPm7tWC-BI&feature=youtu.be
or you can follow this path. Make sure to include the quotation marks
Project - Build Settings - Search Paths - Headers Search Paths, and add the following path:
"/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/"
So, I restarted my laptop and everything seems to be fine right now, thanks for those who tried to help.
libstdc++ is not OK for Xcode Build & Compile time,
libstdc++ is OK for iPhone Run Time
From answer recommended by #Alan Birtles
libstdc++ Support was removed from the iOS 12.0 Simulator runtime, but
it remains in the iOS 12.0 (device) runtime for binary compatibility
with shipping apps.
I encountered this when declaration in .hpp file.
#include <iostream>
#include <string>
OK with
#ifdef __cplusplus
#include <iostream>
#include <string>
// usage code
#endif
I tried a fresh Catalina install with Xcode. I copied and pasted your code into "test.cpp" and then ran:
clang++ test.cpp
in the same directory as the "test.cpp" file from Terminal. The result was an "a.out" file which when run:
./a.out
output the required "Hello, World!" result. Hopefully that is of some use (as a point of reference).

Formatting predefined types in code eclipse oxygen [c++]

If the type is defined with #define eclipse is removing the space between type and variable name. It's okay with typedef.
#include<iostream>
using namespace std;
typedef vector<int> vi;
#define vs vector<string>
int main() {
vi v1; // no problem
vs v2; // after pressing ctrl+shift+f, space between vs and v2 is sucked up..
//producing some syntax errors
return 0;
}
Version info:
Eclipse IDE for C/C++ Developers
Version: Oxygen.3 Release (4.7.3)
Build id: 20180308-1800
OS: Windows 10, v.10.0, x86_64 / win32
Any way to fix this, thanks.
This is a bug in Eclipse CDT's code formatter. I filed this bug report for it (but be aware that CDT's formatter hasn't seen much maintenance in recent years, so it's not likely to be fixed soon).
As already mentioned, a simple workaround is to replace the #define with a typedef.
You could also use an Eclipse plugin that provides a better formatter than the built-in one, such as this plugin which uses clang-format.

Can't resolve namespace member 'thread'

I wanted to practice with standard C++ threads instead of UNIX ones, but soon encountered a problem, whenever I write std::thread CLion underlines it with red and says Can't resolve namespace member 'thread'. I checked my CMake file it's set for C++11. I reinstalled the latest version of MinGW (6.3.0) and ticked a box with G++ compiler. I have been told by my friend that he uses Cygwin and everything works. But is it still possible to make it work with MinGW?
#include <iostream>
#include <thread>
#define BUFFER_SIZE 3
#define PROD_NUM 3
#define CONS_NUM 2
void produce(){
//production
}
void consume(){
//consumption
}
int main() {
std::cout << "Hello, World!" << std::endl;
int i,j;
std::thread producer(produce);
std::thread consumer (consume);
return 0;
}
The code itself has literally nothing
EDIT
in thread library there is
#pragma GCC system_header
#if __cplusplus < 201103L
# include <bits/c++0x_warning.h>
#else
#include <chrono>
#include <functional>
#include <memory>
#include <cerrno>
#include <bits/functexcept.h>
#include <bits/functional_hash.h>
#include <bits/gthr.h>
#if defined(_GLIBCXX_HAS_GTHREADS) && defined(_GLIBCXX_USE_C99_STDINT_TR1)
namespace std _GLIBCXX_VISIBILITY(default)
{
_GLIBCXX_BEGIN_NAMESPACE_VERSION
/**
* #defgroup threads Threads
* #ingroup concurrency
*
* Classes for thread support.
* #{
*/
/// thread
class thread
{
public:
// Abstract base class for types that wrap arbitrary functors to be
// invoked in the new thread of execution.
struct _State
{
virtual ~_State();
virtual void _M_run() = 0;
};
can you make sure if the library is available in the CLion toolchain? For example Cygwin does have the include.
CLion shows things red when it can't link codes with the library.
It is possibly a host environment variable error. Make sure your CMakeLists.txt is working and your environment variables, standard library linkage is correct as well as your compiler setup.
Compiler version and and standard libraries compatible. (e.g. you are using a cross-compiler (RasPi, Android) but environment vars shows host library etc. will make it fail)
Check this relevant post, it may help.
C++11 std::threads vs posix threads
Ok, so I finally solved the problem. I installed Cygwin and in CLion Settings I manually linked C/C++ compilers (for some reason CLion was unable to auto-detect them). Cleared all and re-indexed the project. Now it shows no errors and code compiles.
Regarding MinGW, I read on cplusplus.com some posts regarding the issue but they were about previous versions of MinGW and it was said that they finally fixed it, however I tell: No, they didn't. Here there is a nice repository and its README file suggests that thread of win32 rely on gthreads, however i found gthread file in my libraries everything seemed ok... so still need to investigate the issue. Write your ideas and experience here if you know more.
As for now solution is Cygwin, use it instead of MinGW.
P.S. Thanks #KillzoneKid for links

How to use GMP library in dev c with gcc4.7.2

I have absolutely no idea about using gmp. Need some functions for a project and need a quick installation guide. I am Absolutely beginner to this field so please help accordingly.
I have:
Dev C++ 5.4.2 in windows 8.1 configuration with GCC4.7.2 as default
compiler.
gmp-static-mingw-4.1.tar
Please specify the correct procedure to configure gmp library.
At first put gmp.h into ..\Dev-Cpp\MinGW32\include and both libgmp.a and libgmp.la into ..\Dev-Cpp\MinGW32\lib directory, then create some project in DevCpp, for example:
#include <stdio.h>
#include <gmp.h>
int main(void)
{
mpz_t x;
mpz_init(x);
mpz_set_str(x, "12345", 10);
mpz_mul_ui(x, x, 2);
gmp_printf("%Zd\n", x);
mpz_clear(x);
return 0;
}
After that go to Project Options -> Parameters and click Add Library of Object:
From the list select libgmp.a file (your static library) and click Open:
Compile and run you project, you will see some note about Makefile update, simply confirm.
Note that GMP 4.1 is now rather old, consider latest version and/or manual compilation for best possible performance on your configuration.