system("cd the_files_directory");
system("g++ *.h *.cpp -o execute");
The first line spawns a new process, a shell, which executes the command "cd the_files_directory", and exits. Your processes working directory is unaffected by the child process. So you are still in your original directory when you execute the g++ command.
Also, this command tries to compile header (.h) files, which would might cause g++ to fail, but is probably just redundant.
system("g++ the_files_directory/*.h the_files_directory/*cpp -o execute)
Again, you're trying to compile header (.h) files, which might cause g++ to fail, but even if it works, it is creating execute in your current directory and not in the_files_directory. You probably want
system("g++ the_files_directory/*.cpp -o the_files_directory/execute");
Your last variant
system("g++ -fworking-directory the_files_directory/*.h the_files_directory/*cpp -o execute")
Is the same command with debugging information about directories, so it has the same problems as above.
Given ('tfd' for 'the_files_directory'):
tfd/foo.cpp
#include <iostream>
#include "foo.h"
void foo() {
std::cout << "foo\n";
}
tfd/foo.h
extern void foo();
tfd/test.cpp
#include "foo.h"
int main() {
foo();
}
And at the top level, comp.cpp
#include <stdlib.h>
int main() {
system("g++ tfd/*.cpp -o tfd/execute");
}
I did the following:
osmith@vbx:~/tmp$ g++ -Wall -std=c++14 comp.cpp -o compiler
Execute the compiler:
osmith@vbx:~/tmp$ ls tfd
foo.cpp foo.h test.cpp
osmith@vbx:~/tmp$ ./compiler
osmith@vbx:~/tmp$ ls tfd
execute foo.cpp foo.h test.cpp
Now test the resulting executable:
osmith@vbx:~/tmp$ tfd/execute
foo
system("cd the_files_directory")invokes a shell process which executes the commandcd the_files_directoryand then exits. Your shell and CWD are unchanged. Try using thechdircall.system()opens it's own shell, and exits it after completion of the command. So doing such likesystem("cd the_files_directory")is futile.