View Full Version : Executing string as a command in C, is it possible, and if so, how to do it?
crazyfuturamanoob
November 10th, 2008, 02:12 PM
In python, exec does this thing. But is there any equivalent in C for it?
I'm suspecting it is impossible, as C isn't interpreted language. :(
Sinkingships7
November 10th, 2008, 02:17 PM
I think you're looking for the system() function. It takes a string as an arg, like this:
system("cp /data/files /other/location");
You'll need the stdlib.h header for this function.
Cracauer
November 10th, 2008, 02:19 PM
dlopen(3)
CptPicard
November 10th, 2008, 03:37 PM
No, there is no "eval" function (in lisp terms) in C -- runtime evaluation of code -- for exactly the reason you suspect.
crazyfuturamanoob
November 10th, 2008, 03:48 PM
@Sinkingships7:
I don't want to run a command in terminal. I want to execute the string as C code, like:
executemystring("int a = 5;");
printf("%i", a);
crazyfuturamanoob
November 10th, 2008, 03:51 PM
No, there is no "eval" function (in lisp terms) in C -- runtime evaluation of code -- for exactly the reason you suspect.
Perhaps I could then embed Python or Lua or any other interpreted language within my program?
CptPicard
November 10th, 2008, 04:21 PM
Perhaps I could then embed Python or Lua or any other interpreted language within my program?
Yep, that's the ticket.
Cracauer
November 10th, 2008, 08:08 PM
@Sinkingships7:
I don't want to run a command in terminal. I want to execute the string as C code, like:
executemystring("int a = 5;");
printf("%i", a);
As I said, dlopen(3) is your only option.
Write C, compile it to an object file, load it, call the function inside.
Powered by vBulletin® Version 4.2.2 Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.