I'm trying to run my cpp program with matrix from dlib in R. For hours I have been trying to run code, but I couldn't resolve one error. Can you help me?
There is fragment of my code:
#include <Rcpp.h>
#include <cmath>
#include <vector>
// [[Rcpp::depends(RcppEigen)]]
#include <RcppEigen.h>
// [[Rcpp::plugins(cpp11)]]
// [[Rcpp::depends(dlib)]]
#include <dlib/optimization/optimization.h>
using namespace Eigen;
using namespace std;
using namespace dlib;
typedef matrix<double,0,1> column_vector;
Unfortunately I can't compile code because of Source Cpp error:
~/R/win-library/3.4/Rcpp/include/Rcpp/internal/Exporter.h
Line 31 invalid conversion from 'SEXP' to long int [-fpermissive]
and logs from console:
C:/RBuildTools/3.4/mingw_64/bin/g++ -std=gnu++11 -I"C:/PROGRA~1/R/R-34~1.1/include" -DNDEBUG -I"C:/Users/user_name/Documents/R/win-library/3.4/Rcpp/include" -I"C:/Users/user_name/Documents/R/win-library/3.4/RcppEigen/include" -I"C:/Users/user_name/Documents/R/win-library/3.4/dlib/include" -I"C:/Users/user_name/Documents/project_name/cpp_to_R" -I"d:/Compiler/gcc-4.9.3/local330/include" -std=c++0x -O2 -Wall -mtune=core2 -c project_name.cpp -o project_name.o
In file included from C:/Users/user_name/Documents/R/win-library/3.4/Rcpp/include/Rcpp/as.h:25:0,
from C:/Users/user_name/Documents/R/win-library/3.4/Rcpp/include/RcppCommon.h:160,
from C:/Users/user_name/Documents/R/win-library/3.4/Rcpp/include/Rcpp.h:27,
from project_name.cpp:1:
C:/Users/user_name/Documents/R/win-library/3.4/Rcpp/include/Rcpp/internal/Exporter.h: In instantiation of 'Rcpp::traits::Exporter<T>::Exporter(SEXP) [with T = dlib::matrix<double, 0l, 1l>; SEXP = SEXPREC*]':
C:/Users/user_name/Documents/R/win-library/3.4/Rcpp/include/Rcpp/as.h:87:51: required from 'T Rcpp::internal::as(SEXP, Rcpp::traits::r_type_generic_tag) [with T = dlib::matrix<double, 0l, 1l>; SEXP = SEXPREC*]'
C:/Users/user_name/Documents/R/win-library/3.4/Rcpp/include/Rcpp/as.h:152:82: required from 'T Rcpp::as(SEXP) [with T = dlib::matrix<double, 0l, 1l>; SEXP = SEXPREC*]'
C:/Users/user_name/Documents/R/win-library/3.4/Rcpp/include/Rcpp/InputParameter.h:72:62: required from 'Rcpp::ConstReferenceInputParameter<T>::ConstReferenceInputParameter(SEXP) [with T = dlib::matrix<double, 0l, 1l>; SEXP = SEXPREC*]'
project_name.cpp:279:80: required from here
C:/Users/user_name/Documents/R/win-library/3.4/Rcpp/include/Rcpp/internal/Exporter.h:31:31: error: invalid conversion from 'SEXP' to 'long int' [-fpermissive]
Exporter( SEXP x ) : t(x){}
^
In file included from C:/Users/user_name/Documents/R/win-library/3.4/dlib/include/dlib/optimization/../matrix.h:6:0,
from C:/Users/user_name/Documents/R/win-library/3.4/dlib/include/dlib/optimization/optimization_search_strategies.h:8,
from C:/Users/user_name/Documents/R/win-library/3.4/dlib/include/dlib/optimization/optimization.h:9,
from project_name.cpp:8:
C:/Users/user_name/Documents/R/win-library/3.4/dlib/include/dlib/optimization/../matrix/matrix.h:1019:18: note: initializing argument 1 of 'dlib::matrix<T, num_rows, num_cols, mem_manager, layout>::matrix(long int) [with T = double; long int num_rows = 0l; long int num_cols = 1l; mem_manager = dlib::memory_manager_stateless_kernel_1<char>; layout = dlib::row_major_layout]'
explicit matrix (
^
make: *** [project_name.o] Error 1
Warning message:
running command 'make -f "C:/PROGRA~1/R/R-34~1.1/etc/x64/Makeconf" -f "C:/PROGRA~1/R/R-34~1.1/share/make/winshlib.mk" CXX='$(CXX11) $(CXX11STD)' CXXFLAGS='$(CXX11FLAGS)' CXXPICFLAGS='$(CXX11PICFLAGS)' SHLIB_LDFLAGS='$(SHLIB_CXX11LDFLAGS)' SHLIB_LD='$(SHLIB_CXX11LD)' SHLIB="sourceCpp_78.dll" WIN=64 TCLBIN=64 OBJECTS="project_name.o"' had status 2
Error in Rcpp::sourceCpp("cpp_to_R/project_name.cpp") :
Error 1 occurred building shared library.
What's wrong here? I don't get it :(
Edit:
This error appears only when I add // [[Rcpp::export]] before function which uses my column_vector
The edit helps. My earlier comment was off. So basically, you typedef this
typedef matrix<double,0,1> column_vector;
and then use a column_vector and expect Rcpp to magically convert its content.
In short, it doesn't work that way. If you have new types that Rcpp does not yet know about then the burden is on you to provide new converters.
We wrote a vignette 'Extending Rcpp' about this which may help.
Related
Hey I'm trying to save text entered in textbox as string
#pragma once
#include <iostream>
#include <string>
namespace Project1 {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
int IloscBD1=0, IloscBD2=0, IloscPD1, IloscPD2, Suma, I, J, Punkty, P1, P2, P3, P4, P5, P6, Druzyna;
int TabPunkt[10][6];
std::string TabOdpowiedzi[10][6];
...
...
private: System::Void buttonZ_Click(System::Object^ sender, System::EventArgs^ e) {
TabOdpowiedzi[1][1] = (Convert::ToString(textPO->Text));
this->textPN->Text = (Convert::ToString(TabOdpowiedzi[1][1]));
}
But I am getting those errors, what's wrong? How can I keep input from text box as a string for future or is there a better way to keeping text input for future usage ?
Error 1 error C2679: binary '=' : no operator found which takes a right-hand operand of type 'System::String ^' (or there is no acceptable conversion)
Error 2 error C2665: 'System::Convert::ToString' : none of the 37 overloads could convert all the argument types
3 IntelliSense: no operator "=" matches these operands
operand types are: std::string = System::String ^
4 IntelliSense: no instance of overloaded function "System::Convert::ToString" matches the argument list
argument types are: (std::string)
You are mixing up your types. Declare
std::string TabOdpowiedzi[10][6];
As
array<System::String^,2>^ TabOdpowiedzi = gcnew array<System::String^,2>(10, 6);
And your problems here vanish. Maybe you will have problems in other parts of your code then, but you have not posted those...
I'm using RcppArmadillo in my R package and I would like to use the Rcpp::Nullable in the parameter list.
NumericVector d_snb(NumericVector& x,
Nullable<arma::mat> size_param = R_NilValue,
const int& infinite = 100000, const bool& log_v = false)
This gives an error like:
Error: package or namespace load failed for ‘packagex’ in dyn.load(file, DLLpath = DLLpath, ...): unable to load shared object '/usr/local/lib/R/3.4/site-library/packagex/libs/packagex.so': dlopen(/usr/local/lib/R/3.4/site-library/packagex/libs/packagex.so, 6):
Symbol not found: __Z5d_snbRN4Rcpp6VectorILi14ENS_15PreserveStorageEEENS_8NullableIS2_EES5_S5_RKiRKb
Referenced from: /usr/local/lib/R/3.4/site-library/packagex/libs/packagex.so
Expected in: flat namespace in /usr/local/lib/R/3.4/site-library/packagex/libs/packagex.so
Error: loading failed
Execution halted
The only possible solution right now seems to get the parameter as NumericVector and then get contents and then cast it into Armadillo types.
NumericVector d_snb(NumericVector& x,
Nullable<NumericVector> size_param = R_NilValue ...)
{
...
if(size_param.isNotNull()) {
arma::mat test(NumericVector(size_param));
param_check++;
}
...
}
This gives a warning:
d_snb.cpp:36:21: warning: parentheses were disambiguated as a function declaration [-Wvexing-parse]
arma::mat test(NumericVector(size_param));
^~~~~~~~~~~~~~~~~~~~~~~~~~~
d_snb.cpp:36:22: note: add a pair of parentheses to declare a variable
arma::mat test(NumericVector(size_param));
^
( )
1 warning generated.
What's the best way to go about this ?
Yes, Rcpp::Nullable<> works only around SEXP-based Rcpp types.
So you have to do the two-step procedure you found. But I think you can do one simpler: arma::mat test = Rcpp::as<arma::mat>(size_param);
Here is a complete example which compiles (but does 'nothing'):
#include <RcppArmadillo.h>
// [[Rcpp::depends(RcppArmadillo)]]
// [[Rcpp::export]]
void d_snb(Rcpp::NumericVector& x,
Rcpp::Nullable<Rcpp::NumericVector> size_param = R_NilValue) {
if (size_param.isNotNull()) {
arma::vec test = Rcpp::as<arma::vec>(size_param);
test.print("vector");
}
Rcpp::Rcout << x << std::endl;
}
And a quick demo:
R> sourceCpp("/tmp/so44102346.cpp")
R> d_snb(c(1,2,3))
1 2 3
R> d_snb(c(1,2,3), c(4,5,6))
vector
4.0000
5.0000
6.0000
1 2 3
R>
When I compile this code in Visual C++, I got the below error. Can help me solve this issue..
DWORD nBufferLength = MAX_PATH;
char szCurrentDirectory[MAX_PATH + 1];
GetCurrentDirectory(nBufferLength, szCurrentDirectory);
szCurrentDirectory[MAX_PATH +1 ] = '\0';
Error message:
Error 5 error C2664: 'GetCurrentDirectoryW' : cannot convert parameter 2 from 'char [261]' to 'LPWSTR' c:\car.cpp
Your program is configured to be compiled as unicode. Thats why GetCurrentDirectory is GetCurrentDirectoryW, which expects a LPWSTR (wchar_t*).
GetCurrentDirectoryW expects a wchar_t instead of char array. You can do this using TCHAR, which - like GetCurrentDirectory - depends on the unicode setting and always represents the appropriate character type.
Don't forget to prepend your '\0' with an L in order to make the char literal unicode, too!
It seems you have define UNICODE, _UNICODE compiler flags. In that case, you need to change the type of szCurrentDirectory from char to TCHAR.
Headers:
#include <iostream>
#include <fstream>
#include <direct.h>
#include <string.h>
#include <windows.h> //not sure
Function to get current directory:
std::string getCurrentDirectoryOnWindows()
{
const unsigned long maxDir = 260;
wchar_t currentDir[maxDir];
GetCurrentDirectory(maxDir, currentDir);
std::wstring ws(currentDir);
std::string current_dir(ws.begin(), ws.end());
return std::string(current_dir);
}
To call function:
std::string path = getCurrentDirectoryOnWindows(); //Output like: C:\Users\NameUser\Documents\Programming\MFC Program 5
To make dir (Folder) in current directory:
std::string FolderName = "NewFolder";
std::string Dir1 = getCurrentDirectoryOnWindows() + "\\" + FolderName;
_mkdir(Dir1.c_str());
This works for me in MFC C++.
I am trying to create a JNI wrapper for a C library that depends on the ICU libraries (libicuuc.so and libicui18n.so).
I tried building ICU4C in my NDK (both standard and CrystaX versions, on a Mac OS X machine) and kept running into linking issues like this:
/Users/kyip/KyVmShared/KyAndroid/myproject/obj/local/armeabi/objs/icuuc/udata.o: In function `openCommonData':
/Users/kyip/KyVmShared/KyAndroid/myproject/jni/icu4c/common/udata.c:836: undefined reference to `icudt42_dat'
/Users/kyip/KyVmShared/KyAndroid/myproject/obj/local/armeabi/objs/icuuc/ustr_wcs.o: In function `_strFromWCS':
/Users/kyip/KyVmShared/KyAndroid/myproject/jni/icu4c/common/ustr_wcs.c:365: undefined reference to `wcstombs'
/Users/kyip/KyVmShared/KyAndroid/myproject/jni/icu4c/common/ustr_wcs.c:415: undefined reference to `wcstombs'
/Users/kyip/KyVmShared/KyAndroid/myproject/jni/icu4c/common/ustr_wcs.c:314: undefined reference to `wcstombs'
/Users/kyip/KyVmShared/KyAndroid/myproject/obj/local/armeabi/objs/icuuc/ustr_wcs.o: In function `_strToWCS':
/Users/kyip/KyVmShared/KyAndroid/myproject/jni/icu4c/common/ustr_wcs.c:164: undefined reference to `mbstowcs'
collect2: ld returned 1 exit status
I also tried the suggestion given at unicode support in android ndk but no luck. I got stuck at:
arm-eabi-g++ -I/ky/crystax/android-ndk-r4-crystax/build/platforms/android-8/arch-arm/usr/include/ -O3 -fno-short-wchar -DU_USING_ICU_NAMESPACE=0 -DU_GNUC_UTF16_STRING=0 -fno-short-enums -nostdlib -fPIC -DU_COMMON_IMPLEMENTATION -D_REENTRANT -I../common -I../../icu/source/common -I../../icu/source/i18n "-DDEFAULT_ICU_PLUGINS=\"/usr/local/lib/icu\" " -DU_COMMON_IMPLEMENTATION -DHAVE_CONFIG_H -I/ky/crystax/android-ndk-r4-crystax/build/platforms/android-8/arch-arm/usr/include/ -O3 -fno-short-wchar -DU_USING_ICU_NAMESPACE=0 -DU_GNUC_UTF16_STRING=0 -fno-short-enums -nostdlib -fPIC -DU_COMMON_IMPLEMENTATION -std=c++0x -fvisibility=hidden -c -o errorcode.ao ../../icu/source/common/errorcode.cpp
In file included from ../../icu/source/common/unicode/ptypes.h:23,
from ../../icu/source/common/unicode/umachine.h:52,
from ../../icu/source/common/unicode/utypes.h:36,
from ../../icu/source/common/errorcode.cpp:17:
/ky/crystax/android-ndk-r4-crystax/build/platforms/android-8/arch-arm/usr/include/sys/types.h:122: error: 'uint64_t' does not name a type
make[1]: *** [errorcode.ao] Error 1
make: *** [all-recursive] Error 2
Any help would be appreciated.
It seems that two files are involved in this issue. icu/source/common/unicode/ptypes.h which calls sys/types.h includes
#if ! U_HAVE_UINT64_T
typedef unsigned long long uint64_t;
/* else we may not have a 64-bit type */
#endif
By including sys/types.h from Android, we involve (near line 122/124)
#ifdef __BSD_VISIBLE
typedef unsigned char u_char;
typedef unsigned short u_short;
typedef unsigned int u_int;
typedef unsigned long u_long;
typedef uint32_t u_int32_t;
typedef uint16_t u_int16_t;
typedef uint8_t u_int8_t;
typedef uint64_t u_int64_t;
#endif
It seems that uint64_t has not been declared when it is assigned to u_int64_t. Indeed, sys/types.h includes stdint.h which has the following:
#if !defined __STRICT_ANSI__ || __STDC_VERSION__ >= 199901L
# define __STDC_INT64__
#endif
typedef __int8_t int8_t;
typedef __uint8_t uint8_t;
typedef __int16_t int16_t;
typedef __uint16_t uint16_t;
typedef __int32_t int32_t;
typedef __uint32_t uint32_t;
#if defined(__STDC_INT64__)
typedef __int64_t int64_t;
typedef __uint64_t uint64_t;
#endif
Likely STRICT_ANSI is not defined. Seems like this is a bug in the Android code in sys/types.h. If STDC_INT64 is not defined, it will not define uint64_t so it can't define u_int64_t. Perhaps the real solution is to have sys/types.h modified so that it has
#ifdef __BSD_VISIBLE
typedef unsigned char u_char;
typedef unsigned short u_short;
typedef unsigned int u_int;
typedef unsigned long u_long;
typedef uint32_t u_int32_t;
typedef uint16_t u_int16_t;
typedef uint8_t u_int8_t;
$if defined(__STDC_INT64__)
typedef uint64_t u_int64_t;
#endif
#endif
If you fix this, the next error will be in cstring.h:109
icu/source/common/cstring.h:109: error: 'int64_t' has not been declared
If you instead #define STDC_INT64 in common/unicode/ptypes.h it will go substantially farther, but will end at
icu/source/common/ustrenum.cpp:118: error: must #include <typeinfo> before using typeid
with more info here: http://groups.google.com/group/android-ndk/browse_thread/thread/2ec9dc289d815ba3?pli=1 but no real solutions
I also had this issue:
undefined reference to `mbstowcs'
You should build and link with higher version of android api.
Note:
I tried to link it with libraries from android-ndk/platforms/android-4... I had thought that 4 is version of Android, but 4 is version of Android API. And Android API 4 corresponds to Android 1.6 witch is very very old there is really no mbstowcs function in libc
Here is how i solved the problem. It is dirty but it works. The lib got compiled:
1. file: /icu4c/common/cwchar.h
comment out the #if U_HAVE_WCHAR_H and the respective #endif so the <wchar.h> is always included.
replace the previous uprv_wcstombs definition with:
#define uprv_wcstombs(mbstr, wcstr, count) U_STANDARD_CPP_NAMESPACE wcs2mbs(mbstr, wcstr, count)
replace the previous uprv_mbstowcs definition with:
#define uprv_mbstowcs(wcstr, mbstr, count) U_STANDARD_CPP_NAMESPACE mbs2wcs(wcstr, mbstr, count)
2. file: /icu4c/common/ustr_wcs.cpp
somewhere at the top, under the already existing includes add the line:
#include "../wcsmbs.h"
3. create new file "icu4c/wcsmbs.h"
size_t mbs2wcs(wchar_t * __ restrict pwcs, const char * __ restrict s, size_t n)
{
mbstate_t mbs;
const char *sp;
memset(&mbs, 0, sizeof(mbs));
sp=s;
return (mbsrtowcs(pwcs,&sp,n,&mbs));
}
size_t wcs2mbs(char * __restrict s, const wchar_t * __restrict pwcs, size_t n)
{
mbstate_t mbs;
const wchar_t *pwcsp;
memset(&mbs,0,sizeof(mbs));
pwcsp = pwcs;
return (wcsrtombs(s,&pwcsp,n,&mbs));
}
Hope it helps.
There has been an effort to provide NDK wrappers for the ICU libraries that are part of the system: https://android-review.googlesource.com/c/153001/.
I was following this tutorial, bit stuck here:
This code doesn't compile, and the error message is
c:35: error: invalid initializer
I'm not sure what's wrong with the line
XGCValues valu=CapButt|JoinBevel;
infact, I copied it from the said tutorial. Here's the full code I have:
#include <stdio.h>
#include <X11/Xlib.h>
#include <unistd.h>
int main()
{
Display *display=XOpenDisplay(NULL);
int scr=DefaultScreen(display);
Window root_window=RootWindow(display,scr);
unsigned int width=DisplayWidth(display,scr)/3;
unsigned int height=DisplayHeight(display,scr)/3;
unsigned int border=2;
Window my_win=XCreateSimpleWindow(display,root_window,0,0,width,height,border,BlackPixel(display,scr),WhitePixel(display,scr));
GC gc;
XGCValues valu=CapButt|JoinBevel;
unsigned long valmask=GCCapStyle|GCJoinStyle;
gc=XCreateGC(display,my_win,valmask,&valu);
XDrawLine(display,my_win,gc,5,5,20,20);
XMapWindow(display,my_win);
XFlush(display);
sleep(10);
return 0;
}
Thank You
The example in the tutorial is wrong - if you look in <X11/Xlib.h> or read the XCreateGC man page you'll see XGCValues is a struct, not a integral type, so you would need to initialize it with something like:
XGCValues values;
values.cap_style = CapButt;
values.join_style = JoinBevel;