You are viewing the article How to Compile C Programs with GNU (GCC) at Lassho.edu.vn you can quickly access the necessary information in the table of contents of the article below.
This article is co-authored by a team of editors and trained researchers who confirm the accuracy and completeness of the article.
The wikiHow Content Management team carefully monitors the work of editors to ensure that every article is up to a high standard of quality.
This article has been viewed 16,454 times.
This wikiHow teaches you how to compile a C program from source code using GNU (full name is GNU Compiler Cplection, abbreviated GCC) – a compiler for Linux and Minimalist Gnu (MinGW) on Linux. Windows.
Steps
By GCC on Unix
- In this case, go through the documentation for your Linux distribution to learn how to get the package right.
- If you are compiling a C++ program, use “g++” instead of “gcc.”
- For example, if the source code file “main.c” is located at /usr/wikiHow/source, type cd /usr/wikiHow/source .
- If you see an error message and want to see more information about the problem, use the command gcc -Wall -o errorlog file1.c . Then, continue viewing the “errorlog” file in the current directory with the command cat errorlog .
- To compile a program from multiple source files, you use the command gcc -o outputfile file1.c file2.c file3.c .
- To compile multiple programs with multiple source files at the same time, use the command gcc -c file1.c file2.c file3.c .
By MinGW on Windows
- If the file doesn’t open on its own, double-click the file in the download folder and then click Install.
- MinGW recommends users to use the default installation directory ( C:MinGW ). If you have to change, you should also not use a folder with a name with spaces (for example, “Program Files”). [3] X Research Sources
- With minimal need, you can select Basic Setup in the left pane, then check next to all the compilers listed in the main right pane.
- More advanced users can choose All Packages and additional compilers.
- Press ⊞ Win + S to launch the search bar, then type environment .
- Click Edit the system environment variables in the search results that appear.
- Click Environment Variables
- Click Edit below the top pane (below the “User Variables” line).
- Scroll to the bottom of the “Variable Value” box.
- Enter ;C:MinGWbin after the last word in the box. Note: if you have installed MinGW in another directory, enter the command ;C:path-to-that-directorybin , remember to replace “path-to-that-directory” with the path to the actual directory.
- Click OK twice. Click the remaining OK button to close the window.
- Press ⊞ Win + S and type cmd .
- Right-click Command Prompt in the search results and select Run As Administrator .
- Click Yes to allow the changes.
- For example, if the source file is called helloworld.c and is located at C:SourcePrograms, type cd C:SourcePrograms .
- Any programming errors that appear should be fixed before compiling the program.
Advice
- Building code with the (flag) -g flag will generate debug information that the respective GDB debugger can use for better performance.
- Makefiles can be created to make compiling large programs easier.
- If you apply a lot of optimization, be cautious because optimizing for speed can be a trade-off with size, sometimes program accuracy, and vice versa.
- When compiling a C++ program, you can use G++ in the same way as you would with GCC. Note: C++ files will have the extension .cpp instead of .c.
Things you need
- Computer running Linux or Windows operating system
- Basic knowledge of GNU/Linux and how to install applications.
- Program code
- Text editor (e.g. Emacs, vi)
This article is co-authored by a team of editors and trained researchers who confirm the accuracy and completeness of the article.
The wikiHow Content Management team carefully monitors the work of editors to ensure that every article is up to a high standard of quality.
This article has been viewed 16,454 times.
This wikiHow teaches you how to compile a C program from source code using GNU (full name is GNU Compiler Cplection, abbreviated GCC) – a compiler for Linux and Minimalist Gnu (MinGW) on Linux. Windows.
Thank you for reading this post How to Compile C Programs with GNU (GCC) at Lassho.edu.vn You can comment, see more related articles below and hope to help you with interesting information.
Related Search: