SlideShare ist ein Scribd-Unternehmen logo
1 von 9
Downloaden Sie, um offline zu lesen
Biên dịch với GNU gcc, make



Phạm Nguyên Khang, Đỗ Thanh Nghị
Email: pnkhang,dtnghi@cit.ctu.edu.vn
Biên dịch

   Ví dụ đơn giản trong ngôn ngữ C
       Soạn thảo 2 files: main.c và func.c




                                              2
Biên dịch

   Sử dụng gcc [options] sources
    các tùy chọn (options)
     -o: sinh ra tập tin output

     -c: sinh ra tập tin đối tượng .o

     -I: đặc tả thư mục chứa tập tin include

     -l: đặc tả tên thư viện

     -L: đặc tả đường dẫn đến thư viện




                                                3
Biên dịch

   Sử dụng gcc để dịch ra file đối tượng (.o)
       Gõ: gcc –c main.c (sinh ra main.o)
       Gõ: gcc –c func.c (sinh ra func.o)
   Sử dụng gcc để dịch ra file thực thi từ các file đối tượng
       Gõ: gcc –o main main.o func.o (sinh ra chtrình main)




                                                               4
Biên dịch

   Dịch với make và Makefile để dịch tự động các bước trên
     Soạn thảo Makefile
     Makefile là 1 file đặc biệt dùng để quản lý các tập tin trong dự án

     Chứa các quy tắc biên dịch

     Sử dụng thuộc tính “modified time” để biên dịch lại

     Tạo ra 1 đồ thị phụ thuộc

     Cú pháp:

    <target>: <danh sách các file phụ thuộc>
    <TAB> lệnh

    Chú ý phải dùng phím <TAB> chứ không phải khoảng trắng
                                                                     5
Biên dịch

   Dịch với make và Makefile để dịch tự động các bước trên
       Soạn thảo Makefile như sau




       Gõ: make
       Lệnh make sẽ đọc các bước dịch trong Makefile để dịch và sinh
        ra chtrình main


                                                                  6
Biên dịch
   Thư viện liên kết
       Mặc định gcc có thể liên kết với các file đối tượng
       Gõ: gcc –o main main.c func.o
       Tĩnh: chứa những file đối tượng .o được tạo bởi công cụ ar
       Gõ: ar rcs libfunc.a func.o (tạo ra thư viện libfunc.a)
       Dịch main với: gcc –o main main.c libfunc.a
       Động: được load khi chtrình chạy, được tạo bởi gcc
       Gõ: gcc –c –fPIC func.c (tạo ra func.o)
       Gõ: gcc –shared –W1,soname,libfunc.so.1 –o libfunc.so.1.0
        func.o
       Tạo ra libfunc.so.1.0 (libfunc.dll)
       Dịch main với: gcc –o main main.c libfunc.so.1.0
                                                                     7
Biên dịch

   Thư viện liên kết động
       Chạy chtrình main: có lỗi không tìm thấy thư viện liên kết động
       Hoặc chỉ lại đường dẫn đến thư viện
       gõ: export LD_LIBRARY_PATH=$(pwd)
       Hoặc sao chép libfunc.so.1.0 đến /usr/lib rồi
       chạy ldconfig để cập nhật LD




                                                                     8
Devel

Weitere ähnliche Inhalte

Was ist angesagt?

Was ist angesagt? (12)

Carte ref-ubuntu-vi
Carte ref-ubuntu-viCarte ref-ubuntu-vi
Carte ref-ubuntu-vi
 
10 compiling kernel
10  compiling kernel10  compiling kernel
10 compiling kernel
 
Linux+03
Linux+03Linux+03
Linux+03
 
Python
PythonPython
Python
 
Linux+02
Linux+02Linux+02
Linux+02
 
03 installing linux as a server
03  installing linux as a server03  installing linux as a server
03 installing linux as a server
 
Linux và mã nguồn mở
Linux và mã nguồn mởLinux và mã nguồn mở
Linux và mã nguồn mở
 
06 the command line
06  the command line06  the command line
06 the command line
 
Linux+01
Linux+01Linux+01
Linux+01
 
Lesson 6 - Administering Linux System (2)
Lesson 6 - Administering Linux System (2)Lesson 6 - Administering Linux System (2)
Lesson 6 - Administering Linux System (2)
 
Quan tri he dieu hanh linux
Quan tri he dieu hanh linuxQuan tri he dieu hanh linux
Quan tri he dieu hanh linux
 
Session 21
Session 21Session 21
Session 21
 

Andere mochten auch

Open data INPS
Open data INPS Open data INPS
Open data INPS DatiGovIT
 
