Writing an R package containing C++ code in Eclipse/StatET - c++

I'm trying to figure out how to set up Eclipse so that I can write R projects and packages which contain C++ code also.
Of course I could make separate R and C++ projects, compile the C++ code, copy the .dll to R project and use it in R using dyn.load function, but what If I want to build an R package which contains C++ source codes? I could again have separate C++ and R projects, put copy the C++ codes into the src folder of my R package, and I guess using R package builder within StatET perspective would work properly? But I'm wondering can I make just one R package project and embed C++ code inside of that project so that I get the benefits of the C++ perspective, ie. color codings of the code etc?
I know someone will mention Rcpp, but that is not an option here. I'm trying to learn C++ and how to combine it with R in a hard way first.
I'm using both Linux and Windows.
edit: Just to clarify, I want to know how I can set up Eclipse so that it understands both R and C++ codes at the same time, ie. both codes have color codings etc.

I am as much as fan of the editor war's as anybody else but you misunderstand. Take what you wrote:
I'm trying to learn C++ and how to combine it with R in a hard way
first.
So read "Writing R Extensions" carefully, and redo the examples. On the command-line. Then learn how to use a Makefile.
It is about the code and the compiler invocation. Whether you write the code in vi, Emacs, or Eclipse does not matter if you do not understand how to call the compiler, or how to write your code. So your intent is right: learn these basics.
Once you grok all that plumming you are ready to insert appropriate declarations into your Eclipse/StatET configurations.
(And if you want to cheat I could point you to the contributed cmake files in the RInside package.)

Okay it seems that I had poorly installed Eclipse which caused some problems. The answer to my question is that I can work in StatET perspective and it automatically understands C++ code if the filename extension is of form .c or .cpp. I can then compile the code by using R CMD SHLIB or R CMD INSTALL, if the code is part of some package.

Related

Eclipse C++: having trouble with including a file with extension ji

maybe the title is not the most helpful but wasn't sure how to put it with a few words.
So this is my problem.
I have some C++ projects in Eclipse and I want to use for a part of my project some Julia code.
I have downloaded and installed properly the latest version of Julia on my machine and what I want to do is embed Julia inside my C++ project in Eclipse, so that I will be able to write directly some julia code inside the C++ project.
Such an option is possible and there is this guidance on how to do it.
I do properly in eclipse both the path to include julia.h and the julia library, and I can actually build the project, but when I try to run I receive the following error:
"System image file "/home/kostav/workspace/juli/Debug/../lib/x86_64-linux-gnu/julia/sys.ji" not found
"
Now this file does exist and its path is included in Eclipse, so I really don't understand why I do receive this error and what should I do in order to fix it.
Any suggestion would be really helpful to me.
jl_init_with_image
jl_init_with_image("pathtosysji", "sys.ji");
The path must be the abs path.
ie: /home/kostav/workspace/juli/Debug/../lib/x86_64-linux-gnu/julia/sys.ji

Link c++ object during runtime?

