0

I am using Qt to create a project which compiles external c++ project which has one .cpp file or multiple .cpp and .h files.

Using system("command") I can compile and run ONE .cpp file but I could not figure out how to compile multiple files:(

I tried different ways:

  1. system("cd the_files_directory") then system("g++ *.h *.cpp -o execute") but system("dir") told me it didn't change the directory.

  2. system("g++ the_files_directory/*.h the_files_directory/*cpp -o execute)

  3. system("g++ -fworking-directory the_files_directory/*.h the_files_directory/*cpp -o execute")

All work in normal windows or Linux terminal but just didn't work in system(command).

Thanks in advance

5
  • Why are you passing "*.h"? Were you intending to try and compile your header files? Commented Aug 3, 2016 at 8:50
  • 1
    system("cd the_files_directory") invokes a shell process which executes the command cd the_files_directory and then exits. Your shell and CWD are unchanged. Try using the chdir call. Commented Aug 3, 2016 at 8:52
  • Each call to system() opens it's own shell, and exits it after completion of the command. So doing such like system("cd the_files_directory") is futile. Commented Aug 3, 2016 at 8:52
  • 1
    at some point, it might be easier to use Makefiles instead of trying to have all the flags, files & directories in a single compiler call. You could still pass variables to the command if you need it to be dynamic. Commented Aug 3, 2016 at 9:13
  • This just seems like a terrible idea in general. Why are you trying to build C++ source from within another C++ program? What sort of messed up build system is that?! Commented Aug 3, 2016 at 9:47

2 Answers 2

1
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
Sign up to request clarification or add additional context in comments.

Comments

0

-Guys! I found my own solution last night! -Here are my steps which will surely work after so much experiment for different ways by myself before you guys posts!

-Step1: -Move my whole project out from Qt project location and remove .pro and other files generated by Qt and just keep my own .cpp files and .h files. I was doubting if Qt is too protective because I tried to move my project to the "current location" which is Qt project build folder by using -system("copy project_path/xxx.cpp xxx.cpp"); this didn't give an error but said -(0) files are copied. -So I decided to get rid of Qt lol

-Step2: -cd the folder where I put my project in Windows Command Prompt

$cd project_path

-Step3: -compile my own project in Windows Command Prompt

project_path$g++ *.h *.cpp -o execute

compile command: g++ project_path\main.cpp project_path\pair.cpp project_path\map.cpp project_path\pair.h project_path\map.h

(i tried to compile not in its own directory but somehow doesn't work:(( ) ps: inside my project, I have these codes to compile and run the external program:

    void compile(){
    string s = "g++ "
               "project_path\\main.cpp "
               "project_path\\pair.cpp  "
               "project_path\\map.cpp  "                 
               "project_path\\pair.h  "
               "project_path\\map.h";//only this works!!!!

        char *compile_command = new char[s.length()];
        compile_command[0] = '\0';
        strcat(compile_command, s.c_str());
        cout << "compile command: " << compile_command << endl;
        system(compile_command);
    }

-I tried to add "-o execute" to the end of string s but it won't work correctly later. and I tried to set s = "g++ project_path/*.h project_path/*cpp" but it didn't work either:((

    void run(bool input, string input_path){
        if(input){
            string s = "a <" + input_path + "> print.txt";
            char *execute_command = new char[s.length() + 1];
            execute_command[0] = '\0';
            strcat(execute_command, s.c_str());
            system(execute_command);
            cout << "execute_command: " << execute_command << endl;
        }else{
            system("a > print.txt");
        }
    }

    int main(){
        compile();
        run(false, "");
    }

-Step4: -run execute.exe

project_path$execute

then a.exe and print.txt will be seen in the project_folder and print.txt showing correct content.

-I also tried to compile and run for some projects requiring input with input.txt. Works fine too!!!

-I will try the solutions posted above too, later! All explanations are very good! Thank all very much! good day :D

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.