Credit Card Product Update: 2014 Overview
Credit Card Product Update: 2014 OverviewCredit Card Product Update: 2014 Overview
Credit Card Product Update: 2014 OverviewCorporate Insight
 
Translation Technologies & Business in the Future
Translation Technologies & Business in the FutureTranslation Technologies & Business in the Future
Translation Technologies & Business in the FutureMultilizer
 
Innovations in Materials Science: Albany Engineered Composites
Innovations in Materials Science: Albany Engineered CompositesInnovations in Materials Science: Albany Engineered Composites
Innovations in Materials Science: Albany Engineered CompositesUNHInnovation
 
Outsourcing your SharePoint Hosting: The Cloud's Fine Print Magnified
Outsourcing your SharePoint Hosting: The Cloud's Fine Print MagnifiedOutsourcing your SharePoint Hosting: The Cloud's Fine Print Magnified
Outsourcing your SharePoint Hosting: The Cloud's Fine Print MagnifiedSherWeb
 
Climate faith feb_15
Climate  faith  feb_15Climate  faith  feb_15
Climate faith feb_15Wayne Irwin
 
Optimal health products
Optimal health productsOptimal health products
Optimal health productsDyna Smith
 
השועל והחסידה
השועל והחסידההשועל והחסידה
השועל והחסידהahmadeg1
 
Ppt -optimal_healthusa
Ppt  -optimal_healthusaPpt  -optimal_healthusa
Ppt -optimal_healthusaDyna Smith
 
Waterproofing contractor in spartanburg
Waterproofing contractor in spartanburgWaterproofing contractor in spartanburg
Waterproofing contractor in spartanburgDyna Smith
 
COLÔMBIA EDUCA
COLÔMBIA EDUCACOLÔMBIA EDUCA
COLÔMBIA EDUCAblog2012
 
Parambariyam
ParambariyamParambariyam
ParambariyamSano Anil
 

Andere mochten auch (19)

Open data INPS
Open data INPS Open data INPS
Open data INPS
 
Credit Card Product Update: 2014 Overview
Credit Card Product Update: 2014 OverviewCredit Card Product Update: 2014 Overview
Credit Card Product Update: 2014 Overview
 
Lecture10
Lecture10Lecture10
Lecture10
 
Translation Technologies & Business in the Future
Translation Technologies & Business in the FutureTranslation Technologies & Business in the Future
Translation Technologies & Business in the Future
 
Innovations in Materials Science: Albany Engineered Composites
Innovations in Materials Science: Albany Engineered CompositesInnovations in Materials Science: Albany Engineered Composites
Innovations in Materials Science: Albany Engineered Composites
 
Outsourcing your SharePoint Hosting: The Cloud's Fine Print Magnified
Outsourcing your SharePoint Hosting: The Cloud's Fine Print MagnifiedOutsourcing your SharePoint Hosting: The Cloud's Fine Print Magnified
Outsourcing your SharePoint Hosting: The Cloud's Fine Print Magnified
 
Chc6b0c6a1ng 12
Chc6b0c6a1ng 12Chc6b0c6a1ng 12
Chc6b0c6a1ng 12
 
Climate faith feb_15
Climate  faith  feb_15Climate  faith  feb_15
Climate faith feb_15
 
Dominios
DominiosDominios
Dominios
 
Fragance
FraganceFragance
Fragance
 
Sombong
SombongSombong
Sombong
 
Optimal health products
Optimal health productsOptimal health products
Optimal health products
 
השועל והחסידה
השועל והחסידההשועל והחסידה
השועל והחסידה
 
Ppt -optimal_healthusa
Ppt  -optimal_healthusaPpt  -optimal_healthusa
Ppt -optimal_healthusa
 
Waterproofing contractor in spartanburg
Waterproofing contractor in spartanburgWaterproofing contractor in spartanburg
Waterproofing contractor in spartanburg
 
COLÔMBIA EDUCA
COLÔMBIA EDUCACOLÔMBIA EDUCA
COLÔMBIA EDUCA
 
Waves
WavesWaves
Waves
 
Imagina
ImaginaImagina
Imagina
 
Parambariyam
ParambariyamParambariyam
Parambariyam
 

Ähnlich wie Devel

[C] giao trinh c dhbk - viet nhat
[C] giao trinh c   dhbk - viet nhat[C] giao trinh c   dhbk - viet nhat
[C] giao trinh c dhbk - viet nhatHoang Nguyen
 
Reverse Engineering .NET - Advanced Patching, Playing with IL
Reverse Engineering .NET - Advanced Patching, Playing with ILReverse Engineering .NET - Advanced Patching, Playing with IL
Reverse Engineering .NET - Advanced Patching, Playing with ILLevis Nickaster
 
