Linux Hello word!

Source: Internet
Author: User

Create a text file: Hello. cpp in the GCC folder of the root directory.

The content is as follows:

# Include <stdio. h>

Int main (void)
{
Printf ("Hello world! \ N ");
Return 0;
}

Application> attachment> terminal:

[Root @ localhost ~] # Cd gcc

Execute the compilation command:

[Root @ localhost GCC] # gcc-C hello. cpp-O hello

Run:

[Root @ localhost GCC] #./Hello
Bash:./Hello: insufficient Permissions

View permissions:

[Root @ localhost GCC] # ls-l

Total 16
-RW ------- 1 Root 77 10-30 16:33 hello. cpp
-RW ------- 1 Root 77 10-30 33 new file ~

Regenerate a generation extension (the extension is not important to Linux)

[Root @ localhost GCC] # gcc-C hello. cpp-O hello. o

[Root @ localhost GCC] #./Hello. o

Bash:./Hello. O: insufficient Permissions

Modify permissions for hello. O:

[Root @ localhost GCC] # chmod 777 hello. o

Execute again:

[Root @ localhost GCC] #./Hello. o

Bash:./Hello. O: cannot execute binary file

Output compilation details:

[Root @ localhost GCC] # gcc-V hello. cpp

Use built-in specs.
Goals: i386-redhat-linux
Configuration :.. /configure -- prefix =/usr -- Mandir =/usr/share/man -- infodir =/usr/share/info -- enable-shared -- enable-threads = POSIX -- enable-checking = release -- With-system-zlib -- enable-_ cxa_atexit -- disable-libunwind-exceptions -- enable-libgcj-Multifile -- enable-versions ages = C, c ++, objc, obj-C ++, Java, Fortran, ADA
-- Enable-Java-AWT = GTK -- disable-DSSI -- enable-plugin -- With-Java-home =/usr/lib/JVM/java-1.4.2-gcj-1.4.2.0/JRE -- With-CPU = generic -- host = i386-redhat-linux
Thread model: POSIX
GCC version 4.1.2 20080704 (Red Hat 4.1.2-48)
/Usr/libexec/GCC/i386-redhat-linux/4.1.2/cc1plus-quiet-v-d_gnu_source hello. CPP-quiet-dumpbase hello. CPP-mtune = generic-auxbase hello-version-O/tmp/ccp3falx. s
Ignore the nonexistent directory "/usr/lib/GCC/i386-redhat-linux/4.1.2/.../../i386-redhat-linux/include"
# Include "..." search starts from here:
# Include <...> search starts from here:
/Usr/lib/GCC/i386-redhat-linux/4.1.2/.../../include/C ++/4.1.2
/Usr/lib/GCC/i386-redhat-linux/4.1.2/.../../include/C ++/4.1.2/i386-redhat-linux
/Usr/lib/GCC/i386-redhat-linux/4.1.2/.../../include/C ++/4.1.2/backward
/Usr/local/include
/Usr/lib/GCC/i386-redhat-linux/4.1.2/include
/Usr/include
The search list ends.
Gnu c ++ version 4.1.2 20080704 (Red Hat 4.1.2-48) (i386-redhat-linux)
Compiled by gnu c version 4.1.2 20080704 (Red Hat 4.1.2-48.
GGC criterion: -- Param GGC-Min-expand = 98 -- Param GGC-Min-heapsize = 128356
Compiler executable checksum: e1c6e7e253b2b6b8ac8ef94463a3ee70
As-v-QY-O/tmp/ipv5uleo. O/tmp/ccp3falx. s
GNU extends er version 2.17.50.0.6-14. EL5 (i386-redhat-linux) using BFD version 2.17.50.0.6-14. EL5 20061020
/Usr/libexec/GCC/i386-redhat-linux/4.1.2/collect2 -- Eh-frame-HDR-M elf_i386 -- hash-style = GNU-dynamic-linker/lib/ld-linux.so.2/usr/lib/ gcc/i386-redhat-linux/4.1.2 /.. /.. /.. /crt1.o/usr/lib/GCC/i386-redhat-linux/4.1.2 /.. /.. /.. /crti. o/usr/lib/GCC/i386-redhat-linux/4.1.2/crtbegin. O
-L/usr/lib/GCC/i386-redhat-linux/4.1.2-L/usr/lib/GCC/i386-redhat-linux/4.1.2-L/usr/lib/GCC/i386-redhat-linux/4.1.2 /.. /.. /.. /tmp/ipv5uleo. o-lgcc -- as-needed-lgcc_s -- no-as-needed-LC-lgcc -- as-needed-lgcc_s -- no-as-needed/usr/lib/GCC/i386-redhat-linux/ 4.1.2/crtend. O
/Usr/lib/GCC/i386-redhat-linux/4.1.2/.../crtn. o
/Tmp/ipv5uleo. O :(. eh_frame + 0x11): Undefined reference to '_ gxx_personality_v0'

Collect2: LD returns 1

In the above output: Undefined reference to '_ gxx_personality_v0' is an error message!

I searched the internet and found the answer:

--------------------------------------------
7 floor excaliburxk

[Reply] [Reference]

[Report]
GCC determines how to process a file by its suffix.
GCC treats. C as a C-language source code file
Treat. C or. cc as C ++ source code
By default,. cpp files are not supported. You can change the file suffix or use the option to specify C ++ File Processing in the compilation command line.
Yankai0219, 6th Floor

[Reply] [Reference]

[Report]
The problem was not solved last night. I found it in the morning because
Because the extension name of the source code file does not match the syntax of the Code, for example, for a file using the C syntax, select a suffix name such as CPP (or <uppercase C> )". Solution: Change the suffix of. cpp to. C to solve the problem.

--------------------------------------------

Change hello. cpp to hello. C and recompile it:

[Root @ localhost GCC] # gcc-V hello. C-O hello. o

Use built-in specs.
Goals: i386-redhat-linux
Configuration :.. /configure -- prefix =/usr -- Mandir =/usr/share/man -- infodir =/usr/share/info -- enable-shared -- enable-threads = POSIX -- enable-checking = release -- With-system-zlib -- enable-_ cxa_atexit -- disable-libunwind-exceptions -- enable-libgcj-Multifile -- enable-versions ages = C, c ++, objc, obj-C ++, Java, Fortran, ADA
-- Enable-Java-AWT = GTK -- disable-DSSI -- enable-plugin -- With-Java-home =/usr/lib/JVM/java-1.4.2-gcj-1.4.2.0/JRE -- With-CPU = generic -- host = i386-redhat-linux
Thread model: POSIX
GCC version 4.1.2 20080704 (Red Hat 4.1.2-48)
/Usr/libexec/GCC/i386-redhat-linux/4.1.2/PC3-quiet-V hello. c-quiet-dumpbase hello. c-mtune = generic-auxbase hello-version-O/tmp/ccfqdbub. s
Ignore the nonexistent directory "/usr/lib/GCC/i386-redhat-linux/4.1.2/.../../i386-redhat-linux/include"
# Include "..." search starts from here:
# Include <...> search starts from here:
/Usr/local/include
/Usr/lib/GCC/i386-redhat-linux/4.1.2/include
/Usr/include
The search list ends.
Gnu c 4.1.2 20080704 (Red Hat 4.1.2-48) (i386-redhat-linux)
Compiled by gnu c version 4.1.2 20080704 (Red Hat 4.1.2-48.
GGC criterion: -- Param GGC-Min-expand = 98 -- Param GGC-Min-heapsize = 128356
Compiler executable checksum: 2da9a5b9b7fcbb90f7b2463be5d65b32
As-v-QY-O/tmp/ccedgpge. O/tmp/ccfqdbub. s
GNU extends er version 2.17.50.0.6-14. EL5 (i386-redhat-linux) using BFD version 2.17.50.0.6-14. EL5 20061020
/Usr/libexec/GCC/i386-redhat-linux/4.1.2/collect2 -- Eh-frame-HDR-M elf_i386 -- hash-style = GNU-dynamic-linker/lib/ld-linux.so.2-O hello. o/usr/lib/GCC/i386-redhat-linux/4.1.2 /.. /.. /.. /crt1.o/usr/lib/GCC/i386-redhat-linux/4.1.2 /.. /.. /.. /crti. o/usr/lib/GCC/i386-redhat-linux/4.1.2/crtbegin. O
-L/usr/lib/GCC/i386-redhat-linux/4.1.2-L/usr/lib/GCC/i386-redhat-linux/4.1.2-L/usr/lib/GCC/i386-redhat-linux/4.1.2 /.. /.. /.. /tmp/ccedgpge. o-lgcc -- as-needed-lgcc_s -- no-as-needed-LC-lgcc -- as-needed-lgcc_s -- no-as-needed/usr/lib/GCC/i386-redhat-linux/ 4.1.2/crtend. O
/Usr/lib/GCC/i386-redhat-linux/4.1.2/.../crtn. o

The above content does not have error information,

So run:

[Root @ localhost GCC] #./Hello. o

Hello world!

Finally, I came out! Amitabha!

[Root @ localhost GCC] #

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.