File processing and makefile writing of C language programming library under Linux

Source: Internet
Author: User

After 3 years of development, it is commonplace to compile and install software under Linux. Take GCC, there are not more than 10 times, but basically every time you will encounter some weird problems. It seems that Ides like VS and codeblocks make people stupid. Then make up your mind to learn how to compile the C project in pure manual gcc under Linux. Today I learned 2 points, one is the library file processing, the other is makefile writing.

Learn the system is centos6.6, compile and upgrade the gcc4.8.2, tomorrow to write a blog summary of this back to the GCC installation process, every time can learn something.

The compilation process of GCC

The first thing you need to know is what GCC compiles.

source Files ----preprocessing----> preprocessing Files (*.i)----Compiling----> assembly Files (*.S)----compiling----> target files (*.O) ----LINK----> executable file

Basically similar to Windows. By adding some options to GCC, you can control the compilation work to a specified stage, and try some of the common GCC compilation options below

    • -C compile, assemble source file, do not link, get *.o file
    • -S compiles only, does not assemble, gets *.s file
    • -e preprocessing file, get *. E file
    • -o [dis] [src] compile the src file into an executable file dis
    • -idir specifying include contains file search paths
    • -G generates a debug message, and if this option is not added, the. Debug segment is not generated in the *.O file, and the print variable value cannot be viewed during debugging

Both C and C + + know that a C or C + + program has a wide variety of library files, which are already compiled binaries that contain data and execute code. Windows is a DLL file, and Linux has. A and. so files. If you need to use these library files, in the IDE environment to check the option to do things, in the case of manual compilation of the trouble point.

GCC Create and use static libraries to write STATIC_LIB.C files

Create a static library
1 gcc -c static_lib.c2ar RCS STATIC_LIB.A STATIC_LIB.O

The above command will produce STATIC_LIB.A static library files in the current directory.

Using linked static libraries

Writing static_lib.h files

Writing main3.c files, using methods from the static library

Compiling main3.c and linking static library files

Perform

1 gcc main3.c-lstatic_lib.a-o app3

However, the linker LD could not find the library problem, the-l parameter removed is normal

1 gcc main3.c static_lib.a-o app3

Finally, the executable file app3 is generated. The static library is characterized by putting the code in the library into the execution file, if you modify the static library code, to recompile the dependent on it to execute the file to upgrade

GCC Create and use dynamic libraries

A dynamic library is a library file that is dynamically loaded into execution when there are executable files that need to use the library.

Writing SHARE_LIB.C files

Create a dynamic library

Because of the need for location-independent, so you need to use the-FPIC option, there are thousands of GCC options, you need to query an option can be man gcc and then find the view

1 gcc -shared-fpic-o share_lib.so SHARE_LIB.C

The generated share_lib.so file is a dynamic library file that is dynamically loaded when used by a program that uses the library and is not written to another executable file, so when the library file is modified, there is no need to recompile other programs that use the library.

Working with Dynamic libraries

Share_lib.h File Declaration function

Write main4.c file, include "Share_lib.h" file

Compiling main4.c and linking dynamic libraries

1 gcc main4.c./share_lib.so-o APP4

The generated APP4 is the executable file.

Writing makefile Compilation

Place the 3 source files of the previous static library main3.c,static_lib.c,static_lib.h into one directory.

Write the makefile file.

Where the colon to the left represents the target file or command, the command is also called a pseudo-target.

Execute make

Do clean to clear the compiled file

Of course, this is probably the simplest makefile, makefile still have a lot of learning, I also in the study, after the harvest will continue to write blog records, for later to see the great God write makefile don't understand a little bit

File processing and makefile writing of C language programming library under Linux

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.