How to compile and run C programs under Linux

Source: Internet
Author: User

Synthesis From: http://www.cnblogs.com/javadotnet/articles/1918930.html

http://blog.csdn.net/shijinupc/article/details/6789554

CC is a very important source code compilation tool under the Linux operating system, with many important options to support the compilation of many different languages, such as C, C + +, Ada, Fortran, Objective, Perl, Python, Ruby and Java, etc. Even Linux's many kernels and many other free software and open source applications are written in C and compiled by GCC.

1. Compiling a single source file

[For example] print "Hello,linux." On the screen.

[Source code]

#include <stdio.h>

#include <stdlib.h>

int main (int argc,char **argv)

{

printf ("hello,linux.\n");

Exit (0);

}

Save the source file as hello.c and start compiling

$GCC-O Hello hello.c

After the compilation completes successfully, under the current path, generate a file named Hello, and then execute

$./hello

On the screen, you will see the results of the printing: Hello,linux.

Note: By default, after the compilation completes successfully, a file named A.out is generated under the current path, and then the $./a.out can be printed, but you can usually specify your own executable name by using the option-O.

2. Compiling multiple source files

3. Using an external function library

4. Using shared function libraries and static function libraries

5.GCC Options Detailed description


main options for 1.7.2 Gcc/egcs
table 1-3 Common options for GCC commands
option Explanation
-ansi only supports the ANSI standard C syntax. This option will prohibit certain features of GNU C,
such as ASM or typeof keywords.
-C compiles and generates only the target file.
-DMACRO defines macro macros with the string "1".
-DMACRO=DEFN defines macro macros with the string "DEFN".
-E only runs the C precompiled compiler.
-G generates debug information. The GNU debugger can take advantage of this information.
-idirectory Specifies additional header file search paths for directory.
-ldirectory Specifies additional library search paths for directory.
-llibrary searches for the specified library of libraries when connected.
-m486 code optimization for 486.
-o file to generate the specified output files. Used when generating an executable file.
-o0 is not optimized for processing.
-O or-o1 optimization generates code.
-o2 further optimization.
-o3 is further optimized than-O2, including the inline function.
-shared generate a shared destination file. Typically used when building a shared library.
-static prohibit the use of shared connections.
-umacro cancels the definition of macro macros.
-W does not generate any warning messages.
-wall generates all warning messages.

How to compile and run C programs under Linux

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.