## system and system2 in R [Software]

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 🖖🏼 Довге життя Україна!
Helmut Schütz

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