compile multiple files C++ linux

To compile multiple files in C++ on Linux, you can follow these steps:

  1. Open a terminal: Launch the terminal on your Linux system.

  2. Navigate to the directory: Use the cd command to navigate to the directory where your C++ files are located. For example, if your files are in the "code" directory, you can use the command cd code to navigate to that directory.

  3. Compile each file individually: Use the g++ command followed by the file name to compile each C++ file individually. For example, if you have two files named "file1.cpp" and "file2.cpp," you can use the following commands to compile them separately:

g++ -c file1.cpp g++ -c file2.cpp

The -c flag tells the compiler to generate object files without linking them.

  1. Link the object files: Once you have compiled all the individual files, you need to link them together to create the final executable. Use the g++ command followed by the object file names to link them. For example:

g++ file1.o file2.o -o output

The -o flag specifies the name of the output file (in this case, "output").

  1. Run the executable: After successfully linking the object files, you can run the executable by typing its name in the terminal. For example:

./output

This will execute the C++ program and display the output on the terminal.

That's it! By following these steps, you can compile multiple C++ files on Linux. Remember to replace "file1.cpp," "file2.cpp," and "output" with the actual names of your files.