1. 程式人生 > >從無到有搭建Ubuntu下的編譯環境和製作debian包

從無到有搭建Ubuntu下的編譯環境和製作debian包

1.        環境準備:安裝 gcc, autoconf, automake, libtool, gettext, dh-makedebhelper等包;

2.        /home/workroom/test目錄下寫一個簡單的測試程式test.c:

  #include <stdio.h>

  void main()

   {

      printf("Hello world!\r\n");

   }

3.        /home/workroom/test目錄下執行 autoscan產生基本的 configure.scan檔案,開啟該檔案,並新增紅色的幾行,然後另存為configure.ac

#                                                -*- Autoconf -*-

# Process this file with autoconf to  produce a configure script.

AC_PREREQ([2.68])

AC_INIT([FULL-PACKAGE-NAME], [VERSION],  [BUG-REPORT-ADDRESS])

AC_CONFIG_SRCDIR([test.c])

AM_INIT_AUTOMAKE([-Wall  -Werror foreign])

AC_CONFIG_HEADERS([config.h])

# Checks for programs.

AC_PROG_CC

# Checks for libraries.

# Checks for header files.

# Checks for typedefs, structures, and  compiler characteristics.

# Checks for library functions.

AC_CONFIG_FILES([

Makefile

])

AC_OUTPUT

4.        手工生成一個autogen.sh檔案,內容如下:

#! /bin/sh

# Licensed to the Apache Software  Foundation (ASF) under one or more

# contributor license  agreements.  See the NOTICE file  distributed with

# this work for additional  information regarding copyright ownership.

# The ASF licenses this file to You under the Apache License, Version 2.0

# (the "License"); you  may not use this file except in compliance with

# the License.  You may obtain a copy of the License at

#

# Regenerate the files autoconf /  automake

rm -f configure

rm -f config.cache

rm -f config.log

aclocal -I .

autoheader

autoconf

automake -a -c

5.        手工生成Makefile.am

sbin_PROGRAMS=testapp

testapp_SOURCES= test.c

6.        執行./autogen.sh生成configure檔案

7.        執行./configure生成Makefile檔案

8.        執行 make編譯

[email protected]:/home/workroom/test#make

至此,第一階段程式的自動編譯環境已經完成,可以看到在當前目錄下已經生成了 testapp可執行程式。執行 ./testapp會輸出“Hello world!”

下面繼續完成將當前程式打成deb包。

9.        在當前目錄下新建一個 debian目錄,並進入該目錄建立 control, rules, changlog,copyright,compat等幾個必要的檔案:

[email protected]:/home/workroom/test# mkdir debian

[email protected]:/home/workroom/test# cd debian

[email protected]:/home/workroom/test# touch control

輸入內容:

Source: testapp

Section: unknown

Priority: extra

Maintainer: aaa

Build-Depends: debhelper (>=  8.0.0), autotools-dev

Standards-Version: 3.9.2

Homepage: http://www.test.com

Package: testapp

Architecture: any

Depends: ${shlibs:Depends},  ${misc:Depends}

Description: testapp

 testapp

[email protected]:/home/workroom/test# touch copyright

輸入內容:

Format:  http://dep.debian.net/deps/dep5

Upstream-Name: testapp

Source: <url://example.com>

Files: *

Copyright: <years> <put  author's name and email here>

           <years> <likewise for  another author>

License: <special license>

 <Put the license of the package here  indented by 1 space>

 <This follows the format of Description:  lines in control file>

 .

 <Including paragraphs>

[email protected]:/home/workroom/test# touch changelog

輸入內容:

testapp (1.0.0-1) stable;  urgency=low

  * Initial release (Closes: #nnnn)  <nnnn is the bug number of your ITP>

 -- aaa <[email protected]>  Thu, 28 Jun 2012 13:27:14 +0800

[email protected]:/home/workroom/test# touch compat

輸入內容:

8

[email protected]:/home/workroom/test# touch rules

輸入內容:

#!/usr/bin/make -f

# testApp debian/rules using  debhelper

# Copyright (C) 2012 aaa <[email protected]>

CXXFLAGS += -g -O2

CFLAGS += -g -O2

build-arch: build

build-indep: build

build: build-stamp

build-stamp: build-test-stamp

         touch  build-stamp

build-test-stamp:

         mkdir  build_test

         ls  |grep -v debian|grep -v build_test|xargs -i cp -r {} build_test/

         cd  build_test && ./autogen.sh && ./configure

         $(MAKE)  -C build_test

         touch  build-test-stamp

clean: clean-patched

clean-patched:

         dh_testdir

         dh_testroot

         [  ! -d $(CURDIR)/build_test ] || rm -rf $(CURDIR)/build_test

         dh_clean 

install: install-stamp

install-stamp: build-stamp

         dh_testdir

         dh_testroot

         dh_prep

         dh_installdirs

         $(MAKE) -C $(CURDIR)/build_test install DESTDIR=$(CURDIR)/debian/tmp

         dh_install  --sourcedir=debian/tmp

         touch  install-stamp

binary-arch: build install

         dh_testdir

         dh_testroot

         dh_install  --list-missing

         dh_installchangelogs

         dh_installdocs 

         dh_installexamples

         dh_installman

         dh_python2  --no-guessing-versions

         dh_link

         dh_strip

         dh_compress

         dh_fixperms

         dh_makeshlibs

         dh_installdeb

         dh_shlibdeps

         dh_gencontrol

         dh_md5sums

         dh_builddeb

# Build architecture-independent  files here.

binary-indep: build install

# We have nothing to do by  default.

binary: binary-indep binary-arch

.PHONY: build clean binary-indep  binary-arch binary install

10.    因為我們編譯出來的程式名叫 testapp,為了把它做進deb包,還需要建立一個testapp.install檔案:

[email protected]:/home/workroom/test# touch testapp.install

/usr/local/bin/testapp

至此,為製作deb包的所有環節已經準備好,在當前目錄下執行:

[email protected]:/home/workroom/test# ./debian/rules binary

12.    dpkg -i命令安裝這個deb:

[email protected]:/home/workroom#dpkg –i testapp_1.0.0-1.i386.deb

安裝完後執行 testapp:

[email protected]:/home/workroom# testapp

Hello world!

以上從無到有,介紹了在linux Ubuntu下基本的編譯環境和debian包生成的基本步驟和框架,當然如果工程複雜的話,需要在上述步驟中新增相應的依賴包等,但總體的框架和步驟是一樣的,更詳細的說明可以參考:http://www.debian.org/doc/manuals/maint-guide/index.en.html

相關推薦

no