What if I change int main (void) of the C program to static int main (void?

Source: Internet
Author: User

For example, how does one change the main function int main (void) in the C program to static int main (void? For example, copy the Code # include <stdio. h> int main (void) {printf ("Hi \ n"); return 0;} copy the code to: copy the Code # include <stdio. h> static int main (void) {printf ("Hi \ n"); return 0;} copy the code first! ------------------ Split line ------------------- this problem was raised when I was reading the static keyword. As long as you understand the static keyword, the identifier will have the internal link (Internel Linkage) attribute, and you have learned about the compilation link process of the c program, you can get the answer-changing int main (void) in the C program to static int main (void) will cause the link to fail. Can verify: [zhanghaba @ Fedora code] $ gcc static_int_main.c/usr/lib/gcc/i686-redhat-linux/4.4.5 /.. /.. /.. /crt1.o: In function '_ start ':(. text + 0x18): undefined reference to 'main' collect2: ld returned 1 exit status what if it is changed to gcc-c? [Zhanghaba @ Fedora code] $ gcc-c static_int_main.c [zhanghaiba @ Fedora code] $ it can be seen that gcc-c can be compiled successfully, because gcc-c only has the preprocessing, compilation, and assembly phases, no link phase. First, let's take a look at the process of compiling links for C Programs in the GCC environment in Linux-compiling C Programs, it generally includes the C preprocessing phase, the C-to-assembly compilation phase, the compilation phase from the compilation to the target file, and the link phase of the target file. GCC supports the following commands, so that we can observe these stages: 1) print the overall compilation process during gcc-v GCC. c compilation and see which compilation tools are used. V indicates verbose (lengthy), that is, printing as much information as possible. 2) gcc-e gcc. c uses a pre-processor to process the source file and can be redirected to the GCC. I file to view 3) gcc-S GCC. c. Use the pre-processor and compiler to process the source file and automatically output GCC with the same name. s file 4) gcc-c GCC. c. process the source file with a Preprocessor, compiler, and assembler, and automatically output the same name. o File 5) gcc GCC. c. After the source file is processed by a Preprocessor, compiler, or assembler, use the linker to generate the default name. why is the default name of an out executable file. out? Because early compilation does not have the concept of a linker, a. out is directly generated by the assembler, and a indicates assembly. However, it should be clarified that a. out is generated by the linker in modern compilers. In addition, you can use option-save-temps to retain the files generated in the middle. The example is as follows: [zhanghaiba @ Fedora code] $ ls | grep hihi. chi. ihi. ohi. s. We will use gcc-v to observe the overall compilation process and copy the code [zhanghaiba @ Fedora code] $ gcc-v hi. cUsing built-in specs. target: i686-redhat-linuxConfigured :.. /configure -- prefix =/usr -- mandir =/usr/share/man -- infodir =/usr/share/info -- with-bugurl = http://bugzilla.redhat.com/bugzilla -- Enable-bootstrap -- enable-shared -- enable-threads = posix -- enable-checking = release -- with-system-zlib -- enable-_ cxa_atexit -- disable-libunwind-exceptions -- enable -gnu-unique-object -- enable-ages = c, c ++, objc, obj-c ++, java, fortran, ada -- enable-java-awt = gtk -- disable-dssi -- with-java-home =/usr/lib/jvm/java-1.5.0-gcj-1.5.0.0/jre -- enable-libgcj-multifile -- enable-java -maintainer-mode -- with-ecj-ja R =/usr/share/java/eclipse-ecj.jar -- disable-libjava-multilib -- with-ppl -- with-cloog -- with-tune = generic -- with-arch = i686 -- build = i686-redhat-linuxThread model: posixgcc version 4.4.5 20101112 (Red Hat 4.4.5-2) (GCC) COLLECT_GCC_OPTIONS = '-v''-mtune = generic ''-march = i686'/usr/libexec/gcc/i686-redhat-linux/4.4.5/PC3-quiet-v hi. c-quiet-dumpbase hi. c-mtune = generic-march = i686-auxbase hi-versi On-o/tmp/ccrwAICf. signoring nonexistent directory "/usr/lib/gcc/i686-redhat-linux/4.4.5/include-fixed" ignoring nonexistent directory "/usr/lib/gcc/i686-redhat-linux/4.4.5 /.. /.. /.. /.. /i686-redhat-linux/include "# include "... "search starts here: # include <...> search starts here:/usr/local/include/usr/lib/gcc/i686-redhat-linux/4.4.5/include/usr/includeEnd of search list. gnu c (GCC) version 4.4.5 20101112 (Red Hat 4.4.5-2) (i686-redhat-linux) compiled by gnu c version 4.4.5 20101112 (Red Hat 4.4.5-2), GMP version 4.3.1, MPFR version 2.4.2.GGC heuristics: -- param ggc-min-expand = 81 -- param ggc-min-heapsize = 95788 Compiler executable checksum: e892644090a9a7e8c330a388c518ddcollect_gcc_options = '-V''-mtune = generic ''-march = i686 'as-v-Qy-o/tmp/cc1w7Hxi. o/tmp/ccrwAICf. sGNU extends er versio N 2.20.51.0.2 (i686-redhat-linux) using BFD version 2.20.51.0.2-15. fc13 20091009COMPILER_PATH =/usr/libexec/gcc/i686-redhat-linux/4.4.5/:/usr/libexec/gcc/i686-redhat-linux/4.4.5/:/usr/libexec/gcc/i686-redhat-linux /: /usr/lib/gcc/i686-redhat-linux/4.4.5/:/usr/lib/gcc/i686-redhat-linux/:/usr/libexec/gcc/i686-redhat-linux/4.4.5 /: /usr/libexec/gcc/i686-redhat-linux/:/usr/lib/gcc/i686-redhat-linux. /4.4.5/:/usr/lib/gcc/i686-redhat-linux/LIBRARY_PATH =/usr/lib/gcc/i686-redhat-linux/4.4.5/:/usr/lib/gcc/i686-redhat-linux/4.4.5 /: /usr/lib/gcc/i686-redhat-linux/4.4.5 /.. /.. /.. /:/lib /: /usr/lib/COLLECT_GCC_OPTIONS = '-V''-mtune = generic ''-march = i686 '/usr/libexec/gcc/i686-redhat-linux/4.4.5/collect2 -- no-add- needed -- eh-frame-hdr -- build-id-m elf_i386 -- hash-style = gnu-dynamic-linker/lib/ld-linux.so. 2/usr/lib/gcc/i686-redhat-linux/4.4.5 /.. /.. /.. /crt1.o/usr/lib/gcc/i686-redhat-linux/4.4.5 /.. /.. /.. /crti. o/usr/lib/gcc/i686-redhat-linux/4.4.5/crtbegin. o-L/usr/lib/gcc/i686-redhat-linux/4.4.5-L/usr/lib/gcc/i686-redhat-linux/4.4.5-L/usr/lib/gcc/i686-redhat-linux/4.4.5 /.. /.. /.. /tmp/cc1w7Hxi. o-lgcc -- as-needed-lgcc_s -- no-as-needed-lc-lgcc -- as-needed-lgcc_s -- no-as-needed/usr/lib/gcc/i68 6-redhat-linux/4.4.5/crtend. o/usr/lib/gcc/i686-redhat-linux/4.4.5 /.. /.. /.. /crtn. o copy the code and pay attention to the red and bold parts (the red file is generated from the Green file) -- (1) the cc0 is the C compiler in the GCC compiling environment, which compiles the C code into the assembly code and outputs it. s file (2) as is the assembler. Compile the assembly code as the target file and output it. o file (3) collect2 is the linker (Environment) used by later GCC versions. In fact, it first calls the GNU linker ld to link the target file, and finally collects information related to program initialization, construct the initialization structure of the program. Ld is the real linker for the previous step. o target file and other requirements. o file or static Link Library. a file, dynamic link library. so file (for example, decompress the C standard library libc. obtain the required printf. o file), and the output link is. out file. Later GCC versions use collect2 as the linker. In fact, it indirectly calls the ld linker. In the tool used above, as is the compiler provided by GNU, and ld is the linker provided by GNU. It is the most important binary tool in GNU Binutils. Where the ld-linux.so.2 is a dynamic linker. Note that the-lc parameter, l indicates the link, and c indicates the Standard C library, that is, libc. a or libc. so. Let's go back to the problem itself -- main is not a keyword in C language, but it is a conventional main function name, but it is not the entry to program execution, the real entry of the C program is the global symbol _ start (a function implemented by the Assembly). The _ start function calls the library function _ libc_start_main, then _ libc_start_main calls the main function. We know that the declaration of the main function is in two forms: the Declaration of the main function (main symbol) in fact, we can use the nm tool in the target crt1.o file to check the symbolic copy code included in crt1.o [zhanghaba @ Fedora code] $ nm/usr/lib/crt1.o00000000 R _ IO_stdin_used00000004 D _ U _ libc_csu_fini U _ libc_csu_init U _ libc_start_main00000044 R _ fp_hw00000020 T _ start00000004 W data_start U main copy code crt1.o has the main symbol, but it is undefined (U), so we need to implement the main function (that is, to define the main symbol), and finally link it through the linker (this is called symbol parsing) if the main function is defined as static, that is, it has the Internel Linkage attribute, the compiled target file is a local symbol (visible to the current file) however, the link does not parse the symbols of the local symbols, but only according to the target file. rel. text to indicate the globally undefined symbols of the Link (that is, to modify the signed address of the REL object that can be relocated). Therefore, the main symbol cannot be found during the link, this causes the main symbol to be unable to find the specific implementation (Definition), cause the link to fail, we will then look at the feedback of the compilation failure/usr/lib/gcc/i686-redhat-linux/4.4.5 /.. /.. /.. /crt1.o: In function '_ start ':(. text + 0x18): undefined reference to 'main' collect2: ld returned 1 exit status is not difficult to understand -- in function _ start, reference the undefined Symbol main collect2 shell: the linker ld returns the 1 mark exit status (error status)

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.