Does an R compiler to C/C++ exist?
A byte code compiler will be part of the R 2.13 release. By default it is not used in this release but it is available; I expect the 2.14 release will by default byte compile all base and recommended packages. The compiler::compile help page and the R Installation and Administration Manual give some more details.
Is R an interpreted or compiled programming language?
The R FAQ says: "The core of R is an interpreted computer language".
How to check if a function exists in C/C++?
While other replies are helpful advices (dlsym
, function pointers, ...), you cannot compile C++ code referring to a function which does not exist. At minimum, the function has to be declared; if it is not, your code won't compile. If nothing (a compilation unit, some object file, some library) defines the function, the linker would complain (unless it is weak, see below).
But you should really explain why you are asking that. I can't guess, and there is some way to achieve your unstated goal.
Notice that dlsym
often requires functions without name mangling, i.e. declared as extern "C"
.
If coding on Linux with GCC, you might also use the weak
function attribute in declarations. The linker would then set undefined weak symbols to null.
addenda
If you are getting the function name from some input, you should be aware that only a subset of functions should be callable that way (if you call an arbitrary function without care, it will crash!) and you'll better explicitly construct that subset. You could then use a std::map
, or dlsym
(with each function in the subset declared extern "C"
). Notice that dlopen
with a NULL
path gives a handle to the main program, which you should link with -rdynamic
to have it work correctly.
You really want to call by their name only a suitably defined subset of functions. For instance, you probably don't want to call this way abort
, exit
, or fork
.
NB. If you know dynamically the signature of the called function, you might want to use libffi to call it.
.C Interface to R
Assuming C:\temp\hello.c
exists try this from the windows cmd line (carefully checking that Rtools, R and the paths used exist):
cd c:\temp
path C:\Rtools\bin;C:\Rtools\gcc-4.6.3\bin;%ProgramFiles%\R\R-3.0.2\bin\x64;%path%
R CMD SHLIB hello.c
Rgui
Now in R:
dyn.load("hello.dll")
.C("hello", 3L)
Note: Also there are some batch files here that may help:
http://batchfiles.googlecode.com
http://code.google.com/p/batchfiles/source/browse/#svn%2Ftrunk
See Rpathset.bat
, R.bat
and the documentation batchfiles.md
.
Update Corrections and improvements.
Related Topics
When Does Move Constructor Get Called
List of C++ Name Resolution (And Overloading) Rules
Reading an Application's Manifest File
Safer But Easy-To-Use and Flexible C++ Alternative to Sscanf()
Compile Openmp Programs with Gcc Compiler on Os X Yosemite
Segfaults in Malloc() and Malloc_Consolidate()
Should the Copy-And-Swap Idiom Become the Copy-And-Move Idiom in C++11
Making a Borderless Window with for Qt
Std::Array with Aggregate Initialization on G++ Generates Huge Code
Debug Assertion Failed! Expression: _Acrt_First_Block == Header
Sharing Precompiled Headers Between Projects in Visual Studio
Why Isn't There an Operator[] for a Std::List
How to Use Libraries Compiled with Mingw in Msvc
How to Call Memcpy() and Memmove() with "Number of Bytes" Set to Zero
How to Check If a Key Is Pressed on C++
Undefined Reference to Google::Protobuf::Internal::Empty_String_[Abi:Cxx11]