I'm trying to write my first game in c++, and I want it to dynamically load everything from files. This includes the enemies, and I was wondering if there was a way to dynamically include their code at runtime, instead of linking the on compile, so that the levels are easily interchangeable. Lua might be an option but I have no clue where to start, and dll seems to be Windows-only (and I wouldn't know where to start there anyway). Can anyone help with this?
tl;dr I want to link in code to my c++ game at runtime.
For the Lua approach you first need to choose the version first. Right now there is the major version 5.1 and 5.2. My previous work was using 5.1 and for my new project I decided to update to 5.2, however I found that my favorite script wrapping tool (SWIG) does not work with 5.2. Just something to decide at the beginning, because you do not want to get a version working and then have to change it.
Lua comes with makefile build environment. My first experience of trying to build on Windows was a bit of a nightmare, did not appear to just run out-of-the-box, so I opted to create my own Visual Studio project at the time, and just include all the .C files in the project. There are two files which need to selectively included/excluded depending on how you intend to compile: lua.c and luac.c. If you are planning to embed Lua in your app, then exclude both of these files; they both contain a main() function and are designed to build console apps. Include all the rest of the C files in your project.
You should be able to compile easy from this point.
When you include the headers of Lua, keep in mind that the functions are C functions so if you are including them from C++ you need to wrap the file inclusion inside of: extern "C" {} - example: C++ Lua 5.1 Issue
Wrapping your interfaces in another topic and there are lots of resources available. My favorite is SWIG but there are lots of options, including hand coding the conversion of your C/C++ -> LUA -> C/C++ code. Would recommend just focusing on getting the first part working first, get the interpreter embedded so that you can run a "hello, world!" script from Lua inside your app.
So going by your requirement of crossplatform use and dynamic linking, what you're probably looking for is an environment like QT which has QLibrary: https://stackoverflow.com/a/9675063/453673
But https://softwareengineering.stackexchange.com/questions/88685/why-arent-more-desktop-apps-written-with-qt
MingW is the open-source equivalent for Visual C++, so it can help you writing code for Windows (though if I had a choice, I'd directly use Visual C++). The way dll's are loaded in Windows is somewhat similar to the way they're loaded in Linux, so you'll be able to write code with #ifdef's to do conditional compilation. I've written one such program a couple of years back.
To load a shared library(always with .so as suffix) under Linux, you could use dlopen(), dlsym() and dlclose()

Including R standalone library in a C++ source tree

I am working on a large open source C++ program that uses the R standalone library (libRmath, Ubuntu/Debian package r-mathlib). I want to remove this dependency: I'd like to include the source code from the R standalone library in my source tree, without the entire R source code. Can this be done?
Unfortunately, the R standalone library seems tightly coupled to the rest of the R code. In the standard R source tarball, the same configure script is used for the main package and the standalone library. This configure script doesn't play well on different platforms. I am hoping that the standalone library is available as its own "standalone" source tree.
Related note: I've looked at the Rinside library, particularly via this thread. I think the method I am describing would make distribution easier, for 2 reasons: 1) users that already have R installed won't have to reconfigure their installation (and can maintain a different version as their default installation); 2) users without R won't have to install it.
Update: fixed a typo - I'd originally referred to Rinside as Rinclude
A few points for you:
Can this be done? Of course it can, just copy and paste the code for r-mathlib into your project. The licensing for that library is a very liberal LGPL. Now, is it a good idea? I don't think so. Shared libraries have upside in that you get bugfixes, easier and shorter build etc/
Seems tightly coupled to the rest of the R code. No, that is factually incorrect. The whole point of r-mathlib is that you can depend just on it. I think it may still needs R headers on your system but if you define the MATHLIB_STANDALONE variable. E.g., on my Ubuntu box I just did gcc -o /tmp/rmathTest /usr/share/doc/r-mathlib/examples/test.c -lRmath -lm and the resulting binary depends on libRmath alone, not libR.
Configure script doesn't play well on other platforms. That is a bold statement, don't make it anywhere near Prof Ripley. R is amazingly portable, and I fear your claim is a little lacking in empirical basics here.
What you are after can be done in different ways, and has been done in different ways.
And yes there is ample documentation as well. Start with 'Writing R Extensions' and 'R Admin + Inst' and by all means come back with follow-up questions.
Lastly, in case you change your mind and you do want R inside C++, you may want to consider RInside as well. That does however create a depedencies on R, Rcpp and RInside itself.

Activate Run the libLAS C++ library within R

