system and system2 in R [Software]

posted by Helmut Homepage – Vienna, Austria, 2022-08-30 03:10 (769 d 10:04 ago) – Posting: # 23241
Views: 2,635

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,247 posts in 4,885 threads, 1,652 registered users;
72 visitors (0 registered, 72 guests [including 6 identified bots]).
Forum time: 13:15 CEST (Europe/Vienna)

The real struggle is not between the right and the left
but between the party of the thoughtful
and the party of the jerks.    Jimmy Wales

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