GithubHelp home page GithubHelp logo

c_compilation's Introduction

Context

I made this as a cheat sheet to showcase different style of compilation technics. The program is made of:

  • src/imagegd.c: contains a function to resize an image. As a depedency, this program rely on another lirary called gd
  • main.c: our main function that essentially benchmark our fast our imagegd is

Normal compilation

make clean
gcc -Wall -c main.c
gcc -Wall -c src/imagegd.c
gcc -o main.bin imagegd.o main.o -lgd
./main.bin
    1. compile all our C code into object
    1. link our objects together + dynamically link the gd library to our code

Pro: as simple as possible, not unecessary stuff Cons: program won't start is gd isn't install in the user's machine

Library: Shared

make clean
gcc -Wall -c main.c
gcc -Wall -fPIC -c src/imagegd.c
gcc -shared -o libimage.so imagegd.o
gcc -o main.bin main.o -lgd -L. -limage
LD_LIBRARY_PATH=`pwd` ./main.bin
    1. Compile all our C code into object
    1. Create a shared library
    1. link it all together

Pro: not much, remember this is a cheat sheet :) Cons: Same as previous + we need to specify the path where the program will find our library

Library: Shared and dynamic

make clean
gcc -Wall -c main.c
gcc -Wall -c src/imagegd.c
ar -rc libimage.a imagegd.o
ranlib libimage.a
gcc -o main.bin main.o -lgd -L. -limage
./main.bin
    1. Compile all our C code into object
    1. Create a static libary from our code
    1. link it all together by statically link our library and dynamically link the dependencies of our static library

As a static library (.a)

make clean
gcc -Wall -c src/imagegd.c
ar x /usr/local/lib/libgd.a
ar -rc libimage.a *.o
ranlib libimage.a
gcc -Wall -c main.c
gcc -o main.bin main.o -lm -ljpeg -L. -limage
./main.bin
    1. Compile all our C code into objects
    1. Extract the objects from the libraries we depend on and want to link statically
    1. Create a static library from all those objects
    1. Link all of it together with our dependencies include in our static library.

At this state we still dynamically link against some dependencies of our dependencies but we can extract all those as well and repeat the process to get a 100% static build

Tips

# See symbols in a shared / static library:
nm -D /usr/local/lib/libvips.so
nm -C /usr/local/lib/libvips.a

# See the object within a library:
ar -t libimage.a

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.