当前位置:网站首页>GCC compiling dynamic and static libraries

GCC compiling dynamic and static libraries

2022-06-27 12:43:00 xiongsiyu979

gcc Common options and dynamic library static library

One 、gcc summary

  • compiler (Compiler) hold Source code is converted to other lower level code ( For example, binary code 、 Machine code ), But it won't be executed .
  • Interpreter (Interpreter) Will read the source code , And generate instructions directly Let the computer hardware execute , No other code will be output .
  • gcc It started as C Linguistic compiler , But later the function was expanded , Become able to support compiling more languages , for example C++、Java、Go、Objective -C etc. , So later gcc It was renamed GNU Compiler Suite (GNU Compiler Collection Compiler collection ).
  • gcc The compilation process is based on File suffix To call different compilers
  • .c There are four steps to generate an executable file from a file precompile 、 assembly 、 compile 、 link

Two 、gcc Common options

test.c

#include <stdio.h>

int main(int argc, char *argv[])
{
     
 	printf("Hello world!\n");

    return 0;
}
  1. -E : precompile gcc -E test.c

    Execute to precompile , Replace the code text . Output precompiled results to standard output ( It's usually a monitor )

 Insert picture description here

  1. -S : assembly gcc -S test.c

    Execution to assembly , Source code to assembly code conversion , Output assembly code (.s file )

 Insert picture description here

  1. -c : compile gcc -c test.c

    Execute to compile , Output target file (.o file )

 Insert picture description here
 Insert picture description here

  1. -o : change gcc Output file name

If you want to use it gcc Generate executable files , Can directly gcc test.o Generate executable files a.out,gcc test.o -o test You can rename the generated executable a.out Designated for us test( from .o The process from file to executable file is actually the process of linking ) Insert picture description here

  1. -shared And -fPIC: Create a dynamic library
    -fPIC:(Position-Independent Code) It works on Compile phase , Tell the compiler to generate Location independent code (.o file )

    -shared: Will be multiple .o The file is linked into a .so Dynamic library files

​ Will now print ”Hello world!“ The function of is encapsulated into a function , And will hello.c Make dynamic library and static library respectively , stay test.c Of main() In the static library or dynamic library hello() To print “Hello world!”

hello.h

#ifndef _HELLO_H_ // A defensive statement 
#define _HELLO_H_

extern int hello();

#endif

hello.c

#include <stdio.h>

int hello()
{
     
	printf("Hello world!\n");

	return 0;
}

test.c

#include <stdio.h>
#include "hello.h"

int main(int argc, char *argv[])
{
     
	hello();

return 0;
}

 Insert picture description here

  1. -I( uppercase i): towards gcc Add header file search path to
     Insert picture description here
  2. -l( Lowercase L): Specify the library to link
  3. -L: towards gcc China Canada stock in path

 Insert picture description here
Dynamic links require... At run time export Introduce the dynamic library path to the lookup path to link , Static links don't need
 Insert picture description here
9. -static: Link static libraries

 Insert picture description here  Insert picture description here

  1. -w: Turn off the warning
  2. -Wall: Turn on all warnings
  3. -g: Generate object code with debug information
原网站

版权声明
本文为[xiongsiyu979]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/178/202206271217131116.html