system and system2 in R [Software]

posted by Helmut Homepage – Vienna, Austria, 2022-08-30 03:10 (803 d 01:35 ago) – Posting: # 23241
Views: 2,842

Hi ElMaestro,

❝ I need to spawn the gcc compiler now and then from within R. That's easily done with the system or system2 functions. But I also need to capture the console output that is produced by gcc. And that's my trouble.


❝ So, along the same lines I hope to execute gcc as:

x=system(command="gcc -Wall -O2 myfile.c -o Target", intern=T)

❝ print(x)


❝ and even though gcc does the compilation nicely, x is -surprisingly to me- empty. It is not because gcc does not spit out info and the command is fully ok [foot note 1]. gcc prints plenty lines to the console. I just do not know how to capture them with R.


I tried this

#include <stdio.h>
int main() {
  printf("Hello, world!\n");
  return 0;
}

saved as hw.c
gcc-x86_64-w64 10.3.0 called from R 4.1.2:

system(command = "gcc -Wall -O2 hw.c -o hw", intern = TRUE)
character(0)

compiles to hw.exe
In R 4.2.1:

system(command = "hw", intern = TRUE)
[1] "Hello, world!"

Do I understand you correctly that you want to get eventual warnings / errors of the compiler in R? If yes, your approach cannot work. Since the file compiled successfully, R cannot ‘know’ of any problems. You have to tell the compiler to redirect them to a file and read it in R. Perhaps this thread at stackoverflow helps.

PS: This thread is perfectly On-topic.

Dif-tor heh smusma 🖖🏼 Довге життя Україна! [image]
Helmut Schütz
[image]

The quality of responses received is directly proportional to the quality of the question asked. 🚮
Science Quotes

Complete thread:

UA Flag
Activity
 Admin contact
23,291 posts in 4,891 threads, 1,661 registered users;
79 visitors (0 registered, 79 guests [including 15 identified bots]).
Forum time: 03:45 CET (Europe/Vienna)

I never did anything worth doing by accident,
nor did any of my inventions come by accident;
they came by work.    Thomas Alva Edison

The Bioequivalence and Bioavailability Forum is hosted by
BEBAC Ing. Helmut Schütz
HTML5