Lập trình C# 2008 cơ bản_Nhất Nghệ
Lập trình C# 2008 cơ bản_Nhất NghệLập trình C# 2008 cơ bản_Nhất Nghệ
Lập trình C# 2008 cơ bản_Nhất NghệTrần Thiên Đại
 
Phan2 chuong1 gioithieu_c
Phan2 chuong1 gioithieu_cPhan2 chuong1 gioithieu_c
Phan2 chuong1 gioithieu_cLy hai
 
Tìm hiểu về hệ điều hành android
Tìm hiểu về hệ điều hành androidTìm hiểu về hệ điều hành android
Tìm hiểu về hệ điều hành androidTÓc Đỏ XuÂn
 
Oop unit 11 input và output
Oop unit 11 input và outputOop unit 11 input và output
Oop unit 11 input và outputTráng Hà Viết
 
Hướng dẫn lập trình web với PHP
Hướng dẫn lập trình web với PHPHướng dẫn lập trình web với PHP
Hướng dẫn lập trình web với PHPNguyễn Tuấn Quỳnh
 
Linux Network Administration (LPI-1,LPI-2)
Linux Network Administration (LPI-1,LPI-2)Linux Network Administration (LPI-1,LPI-2)
Linux Network Administration (LPI-1,LPI-2)laonap166
 
Importing othermso files_vi-draft1
Importing othermso files_vi-draft1Importing othermso files_vi-draft1
Importing othermso files_vi-draft1Vu Hung Nguyen
 
Sinh vienit.net --57669587-c-dhkh-hue
Sinh vienit.net --57669587-c-dhkh-hueSinh vienit.net --57669587-c-dhkh-hue
Sinh vienit.net --57669587-c-dhkh-hueTuấn Nguyễn Văn
 
Phan 2 chuong 1-2
Phan 2   chuong 1-2Phan 2   chuong 1-2
Phan 2 chuong 1-2ngoclanh12
 

Ähnlich wie Devel (20)

Os lab2
Os lab2Os lab2
Os lab2
 
[C] giao trinh c dhbk - viet nhat
[C] giao trinh c   dhbk - viet nhat[C] giao trinh c   dhbk - viet nhat
[C] giao trinh c dhbk - viet nhat
 
Reverse Engineering .NET - Advanced Patching, Playing with IL
Reverse Engineering .NET - Advanced Patching, Playing with ILReverse Engineering .NET - Advanced Patching, Playing with IL
Reverse Engineering .NET - Advanced Patching, Playing with IL
 
1 -nhapmon
1  -nhapmon1  -nhapmon
1 -nhapmon
 
Hdsd eclipse
Hdsd eclipseHdsd eclipse
Hdsd eclipse
 
Linux04 hethongtaptin
Linux04 hethongtaptinLinux04 hethongtaptin
Linux04 hethongtaptin
 
Lập trình C# 2008 cơ bản_Nhất Nghệ
Lập trình C# 2008 cơ bản_Nhất NghệLập trình C# 2008 cơ bản_Nhất Nghệ
Lập trình C# 2008 cơ bản_Nhất Nghệ
 
Phan2 chuong1 gioithieu_c
Phan2 chuong1 gioithieu_cPhan2 chuong1 gioithieu_c
Phan2 chuong1 gioithieu_c
 
File
FileFile
File
 
Sigil
SigilSigil
Sigil
 
Web201 slide 1
Web201   slide 1Web201   slide 1
Web201 slide 1
 
Tìm hiểu về hệ điều hành android
Tìm hiểu về hệ điều hành androidTìm hiểu về hệ điều hành android
Tìm hiểu về hệ điều hành android
 
Oop unit 11 input và output
Oop unit 11 input và outputOop unit 11 input và output
Oop unit 11 input và output
 
Sigil
SigilSigil
Sigil
 
Hướng dẫn lập trình web với PHP
Hướng dẫn lập trình web với PHPHướng dẫn lập trình web với PHP
Hướng dẫn lập trình web với PHP
 
Linux Network Administration (LPI-1,LPI-2)
Linux Network Administration (LPI-1,LPI-2)Linux Network Administration (LPI-1,LPI-2)
Linux Network Administration (LPI-1,LPI-2)
 
Importing othermso files_vi-draft1
Importing othermso files_vi-draft1Importing othermso files_vi-draft1
Importing othermso files_vi-draft1
 
