• 0

[C] system() function


Question

I want to use the system() function to call an external program. I compiled my program using cygwin and it worked. But, if try to run it outside cygwin(windows command prompt), it doesn't work. Any ideas?

Link to comment
Share on other sites

6 answers to this question

Recommended Posts

  • 0

When you say "It doesn't work", what exactly do you mean?

Do you get an error, warning, or something along those lines?

Also, remember that applications compiled with cygwin require that cygwin1.dll is in the same directory of the application that you are running, or in a "global" path like C:\WINDOWS or C:\WINDOWS\system32 (you can find cygwin1.dll in the cygwin root directory under the bin folder.)

bwx

Link to comment
Share on other sites

  • 0

on cygwin it works correctly. outside it, it doesn't display any warning or error, the program i call just doesn't run.

the cygwin1.dll is in the same dir.

Link to comment
Share on other sites

  • 0
on cygwin it works correctly. outside it, it doesn't display any warning or error, the program i call just doesn't run.

the cygwin1.dll is in the same dir.

Try checking the return value the system() call returns.

system function reference: http://www.cplusplus.com/ref/cstdlib/system.html

Another thing that could cause this is if the application you're running with system() doesn't exist. Mind posting your code?

bwx

Link to comment
Share on other sites

  • 0

This is the code:

sprintf(command,"gnuplot.exe %s",tempCommandname);

	retCode=system(command);

	printf("Returned code: %i\n",retCode);
	printf("Errno: %i\n",errno);

On cygwin it runs this way:

Returned code: 0

Errno: 0

On the windows console:

Returned code: 127

Errno: 0

I have no idea what the returned code means...

I also tried a very simple program:

void main(void)
{
	system("dir");
}

On cygwin, it displays the directory contents. On the windows console, nothing happens:

C:\Documents and Settings\Fly Soul\Desktop\circ>a.exe

C:\Documents and Settings\Fly Soul\Desktop\circ>

Link to comment
Share on other sites

  • 0
On the windows console:

Returned code: 127

Errno: 0

I have no idea what the returned code means...

I looked it up and it seems windows error code 127 = "The specified procedure could not be found."

I tried this on my copy of cygwin, and it does the same thing; works in cygwin, but not in the windows command shell. After looking it up, I realized it's a bug with cygwin, so you might have to try a workaround.

Check out: http://www.cygwin.com/ml/cygwin/2001-11/msg00255.html

Your best bet is to use a native-windows C compiler.

Some are:

MingW (has a port of GCC)

Microsoft C++ Compiler (Inevitably works with C code aswell.)

LCC-Win32 (a favorite; known for generating compact binaries)

hope that helps,

bwx

Link to comment
Share on other sites

  • 0

Thanks for helping me!

I found the problem, the file cygwin\bin\sh.exe needs to be at the same directory of the program for the system() function work outside cygwin.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.