VS Code: g ++ command to run multi-file compilation


1. Background

  VS is a substantially complete set of development tools, and run the compiled file import and other operations is relatively easy, but only one code VS Code editor, which is essentially achieved compile and run on the C / C ++ program through Mingw-w64. It is summarized using the g ++ compiler command to run multiple .cpp files.

2. Compile and run

2.1 Single File

main.cpp Code:

#include <iostream>
using namespace std;

int main()
{
    int m1 = 3, m2 = 6;
    int sum = m1 + m2;
    cout << m1 << "+" << m2 << "=" << sum << endl;
    return 0;
}

g ++ command:

g++ main.cpp -o main.exe && main.exe

Note:
  Different versions of the operating system, which command the terminal may be different, use windows7 (32-bit) herein. In Windows 10 (64 bits) which command the terminal may be as follows:

g++ main.cpp -o main.exe; ./main.exe

  If main.cpp not under the root directory, you need to jump to the directory or use main.cpp absolute address.

cd E:\1 && g++ main.cpp -o main.exe && main.exe
g++ E:\1\main.cpp -o E:\1\main.exe && E:\1\main.exe

operation result:
operation result

File in the same directory 2.2

main.h Code:

#include <iostream>
using namespace std;

main.cpp Code:

#include "main.h"

int main()
{
    int m1 = 3, m2 = 6;
    int sum = m1 + m2;
    cout << m1 << "+" << m2 << "=" << sum << endl;
    return 0;
}

g ++ command:

g++ main.cpp -o main.exe && main.exe

operation result:
operation result

sum.h Code:

#include <iostream>
using namespace std;

extern int m1, m2;
extern int sum(int x1, int x2);

sum.cpp Code:

#include "sum.h"

int m1 = 3, m2 = 6;

int sum(int x1, int x2)
{
    return x1 + x2;
}

main.cpp Code:

#include "sum.h"

int main()
{
    cout << m1 << "+" << m2 << "=" << sum(m1, m2) << endl;
    return 0;
}

g ++ command:

g++ main.cpp sum.cpp -o main.exe && main.exe

operation result:
operation result

File under 2.3 in the same directory to another folder

main.h Code:

#include <iostream>
using namespace std;

main.cpp Code:

#include "main_h/main.h"

int main()
{
    int m1 = 3, m2 = 6;
    int sum = m1 + m2;
    cout << m1 << "+" << m2 << "=" << sum << endl;
    return 0;
}

g ++ command:

g++ main.cpp -o main.exe && main.exe

operation result:
operation result

sum.h Code:

#include <iostream>
using namespace std;

extern int m1, m2;
extern int sum(int x1, int x2);

sum.cpp Code:

#include "sum.h"

int m1 = 3, m2 = 6;

int sum(int x1, int x2)
{
    return x1 + x2;
}

main.cpp Code:

#include "sum/sum.h"

int main()
{
    cout << m1 << "+" << m2 << "=" << sum(m1, m2) << endl;
    return 0;
}

g ++ command:

g++ main.cpp sum/sum.cpp -o main.exe && main.exe

operation result:
operation result

Files in another directory 2.4

main.h Code:

#include <iostream>
using namespace std;

main_h folder location

main.cpp Code:

#include "C:\Users\Administrator\Desktop\main_h\main.h"

int main()
{
    int m1 = 3, m2 = 6;
    int sum = m1 + m2;
    cout << m1 << "+" << m2 << "=" << sum << endl;
    return 0;
}

g ++ command:

g++ main.cpp -o main.exe && main.exe

operation result:
operation result

sum.h Code:

#include <iostream>
using namespace std;

extern int m1, m2;
extern int sum(int x1, int x2);

sum.cpp Code:

#include "sum.h"

int m1 = 3, m2 = 6;

int sum(int x1, int x2)
{
    return x1 + x2;
}

sum folder location

main.cpp Code:

#include "C:\Users\Administrator\Desktop\sum\sum.h"

int main()
{
    cout << m1 << "+" << m2 << "=" << sum(m1, m2) << endl;
    return 0;
}

g ++ command:

g++ main.cpp C:\Users\Administrator\Desktop\sum\sum.cpp -o main.exe && main.exe

operation result:
operation result

3. Summary

  1. g ++ and gcc is different. g ++ is a C ++ compiler. gcc compiler is C, C ++ compiler To program further need to take "-lstdc ++".

  2. If the file import only .h file, g ++ command unchanged.

g++ ***.cpp -o ***.exe && ***.exe
  1. If the import file .cpp file, needs to be modified in accordance with the command g ++ .cpp file location.

In the same directory on the same folder:

g++ ***.cpp ***.cpp -o ***.exe && ***.exe

The same directory different folder (relative path):

g++ ***.cpp ***\***.cpp -o ***.exe && ***.exe

Different directory (absolute path):

g++ ***.cpp C:\***\***\***.cpp -o ***.exe && ***.exe
  1. For small files, such as a two .h and .cpp file, use the g ++ command-line compiler more convenient. But if more documents, more than a dozen, it is recommended to compile using mingw32-make.exe bin directory of Mingw-w64. Write down all of the commands in the makefile, simply enter the make in a terminal to run. Would simply run the makefile, you can refer to the Makefile explain a (how to use under Windows Makefile file) article, want to learn more, you can peruse the god of Chen Hao Makefile tutorial (absolute classic, look at all the problems this one is enough) .
Published 77 original articles · won praise 25 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_34801642/article/details/103770219
Recommended