![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Dec 2007
Posts: 28
Rep Power: 0
![]() |
Cross-platform exec()
Hello everyone,
I am quite motivated to code a new programming language this summer. My idea is, given the source code of a program written in this language, to translate it to c/c++ and then compile it using gcc/g++. Now, before starting the project, I would like to know if there is a way to do this cross-platform. What I mean is : 1) On UNIX-like systems, you only have to do fork() and exec("g++ -o etc...") 2) On Wind@ws, I don't know of an exec()-like substitute SC, perhaps system(), but I'm not sure if I should use it. What would be the correct way of doing this? Thank you |
|
|
|
|
|
#2 |
|
Expert Programmer
|
Re: Cross-platform exec()
I'm not sure, but popen might be what you're looking for.
|
|
|
|
|
|
#3 |
|
Expert Bug Developer
Join Date: Apr 2008
Posts: 21
Rep Power: 0
![]() |
Re: Cross-platform exec()
>perhaps system(), but I'm not sure if I should use it.
Depends on what you need. system() pauses your program, opens a new shell, and executes the command you give it in its argument. When the program finishes executing, it will exit, the shell will close, and your program will resume execution. popen, like titaniumdecoy mentioned, is for opening a pipe to a program. That is, it'll execute a program, and then you can use the pipe it's opened to communicate with the other process. In other words, your program isn't halted until the process is done (like system()). Last edited by Sorrofix; Jun 8th, 2008 at 3:28 PM. |
|
|
|
|
|
#4 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,223
Rep Power: 5
![]() |
Re: Cross-platform exec()
There is no direct fork() equivalent under windows [creating a duplicate of the current process with the same state, except being able to detect it is forked] but it is possible to create a new process to execute a new program.
Libraries with some compilers (Microsoft, Borland) targeting windows do support an execl() function within a header named something like <process.h>. Also look up the CreateProcess() function in the win32 API. execl() functions and system() functions under windows will typically be implemented using CreateProcess() or related functions. |
|
|
|
|
|
#5 |
|
Expert Programmer
|
Re: Cross-platform exec()
You could use the pthread library, although I believe it requires a DLL on Windows.
|
|
|
|
|
|
#6 |
|
Newbie
Join Date: Dec 2007
Posts: 28
Rep Power: 0
![]() |
Re: Cross-platform exec()
Thank you! I didn't know so much possibilities existed on Wind@ws! I will definately look up popen() and execl(). system() doesn't look like a very good choice, given that you can't communicate with your "child", e.g. you don't know when it has completed it's task or failed, and if so, for what reason.
Thanks for the help! |
|
|
|
|
|
#7 |
|
Expert Bug Developer
Join Date: Apr 2008
Posts: 21
Rep Power: 0
![]() |
Re: Cross-platform exec()
>you don't know when it has completed it's task or failed
system() waits until its child process has completed. Upon completion, system() will return the value returned by the child process. So yes, using the return value of system(), you can determine the success of the program you ran. |
|
|
|
|
|
#8 | |
|
Newbie
Join Date: Dec 2007
Posts: 28
Rep Power: 0
![]() |
Re: Cross-platform exec()
Quote:
)...had no clue that it actually returned something!So what does it return? An int? |
|
|
|
|
|
|
#9 | |
|
Expert Programmer
|
Re: Cross-platform exec()
man 3 system:
Quote:
|
|
|
|
|
|
|
#10 | ||
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,223
Rep Power: 5
![]() |
Re: Cross-platform exec()
Quote:
The C standard has this to say on system(), and is all that can relied upon in cross-platform development. Quote:
|
||
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Coder's Block Arena - The Game AI Platform | Sane | Existing Project Development | 23 | May 6th, 2008 9:12 PM |
| Which Flavor: CPython or IronPython and Exec Opts | Kigneer | Python | 2 | Apr 6th, 2008 10:36 PM |
| Php with Sun Java System Application Server Platform | lucifer | PHP | 0 | Jun 7th, 2007 4:03 AM |
| Need help from someone who has downloaded microsoft platform sdk | rsnd | Other Programming Languages | 3 | Feb 18th, 2006 5:04 PM |
| Platform Api | jstephens | C | 3 | Jan 2nd, 2006 5:17 PM |