This question is related to: C++ and R: Create a .so or .dll plus i have read the questions and replies of these posts:
Compiling RInside programs on Windows
Problem with compiling RInside examples under Windows
I try to run the code provided as an example in the answer provided
#include <RInside.h> // for the embedded R via RInside
#include <iomanip>
int main(int argc, char *argv[]) {
RInside R(argc, argv); // create an embedded R instance
std::string txt = // load library, run regression, create summary
"suppressMessages(require(stats));"
"swisssum <- summary(lm(Fertility ~ . , data = swiss));"
"print(swisssum)";
R.parseEvalQ(txt); // eval command, no return
// evaluate R expressions, and assign directly into Rcpp types
Rcpp::NumericMatrix M( (SEXP) R.parseEval("swcoef <- coef(swisssum)"));
Rcpp::StringVector cnames( (SEXP) R.parseEval("colnames(swcoef)"));
Rcpp::StringVector rnames( (SEXP) R.parseEval("rownames(swcoef)"));
std::cout << "\n\nAnd now from C++\n\n\t\t\t";
for (int i=0; i<cnames.size(); i++) {
std::cout << std::setw(11) << cnames[i] << "\t";
}
std::cout << std::endl;
for (int i=0; i<rnames.size(); i++) {
std::cout << std::setw(16) << rnames[i] << "\t";
for (int j=0; j<cnames.size(); j++) {
std::cout << std::setw(11) << M(i,j) << "\t";
}
std::cout << std::endl;
}
std::cout << std::endl;
exit(0);
}
The error in the CMD is the following
C:\Users\DON\Desktop>R CMD SHLIB final.cpp
g++ -m64 -I"C:/R/R-3.2.4/include" -DNDEBUG -I"d:/RCompile/r-compiling/local/
local323/include" -O2 -Wall -mtune=core2 -c final.cpp -o final.o
final.cpp:1:74: fatal error: RInside.h: No such file or directory
compilation terminated.
make: *** [final.o] Error 1
Warning message:
comando ejecutado 'make -f "C:/R/R-3.2.4/etc/x64/Makeconf" -f "C:/R/R-3.2.4/shar
e/make/winshlib.mk" SHLIB_LDFLAGS='$(SHLIB_CXXLDFLAGS)' SHLIB_LD='$(SHLIB_CXXLD)
' SHLIB="final.dll" WIN=64 TCLBIN=64 OBJECTS="final.o"' tiene estatus 2
Clearly it cant find the RInside.h header. I have the R installed in a folder without spaces. The PATH in global variables have: C:\R\R-3.2.4\bin; C:\Rtools\bin;C:\Rtools\gcc-4.6.3\bin
I understand that in the CMD i cant introduce comands like
$ export PKG_LIBS=‘Rscript -e "Rcpp:::LdFlags()"‘ # if Rcpp older than 0.11.0
$ export PKG_CXXFLAGS=‘Rscript -e "Rcpp:::CxxFlags()"‘
Which first defines and exports two relevant environment variables which R CMD SHLIB then relies on (as put in the FAQ file)
Any advice on this? I need to do a Makefile for each cpp file that i want to compile?
The error is in your approach. You did
R CMD SHLIB final.cpp
which is nowhere given as the correct approach for working with RInside.
Because we need to tell R about headers and libraries for several components, you are supposed to
cd inst/examples/standard
make # build all
or
make rinside_sample3 # build just this
or, if you're on that OS,
make -f Makefile.win # all
or
make -f Makefile.win rinside_sample3
as the Makefile tells R where do find this. That also answers your second question: One Makefile per directory will do. And look at the Makefile: it sets several include directives; your approach only dealt with Rcpp so of course you get an error about RInside.h not found.
I think you keep asking the same question over and over.
Related
I tried to use eigen in mac. After I installing it I run a demo from its' main page in Xcode.The code is as follows:
#include <iostream>
#include <Eigen/Dense>
using Eigen::MatrixXd;
int main()
{
MatrixXd m(2,2);
m(0,0) = 3;
m(1,0) = 2.5;
m(0,1) = -1;
m(1,1) = m(1,0) + m(0,1);
std::cout << m << std::endl;
}
But it shows "Eigen/Dense' file not found". I try the ways as followed:
Change include line to:#include <Eigen/Core>
try to place the "Eigen" into the "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/" which includes header file like .
3.use the command line"g++ -I /usr/local/include/eigen3 aaa.cpp -o aaa -O1 ". The third one is from
fatal error: 'eigen3/Eigen/Dense' file not found
But I don't know why use this command line. Can someone tell me why? What's more, It also doesn't work for me.
Some people said "/usr/local/include" is the default folders, but there is no "/usr/local/include" in my Mac. So I make a file folder named "include" in 'usr/local'and put the Eigen into it. But it doesn't work. I can't run this code in Xcode.
Can someone help me to solve this problem? Thanks!
I am trying to run this simple example in GDCM. I have installed the library c++ version and the installation works perfectly fine but I am not able to figure out how to compile and run a example.
#include "gdcmReader.h"
#include "gdcmWriter.h"
#include "gdcmAttribute.h"
#include <iostream>
int main(int argc, char *argv[])
{
if( argc < 3 )
{
std::cerr << argv[0] << " input.dcm output.dcm" << std::endl;
return 1;
}
const char *filename = argv[1];
const char *outfilename = argv[2];
// Instanciate the reader:
gdcm::Reader reader;
reader.SetFileName( filename );
if( !reader.Read() )
{
std::cerr << "Could not read: " << filename << std::endl;
return 1;
}
// If we reach here, we know for sure only 1 thing:
// It is a valid DICOM file (potentially an old ACR-NEMA 1.0/2.0 file)
// (Maybe, it's NOT a Dicom image -could be a DICOMDIR, a RTSTRUCT, etc-)
// The output of gdcm::Reader is a gdcm::File
gdcm::File &file = reader.GetFile();
// the dataset is the the set of element we are interested in:
gdcm::DataSet &ds = file.GetDataSet();
// Contruct a static(*) type for Image Comments :
gdcm::Attribute<0x0020,0x4000> imagecomments;
imagecomments.SetValue( "Hello, World !" );
// Now replace the Image Comments from the dataset with our:
ds.Replace( imagecomments.GetAsDataElement() );
// Write the modified DataSet back to disk
gdcm::Writer writer;
writer.CheckFileMetaInformationOff(); // Do not attempt to reconstruct the file meta to preserve the file
// as close to the original as possible.
writer.SetFileName( outfilename );
writer.SetFile( file );
if( !writer.Write() )
{
std::cerr << "Could not write: " << outfilename << std::endl;
return 1;
}
return 0;
}
/*
* (*) static type, means that extra DICOM information VR & VM are computed at compilation time.
* The compiler is deducing those values from the template arguments of the class.
*/
It has a few header files that it is looking for namely gdcmreader, gdcmwriter and I want to figure out the compiler flags to use to be able to run this file.
I am doing g++ a.cpp -lgdcmCommon -lgdcmDICT but that gives me the error
a.cpp:18:24: fatal error: gdcmReader.h: No such file or directory
compilation terminated.
Can you please help me out? I have searched everywhere but I can't seem to figure out how to run this file.
When using files that are in different locations of your "normal" files you must instruct the compiler and the linker how to find them.
Your code has a #include <someFile.h> command.
The <> usage means "in other path". The compiler already knows common "other paths" as for "stdio" for common libraries.
In case of "not normal", you can tell g++ where to find the headers by adding -Imydir to the command line (replace 'mydir' with the proper path)
For the libraries, static (.a) or dynamic (.so) the same history stands.
The -Lmydir tells g++ where to look for libraries.
Your command line may look like
g++ a.cpp -I/usr/include -L/usr/local/lib -lgdcmCommon -lgdcmDICT
You did not tell how did you install gdcm library, I assume that using apt system. There are two types of libraries, "normal" and "developer" ones. To be able to compile your own software, you need the latter. So, for example in Ubuntu 16.04, type apt-get install libgdcm2-dev. Then all necessary headers will be installed in /usr/include/gdcm-2.6.
I am trying to integrate AMPL with C/C++ using AMPL-API on Windows-7 in Eclipse Mars 2.0. I created a Makefile project in Eclipse which uses MinGW CC to compile the firstexample code given in their example directory.
firstexample.cpp:
#include <iostream>
#include "ampl/ampl.h"
using namespace std;
int main() {
ampl::AMPL ampl;
// Read the model and data files.
std::string modelDirectory = "models";
ampl.read(modelDirectory + "/diet/diet.mod");
ampl.readData(modelDirectory + "/diet/diet.dat");
// Solve
ampl.solve();
// Get objective entity by AMPL name
ampl::Objective totalcost = ampl.getObjective("total_cost");
// Print it
std::cout << "Objective is: " << totalcost.value() << std::endl;
// Get objective entity by AMPL name
ampl::Objective totalcost = ampl.getObjective("total_cost");
// Print it
std::cout << "Objective is: " << totalcost.value() << std::endl;
// Reassign data - specific instances
ampl::Parameter cost = ampl.getParameter("cost");
cost.setValues(new Tuple[2]{ ampl::Arg("BEEF"), ampl::Arg("HAM")}, new Arg[2]{ 5.01, 4.55 },
2);
std::cout << "Increased costs of beef and ham." << std::endl;
// Resolve and display objective
ampl.solve();
std::cout << "New objective value: " << totalcost.value() << std::endl;
// Reassign data - all instances
ampl::Arg elements[8]{ 3, 5, 5, 6, 1, 2, 5.01, 4.55 };
cost.setValues(elements);
std::cout << "Updated all costs." << std::endl;
// Resolve and display objective
ampl.solve();
std::cout << "New objective value: " << totalcost.value() << std::endl;
// Get the values of the variable Buy in a dataframe object
Variable buy = ampl.getVariable("Buy");
ampl::DataFrame df;
df = buy.getValues();
// Print them
df.print();
ampl::DataFrame df2;
// Get the values of an expression into a DataFrame object
df2 = ampl.getData("{j in FOOD} 100*Buy[j]/Buy[j].ub");
// Print them
df2.print();
}
Following is my Makefile:
CC = g++
CFLAGS = -O2 -g -Wall -fmessage-length=0
INCLUDES = -I "C:\\Local\\AMPL\\AMPL32\\amplapi32\\include"
OBJS = AMPL.o
LFLAGS = -L "C:\\Local\\AMPL\\AMPL32\\amplapi32\\lib"
LIBS = -lampl1.2.2
TARGET = AMPL.exe
$(TARGET): $(OBJS)
$(CC) $(CFLAGS) $(INCLUDES) -o $(TARGET) $(OBJS) $(LFLAGS) $(LIBS)
AMPL.o: AMPL.cpp
$(CC) $(CFLAGS) $(INCLUDES) -c AMPL.cpp
all: $(TARGET)
clean:
rm -f $(OBJS) $(TARGET)
I have added path of required dll files (libampl1.2.2.dll) to the environment variables. I am able to compile and execute code on Visual Studio 2015 with two minor changes:
Without using Makefile (It is a Win32 Console Application)
Adding #include "stdafx.h" in firstexample.cc
However when I execute the same code in Eclipse, it gives me following error:
src\AMPLTesting.o: In function `ZN4ampl8internal11deleteTupleERNS0_5TupleE':
C:/Local/AMPL/AMPL32/amplapi32/include/ampl/ep/tuple_ep.h:24: undefined reference to `_imp___ZN4ampl8internal24AMPL_Variant_DeleteArrayEPKNS0_7VariantE'
src\AMPLTesting.o: In function `ZN4ampl8internal12TupleBuilderC1Ej':
C:/Local/AMPL/AMPL32/amplapi32/include/ampl/ep/tuple_ep.h:35: undefined reference to `_imp___ZN4ampl8internal24AMPL_Variant_CreateArrayEjPNS0_16ErrorInformationE'
collect2.exe: error: ld returned 1 exit status
I am not sure what is the problem? Am I missing some command line option in the Makefile or not adding any specific library? Please help me with this.
The beta version of the C++ API only supports MSVC on Windows at the moment. Support for other compilers will be added in future releases.
I have installed boost and cpp-netlib and successfully ran all tests. I can compile the following example from the command line with the following options:
clang++ -o test main.cpp \
-I/path.../cpp-netlib-0.11.1-final \
-I/path.../boost_1_57_0 \
-L/path.../boost_1_57_0/stage/lib \
-L/usr/local/lib \
-lboost_system \
-lboost_thread \
-lcppnetlib-uri \
-lcppnetlib-client-connections \
-lssl \
-lcrypto \
-pthread
Example from cpp-netlib.org:
#include <boost/network/protocol/http/client.hpp>
#include <iostream>
int main(int argc, char *argv[]) {
using namespace boost::network;
if (argc != 2) {
std::cout << "Usage: " << argv[0] << " [url]" << std::endl;
return 1;
}
http::client client;
http::client::request request(argv[1]);
request << header("Connection", "close");
http::client::response response = client.get(request);
std::cout << body(response) << std::endl;
return 0;
}
Running this program:
./test "url"
Successfully displays the html code of website.
I am running into a problem when I try to import this example into Xcode. I have included the correct search paths for header files and binaries. The code will compile but Xcode crashes every time it reaches this line.
http::client::request request(argv[1]);
I can comment out this line and everything after it and the program compiles and runs. Otherwise it crashes, even when trying to use a breakpoint. Any suggestions would be appreciated.
This is because of the URI parser generating really big symbols in debug builds. The use of Boost.Spirit in the URI parsing uses very heavy template metaprogramming and that shows up as very long symbols which may not be handled appropriately by the system. I'm not sure whether there are work-arounds here as I don't use the Xcode IDE for regular development of cpp-netlib.
I am running a beaglebone and want to write a program to sample the ADC. I am trying to use the blacklib (http://blacklib.yigityuce.com/index.html) from here. I cloned the git:
https://github.com/yigityuce/BlackLib
and tried to compile the example with
g++ exampleAndTiming.cpp -std=c++11
This however gives me a ton of errors like these:
In file included from exampleAndTiming.cpp:33:0:
exampleAndTiming/exampleAndTiming_GPIO.h: In function 'void exampleAndTiming_GPIO()':
exampleAndTiming/exampleAndTiming_GPIO.h:97:12: error: 'sleep' was not declared in this scope
sleep(1);
^
In file included from exampleAndTiming.cpp:34:0:
exampleAndTiming/exampleAndTiming_ADC.h: In function 'void exampleAndTiming_ADC()':
exampleAndTiming/exampleAndTiming_ADC.h:67:16: error: 'usleep' was not declared in this scope
usleep(1000);
^
so I include unistd.h (in exampleAndTiming.cpp), but then I get errors like these:
/tmp/ccbgiXE9.o: In function `exampleAndTiming_GPIO()':
exampleAndTiming.cpp:(.text+0x50): undefined reference to `Timing::startMeasure(std::string)'
exampleAndTiming.cpp:(.text+0x80): undefined reference to `BlackLib::BlackGPIO::BlackGPIO(BlackLib::gpioName, BlackLib::direction, BlackLib::workingMode)'
exampleAndTiming.cpp:(.text+0xbc): undefined reference to `Timing::endMeasure(std::string)'
exampleAndTiming.cpp:(.text+0xec): undefined reference to `BlackLib::BlackGPIO::BlackGPIO(BlackLib::gpioName, BlackLib::direction, BlackLib::workingMode)'
exampleAndTiming.cpp:(.text+0x104): undefined reference to `BlackLib::BlackGPIO::BlackGPIO(BlackLib::gpioName, BlackLib::direction, BlackLib::workingMode)'
exampleAndTiming.cpp:(.text+0x11c): undefined reference to `BlackLib::BlackGPIO::BlackGPIO(BlackLib::gpioName, BlackLib::direction, BlackLib::workingMode)'
exampleAndTiming.cpp:(.text+0x158): undefined reference to `Timing::startMeasure(std::string)'
I've been looking at some library examples and compiling it, but I cannot make sense of it all. I've compiled plenty of c++ and c programs before, but I can't get this one to work. So any help will be appreciated.
COMPLETE GUIDE how to compile BLACKLIB directly on BEAGLEBONE BLACK (rev C) running ANGSTROM:
Programs:
Putty - to communicate with BBB from Windows (using SSH with USB cable)
WinSCP - to manage (upload, create, delete) files directly on BBB
Code::Blocks - to write C++ programs
optionally
Termite 2.9 - to send and receive UART transmission from UART<->USB converter (actually Putty could be used to do that as well)
1) get the BlackLib from official site
2) unzip the library and copy following files into separate folder :
BlackADC.cpp, BlackADC.h, BlackCore.cpp, BlackCore.h, BlackDef.h, BlackErr.h, BlackGPIO.cpp, BlackGPIO.h, BlackI2C.cpp, BlackI2C.h, BlackLib.h, BlackPWM.cpp, BlackPWM.h, BlackSPI.cpp, BlackSPI.h, BlackUART.cpp, BlackUART.h
3) open following files in Code::Blocks BlackUART.cpp, BlackSPI.cpp, BlackI2C.cpp and add
#include <unistd.h>
right after #include "BlackUART.h", the "unistd.h" includes all the functions like sleep(), open(), close(), ... that otherwise seems missing
4) create your own program main.cpp, you may use the following code for testing UART1 and UART2:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sstream>
#include <string>
#include <iostream>
#include "BlackLib.h"
int main(){
std::string writeToUart1;
std::string writeToUart2;
std::string readFromUart1;
std::string readFromUart2;
int counter;
std::ostringstream os1;
std::ostringstream os2;
BlackLib::BlackUART Uart1(BlackLib::UART1,
BlackLib::Baud9600,
BlackLib::ParityEven,
BlackLib::StopOne,
BlackLib::Char8 );
// Pins on BeagleBone Black REV C
// UART1_RX -> GPIO_15 (P9.24)
// UART1_RX -> GPIO_14 (P9.26)
BlackLib::BlackUART Uart2(BlackLib::UART2,
BlackLib::Baud9600,
BlackLib::ParityEven,
BlackLib::StopOne,
BlackLib::Char8 );
// Pins on BeagleBone Black REV C
// UART2_RX -> GPIO_2 (P9.22)
// UART2_RX -> GPIO_3 (P9.21)
std::cout << "Program UART start" << std::endl << std::flush;
Uart1.open( BlackLib::ReadWrite | BlackLib::NonBlock );
Uart2.open( BlackLib::ReadWrite | BlackLib::NonBlock );
counter = 0;
while (true){
os1.str("");
os1.clear();
os1 << "Uart1 to TX: " << counter << "\n";
writeToUart1 = os1.str();
Uart1 << writeToUart1;
readFromUart1 = "";
Uart1 >> readFromUart1;
if (readFromUart1.compare("") != 0){
std::cout << "Uart1 from RX: " << readFromUart1 << "\n" << std::flush;
}
Uart1.flush( BlackLib::bothDirection );
counter++;
sleep(2);
os2.str("");
os2.clear();
os2 << "Uart2 to TX: " << counter << "\n";
writeToUart2 = os2.str();
Uart2 << writeToUart2;
readFromUart2 = "";
Uart2 >> readFromUart2;
if (readFromUart2.compare("") != 0){
std::cout << "Uart2 from RX: " << readFromUart2 << "\n" << std::flush;
}
Uart2.flush( BlackLib::bothDirection );
counter++;
sleep(2);
}
return 1;
}
5) save the main.cpp to the same folder as the BlackLib files
6) using WinSCP, create directory on the BBB (e.g. /home/uart) and copy all the BlackLib files and main.cpp into this folder
7) open Putty and navigate to the folder by :
cd /home/uart
8) compile the files by using :
gcc *.cpp -o main -std=c++11
9) run the program :
./main
10) connect the wires to UART<->USB converter and BBB. The ouput from BBB should look like :
Uart2 to TX: 1 OR Uart1 to TX: 0
Uart2 to TX: 3 OR Uart1 to TX: 2
depending on connection of wires
It seems I managed to fix it myself, some nooblike behaviour not including all the cpp files, but even more, I also needed to add #include to BlackCore.h to avoid tons of undefined function errors.
final command:
g++ exampleAndTiming.cpp exampleAndTiming/Timing.cpp BlackADC.cpp BlackCore.cpp BlackGPIO.cpp BlackI2C.cpp BlackPWM.cpp BlackSPI.cpp BlackUART.cpp -std=c++11
I'd probably need to make a makefile to compile the library seperately, time to do some more digging and learning.
I am the creator of BlackLib, Yiğit YÜCE. You found your answer by yourself. The makefile which you mentioned on your comment will be published shortly.