How to compile a C + + program in gcc++ and run

Source: Internet
Author: User
GCC compiles three languages: C, C + +, and object C (an object-oriented extension of the C language). Use the GCC command to compile and connect both C and C + + source programs at the same time.
#DEMO #: HELLO.C

If you have two or a few C source files, you can also easily compile, connect, and build executables using GCC. For example, suppose you have two source files Main.c and factorial.c two source files, now you want to compile a program that generates a factorial calculation.

-----------------------
List FACTORIAL.C
-----------------------
#include <stdio.h>
#include <stdlib.h>

int factorial (int n)
{
if (n <= 1)
return 1;

Else
return factorial (n-1) * n;
}
-----------------------

-----------------------
List MAIN.C
-----------------------
#include <stdio.h>
#include <stdlib.h>

int factorial (int n);

int main (int argc, char **argv)
{
int n;

if (ARGC < 2) {
printf ("Usage:%s n/n", argv [0]);
return-1;
}
else {
n = atoi (argv[1]);
printf ("Factorial of%d is%d./n", N, factorial (n));
}

return 0;
}
-----------------------

Use the following command to compile the build executable file and execute the program:
$ gcc-o factorial main.c factorial.c
$/factorial 5
Factorial of 5 is 120.

GCC can also be used to compile C programs and C + + programs. Generally speaking, C compiler by the source file suffix name to determine whether the C program or C + + program. In Linux, the C source file has a suffix named. C, and the C + + source file has a suffix named. C or. cpp.

However, the GCC command can only compile C + + source files and not automatically connect to libraries used by C + + programs. Therefore, the g++ command is typically used to compile and connect C + + programs, which automatically invoke GCC implementation compilation. Let's say we have a C + + source file like the following (hello. C):

#include <iostream.h>

void Main (void)
{
cout << "Hello, world!" << Endl;
}

You can compile, connect, and build an executable file by calling the g++ command as follows:

$ g++-o Hello hello. C
$./hello
Hello, world!.

Main options for 1.7.2 Gcc/egcs
Table 1-3 Common options for GCC commands
Options 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 files.
-dmacro defines MACRO macros with the string "1".
-DMACRO=DEFN defines MACRO macros as a string "DEFN".
-e runs only the C precompiled compiler.
-G to generate debugging information. The GNU debugger can take advantage of this information.
-idirectory Specifies the additional header file search path directory.
-LDIRECTORY specifies an additional function library search path directory.
Searches for a specified library of functions when-llibrary a connection.
-m486 code optimization for 486.
The-o file generates the specified output file. Used when generating an executable file.
-o0 is not optimized for processing.
-O or-o1 optimization generates code.
-o2 further optimized.
-o3 is further optimized than-O2, including the inline function.
-shared generates a shared destination file. Typically used when building a shared library.
-static prohibit the use of shared connections.
-umacro cancels the definition of a MACRO macro.
-W does not generate any warning messages.
-wall generates all warning messages.


Related Article

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.