I would like to use the libLAS C/C++ library functions within R to import, analyse, export terrestrial lidar data. libLAS is a C/C++ library for reading and writing the very common LAS LiDAR format ( http://liblas.org/index.html ).
Would it be possible to use the Rcpp package to run this library (or other packages)? http://dirk.eddelbuettel.com/code/rcpp.html
Or should I compile and install it in order to use it following the compilation instructions http://liblas.org/compilation.html ? I am working on a MacOSx 10.6.5. As such I could also use it within Open Source GIS GRASS as described in the following wiki http://grass.osgeo.org/wiki/LIDAR#Micro-tutorial_for_LAS_data_import .
All advice is welcome related to reading and processing LIDAR data with R/GRASS.
Thanks,
Jan
For the question
Would it be possible to use the Rcpp
package to run this library (or other
packages)?
the answer is whopping Yup! as using it for glueing R to a given C/C++ library was pretty much the reason Rcpp was written for. Come and see the documentation and/or the rcpp-devel list for examples. There is some exciting new stuff happening with Rcpp modules but you can also get going the old-fashioned way of writing your wrapper. Rcpp makes mapping and R and C++ types (in both directions) a lot easier.

Educational IDE to start programming in C++?

I'am aware there has been a generic question about a "best IDE in C++" but I would like to stress I'm a new to C++ and programming in general. This means I have the needs of a student:
relatively easy and unbloated working environment
things just work, focus on the code
color coding to show the different language features (comments, etc)
not too unfriendly (not a simple editor, something to handle projects from start to finish)
cross-platform so not to be bound with specific system practices
I think the above are relatively reasonable demands for an educational IDE, perhaps excluding the last as such universal tool might not exist. Any ideas?
It depends on which world are you coming from to learn C++.
Do you have previous Java experience? - Use Eclipse CDT.
Have used .NET previously? - Go with Visual Studio C++ Express Edition (and then throw it away if you really need multiplatform IDE, not just code).
Are you an Unix guy? Use just a syntax-highlighting editor + Makefile. When you want to learn basics of the C++, the project should not be complicated and it is well invested time to learn how the C++ compiler is called with preprocessor options, etc.
Code::Blocks is free and really easy to install and use. I always recommend it to my students.
I've heard good things about Code::Blocks. Might be a bit complex, but you can close any unneeded panes, and it's cross-platform.
I would recommend Komodo Edit.
It functions as a great text editor that I've used on Ubuntu, Windows(XP/7) and OSX. It's big brother is a full blown IDE but KE still allows for projects and some great extensions. It's also free and open source. I found it easy to get started quickly with it and as your skills grow, it has the ability to keep up.
Edit to add a link to ActiveState's community site for Komodo Extensions. If you decide to try out KE, I'd suggest the RemoteDrive Tree (ssh,ftp,scp remote editing) and Source Tree as a start.
If you are using both windows and linux (as your comment indicates), I'd recommend Qt Creator. Qt is cross platform so your apps will work on linux, windows, and mac. Qt has excellent documentation, too, so it's very newbie friendly. Signals and Slots take a bit of getting used to, but IMO it's worth it.
Until the last point I would have said Microsoft Visual C++ Express Edition, which is free and fits your first 4 criteria. Cross platform you'd be looking at something like emacs or vim, neither of which are particularly friendly. On Windows I actually use Notepad++ for small C++ programs as it has good syntax highlighting and a (limited) intellisense.
I'd recommend Eclipse CDT as it does good code completion and it builds code on the fly, so you can see your errors immediately which is very good for a language studying.
Assuming Linux/Unix like system ...
I've found out that it's much easier and beneficial to go the other way round. Try using 'simple' editor like vim and for C++ just Makefiles to compile using gcc and linker.
I've started using that at uni and 5> years and couple of companies later it's still the easiest and most flexible option because you have quick access to all settings in one simple file.
Even when you switch to IDE later on you will know what to look for if things don't work because you will know the basics for example what are the steps to go from source file to object file and link to binary executable, how to handle libraries and so on. These things change between IDE's and are often complicated to trace and modify.
You can start with simple makefile and keep improving it over years. It's easy to copy it to your project directory and update file names - for C++ the compilation process will be fairly standard between projects.
I highly encourage you to consider this option. I've learned a lot doing it that way and you have a backup plan when you IDE just wouldn't work.
I keep one generic Makefile that compiles main.cpp into executable. To compile something quickly I just copy it into directory and make.
My current workflow is to open all files in project directory (flat file system) with vim (vim *.cpp *.hpp), edit, compile with :mak (or :mak -C .. debug) from within vim to invoke the Makefile stored in relevant directory, after compiling it'll jump to first warning/error, use :cn to go over errors, fix what's needed, open errors in separate window with :cope (close with :clo or unload file with :bd, jump between split windows with ctrl-w ctrl-w or ctrl-ww - hold ctrl and press w twice) ...
Vim has syntax highlighting millions of other features, I'm using tags (or ctags) to navigate code from within vim and so on.
Personally, it's my opinion that all C++ IDEs suck. When I write C or C++, I tend to use some sort of powerful programmer's text editor along with command line compilation. If I'm just messing around and have a couple source files, I'll just invoke gcc -g -o myprog *.c on the command line myself. If I have a more involved project, I'll just write a simple makefile. You could also look into gmakemake if you don't want to bother learning makefile syntax just to compile your programs.
On the Mac side, I have always been a fan of both BBEdit and TextMate, but much more so of the latter, especially given its lesser price tag and more modern feel. Both have project organization features.
On Windows, I'd stick with either e (which is basically a port of TextMate to Windows) or Notepad++. The downside of Notepad++ is that it doesn't have any project organization features, whereas e does. You could also look at SciTE, but like Notepad++, it has no project org features.
As for Linux, I'm personally unsure. I'd stick with other people's answers covering that platform for recommendations there.