system() question

Hello all,

Some background information:

I have a program which copies a bunch of files from a source folder to a user-specified folder which includes a build file.

The program itself creates a specific specification file in the user-specified folder.

Copying the file and creating the specification file is a success.

I can't however build the program from my code.
Everything works fine when i manually navigate to the path and run 'sh build'.

But i can't seem to navigate to the path and run the command using system() as i did for copying.

this is what I've tried:

fullpath is indeed correct: /home/gip/svn/Accent/test

Navigation does not work as i thought it would as when ls the current directory structure, the output remains consistently the same. It keeps printing the contents of the calling programs directory and not the path I thought I had just navigated to.
1
2
3
4
5
6
cout << fullpath << endl;
    stringstream gotopath;
    gotopath << "cd " << fullpath;
    system(gotopath.str().c_str());
    cout << system("ls") << endl;
// now I would want to run sh build from the gotopath 


How must I achieve this feat in C++


The environment created to run the shell command is deleted when the system() function exits. So the working directory reverts back after the 'cd' command finishes.
I would be tempted to write a shell script to change directory and to do everything you need to do there. Then call that shell script using system().
Last edited on
Have you tried "cd path; ls"?
@moorecm: your solution seemed promising, but the program crashed after compilation
@Galik: Well, I've written my very first shell file...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#!/bin/sh 
cd ..
cd Accent/
cp -r source/build $1
cp -r source/container $1
cp -r source/result $1
cp -r source/extra.h $1
cp -r source/spec.lex $1
cp -r source/auxil.c $1

cd $1/
chmod +x build
chmod 755 build
./build


The last 3 commands provoke the following error (when using system() to run file from netbeans)

cc: Internal error: Illegal instruction (program collect2)
Please submit a full bug report.
See <http://bugzilla.redhat.com/bugzilla> for instructions.

Fedora's bugreporter sometimes lists chmod or the actual compile command in the buildfile as the wrongdoers.

I assure you navigation commands are correct

All copy instructions and such work, the problem really seems to lie in the last 3 commands
What am I doing wrong?
What is $1? Is it a directory name? If so then it might me wise to create it first. Otherwise you will rename your first copy item to $1 rather than moving it into $1.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#!/bin/sh 
cd ..
cd Accent/
mkdir -p $1
cp -r source/build $1
cp -r source/container $1
cp -r source/result $1
cp -r source/extra.h $1
cp -r source/spec.lex $1
cp -r source/auxil.c $1

cd $1/
chmod +x build
chmod 755 build
./build
Hello again,

I've taken all you said in to consideration.

I was able to narrow down the exact root of the problem.

I now have two script files. The missing files used in the second script file is actually generated by my program. I can confirm that nothing goes wrong with the generation of these files.

The first script file (works perfectly)

1
2
3
4
5
6
7
8
9
#!/bin/sh 
mkdir -p $1
cd ..
cp -r Accent/source/build Parser/$1
cp -r Accent/source/container Parser/$1
cp -r Accent/source/result Parser/$1
cp -r Accent/source/extra.h Parser/$1
cp -r Accent/source/spec.lex Parser/$1
cp -r Accent/source/auxil.c Parser/$1


And the second
1
2
3
4
5
6
7
8
#!/bin/sh 
cd $1
ACCENT=../../Accent/accent/accent
ENTIRE=../../Accent/entire/entire.c

$ACCENT spec.acc
flex spec.lex
gcc -o UniApo yygrammar.c lex.yy.c auxil.c $ENTIRE



Everything works fine up until the last line of the second script file
gcc -o UniApo yygrammar.c lex.yy.c auxil.c $ENTIRE

I'm running both script from a program which was build using netbeans...
And sometimes, once in a blue moon, everything builds to the end. The automatic crash detection of my fedora box however yields some crash in gcc, core dumped and whatnot.

If I exclude the gcc line, No crashes are detected and everything is perfect.
I've tried cc also.

Bear in mind, manually running these commands from the shell itself work just fine.

Is there any way to circumvent these crashes??

thank you

If it always runs in the shell fine, consider using that shell for the script (/bin/bash?).
The problem remains persistent little bugger even when using bash.

I'm using gnome, using the gnome terminal perhaps I must use something else

thanks everybody!
Last edited on
On thing you can try:
1
2
$(which flex) spec.lex
$(which gcc) -o UniApo yygrammar.c lex.yy.c auxil.c $ENTIRE


EDIT: Actually that probably won't work.

Try doing which flex from the shell and see if it has an unusual path. Then put the full path to flex in the script. Same with gcc.
Last edited on
aha,

gcc (the wrongdoer) is located in /usr/lib64/ccache/cc

well-behaved flex in usr/bin/flex

when i put /usr/lib64/ccache/cc -o UniApo yygrammar.c lex.yy.c auxil.c $ENTIRE in the Make file I still get the same results :(
Topic archived. No new replies allowed.