Contents
GCC Tools
apt show build-essential | grep Depends
# Depends: libc6-dev | libc-dev, gcc (>= 4:5.2), g++ (>= 4:5.2), make, dpkg-dev (>= 1.17.11)
man gcc
# gcc - GNU project C and C++ compiler
man g++
# gcc - GNU project C and C++ compiler
GNU是一个自由的操作系统 其内容软件完全以GPL方式发布 GNU核心软件包括gcc、emacs 后来GNU引入Linux以替代自己的内核 组合成了GNU/Linux
Object File
vim test.c
#include <stdio.h>
void hello(int i)
{
printf("hello %d\n", i);
}
int main(void)
{
static int s_v = 64;
int l_v = 10;
hello(s_v + l_v);
return 0;
}
gcc -c test.c
# gcc -c
# Compile or assemble the source files, but do not link. The linking stage simply is not done. The ultimate output is in the form of an object file for each source file
hexdump test.o
objdump -d test.o
readelf -a test.o
Executable File
gcc test.o -o test
./test
# hello 74
ls -lh test*
# -rwxrwxr-x 1 ubuntu ubuntu 8.5K Jun 29 16:57 test
# -rw-rw-r-- 1 ubuntu ubuntu 172 Jun 29 16:57 test.c
# -rw-rw-r-- 1 ubuntu ubuntu 1.7K Jun 29 16:57 test.o
objdump -d test
readelf -a test
Static Link Library
vim hello.h
# void hello(int i);
vim hello.c
#include <stdio.h>
void hello(int i)
{
printf("hello %d\n", i);
}
gcc -c hello.c
ar -rcs libhello.a hello.o
vim main.c
#include "hello.h"
int main(void)
{
static int s_v = 64;
int l_v = 10;
hello(s_v + l_v);
return 0;
}
gcc -o main_static main.c -static -L./ -lhello
./main_static
# hello 74
ar -t /usr/lib/x86_64-linux-gnu/libc.a | grep "^printf.o"
# printf.o
Dynamic Link Library
gcc -fPIC -shared hello.c -o libhello.so
gcc -o main_shared main.c -L./ -lhello
export LD_LIBRARY_PATH=/home/ubuntu
./main_shared
# hello 74
ls -hl main_*
# -rwxrwxr-x 1 ubuntu ubuntu 8.5K Jun 29 17:39 main_shared
# -rwxrwxr-x 1 ubuntu ubuntu 892K Jun 29 17:38 main_static
网友评论