2014-09-01 46 views
0

我按照本指南: http://www.biostat.jhsph.edu/~rpeng/docs/interface.pdf.C接口與R

我想C語言創建copiled代碼中的R 使用它,所以我創造了我的hello.c文件:

#include <R.h> 
void hello(int *n) 
{ 
int i; 
for(i=0; i < *n; i++) { 
Rprintf("Hello, world!\n"); 
} 
} 

我把它保存在c:\ temp 完成之後,我必須編譯C代碼。

老實說,我沒有任何想法,我在做什麼,但我特林在cmd中窗口寫:

cd C:\Program Files\R\R-3.0.2\bin\ 
R CMD SHLIB c:\temp\hello.c 

而我得到的錯誤:

cygwin warning: MS-DOS style path detected: C:/PROGRA~1/R/R-30~1.2/etc/i386/Makeconf Preferred POSIX equivalent is: /cygdrive/c/PROGRA~1/R/R-30~1.2/etc/i386/Makeco nf CYGWIN environment variable option "nodosfilewarning" turns off this warning. Consult the user's guide for more details about POSIX paths: http://cygwin.com/cygwin-ug-net/using.html#using-pathnames make: * No rule to make target c:\temp\hello.o', needed by c:\temp\hello.dll '. Stop.

這應該創建一個文件名「hello.so」。所以我可以把它叫做R:

 dyn.load("hello.so") 

     hello2 <- function(n) { 
           .C("hello", as.integer(n)) 
          } 

     hello2(5) 

但是我的命令並沒有創建hello.so文件。 也許我需要安裝另一個編譯器,但我無法在我使用的PC上安裝軟件。有沒有辦法做到這一點與Windows CMD?我的錯誤在哪裏?

+1

您需要按照R的[附錄D:Windows工具集](http://cran.r-project.org/doc/manuals/R-admin.html#The-Windows-toolset)安裝和管理手冊。 – 2014-09-01 16:56:00

+0

相關問題在這裏:http://stackoverflow.com/questions/13987667/r-external-interface與其他一些資源 – 2014-09-01 19:54:19

+0

我認爲'.C'即將消失。 Dirk在他的答案中提到了它http://stackoverflow.com/questions/23453373/function-c-argument-setup-fails-compilation。推薦'.Call'和'。外部'而不是 – 2014-09-01 20:44:07

回答

3

假設C:\temp\hello.c查找here,並且還在R docs這從presentation存在從窗戶CMD線(仔細檢查Rtools,R和所使用的路徑存在)嘗試:

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 

現在R:

dyn.load("hello.dll") 
.C("hello", 3L) 

注:也有一些在這裏的批處理文件,可以幫助:

http://batchfiles.googlecode.com

http://code.google.com/p/batchfiles/source/browse/#svn%2Ftrunk

Rpathset.batR.bat和文檔batchfiles.md

更新更正和改進。

+0

非常感謝你!它效果非常好! – dax90 2014-09-02 07:43:48

0

這並不回答你的問題直接,但你看看Rcpp包,它允許集成(從R調用)C?這可能會讓你的生活變得更容易,這取決於你在做什麼。

德克Ë

+0

這不會有幫助,因爲它們沒有從源代碼構建C/C++代碼所需的必要工具。 – 2014-09-01 16:53:45

+0

@JoshuaUlrich但安裝'Rtools'後應該可以吧? – 2014-09-01 16:54:40

+0

不...因爲OP說不能安裝軟件......錯過了。謝謝@Joshua – 2014-09-01 16:56:16