Sinh vienit.net --57669587-c-dhkh-hue
Sinh vienit.net --57669587-c-dhkh-hueSinh vienit.net --57669587-c-dhkh-hue
Sinh vienit.net --57669587-c-dhkh-hue
 
Phan 2 chuong 1-2
Phan 2   chuong 1-2Phan 2   chuong 1-2
Phan 2 chuong 1-2
 
thu ha
thu hathu ha
thu ha
 

Mehr von vantinhkhuc (20)

Url programming
Url programmingUrl programming
Url programming
 
Servlets intro
Servlets introServlets intro
Servlets intro
 
Servlet sessions
Servlet sessionsServlet sessions
Servlet sessions
 
Security overview
Security overviewSecurity overview
Security overview
 
Rmi
RmiRmi
Rmi
 
Md5
Md5Md5
Md5
 
Lecture17
Lecture17Lecture17
Lecture17
 
Lecture11 b
Lecture11 bLecture11 b
Lecture11 b
 
Lecture9
Lecture9Lecture9
Lecture9
 
Lecture6
Lecture6Lecture6
Lecture6
 
Jsse
JsseJsse
Jsse
 
Jsf intro
Jsf introJsf intro
Jsf intro
 
Jsp examples
Jsp examplesJsp examples
Jsp examples
 
Jpa
JpaJpa
Jpa
 
Ejb examples
Ejb examplesEjb examples
Ejb examples
 
Corba
CorbaCorba
Corba
 
Ajax
AjaxAjax
Ajax
 
Ejb intro
Ejb introEjb intro
Ejb intro
 
Ch06
Ch06Ch06
Ch06
 
Bai4
Bai4Bai4
Bai4
 

Devel

  • 1. Biên dịch với GNU gcc, make Phạm Nguyên Khang, Đỗ Thanh Nghị Email: pnkhang,dtnghi@cit.ctu.edu.vn
  • 2. Biên dịch  Ví dụ đơn giản trong ngôn ngữ C  Soạn thảo 2 files: main.c và func.c 2
  • 3. Biên dịch  Sử dụng gcc [options] sources các tùy chọn (options)  -o: sinh ra tập tin output  -c: sinh ra tập tin đối tượng .o  -I: đặc tả thư mục chứa tập tin include  -l: đặc tả tên thư viện  -L: đặc tả đường dẫn đến thư viện 3
  • 4. Biên dịch  Sử dụng gcc để dịch ra file đối tượng (.o)  Gõ: gcc –c main.c (sinh ra main.o)  Gõ: gcc –c func.c (sinh ra func.o)  Sử dụng gcc để dịch ra file thực thi từ các file đối tượng  Gõ: gcc –o main main.o func.o (sinh ra chtrình main) 4
  • 5. Biên dịch  Dịch với make và Makefile để dịch tự động các bước trên  Soạn thảo Makefile  Makefile là 1 file đặc biệt dùng để quản lý các tập tin trong dự án  Chứa các quy tắc biên dịch  Sử dụng thuộc tính “modified time” để biên dịch lại  Tạo ra 1 đồ thị phụ thuộc  Cú pháp: <target>: <danh sách các file phụ thuộc> <TAB> lệnh Chú ý phải dùng phím <TAB> chứ không phải khoảng trắng 5
  • 6. Biên dịch  Dịch với make và Makefile để dịch tự động các bước trên  Soạn thảo Makefile như sau  Gõ: make  Lệnh make sẽ đọc các bước dịch trong Makefile để dịch và sinh ra chtrình main 6
  • 7. Biên dịch  Thư viện liên kết  Mặc định gcc có thể liên kết với các file đối tượng  Gõ: gcc –o main main.c func.o  Tĩnh: chứa những file đối tượng .o được tạo bởi công cụ ar  Gõ: ar rcs libfunc.a func.o (tạo ra thư viện libfunc.a)  Dịch main với: gcc –o main main.c libfunc.a  Động: được load khi chtrình chạy, được tạo bởi gcc  Gõ: gcc –c –fPIC func.c (tạo ra func.o)  Gõ: gcc –shared –W1,soname,libfunc.so.1 –o libfunc.so.1.0 func.o  Tạo ra libfunc.so.1.0 (libfunc.dll)  Dịch main với: gcc –o main main.c libfunc.so.1.0 7
  • 8. Biên dịch  Thư viện liên kết động  Chạy chtrình main: có lỗi không tìm thấy thư viện liên kết động  Hoặc chỉ lại đường dẫn đến thư viện  gõ: export LD_LIBRARY_PATH=$(pwd)  Hoặc sao chép libfunc.so.1.0 đến /usr/lib rồi  chạy ldconfig để cập nhật LD 8