个人工具

“UbuntuHelp:CompilingSoftware”的版本间的差异

来自Ubuntu中文

跳转至: 导航, 搜索
第1行: 第1行:
 
{{From|https://help.ubuntu.com/community/CompilingSoftware}}
 
{{From|https://help.ubuntu.com/community/CompilingSoftware}}
 
{{Languages|UbuntuHelp:CompilingSoftware}}
 
{{Languages|UbuntuHelp:CompilingSoftware}}
 
 
 
The Ubuntu repositories contain thousands of packages and with 3rd party repositories you can get even more. However, sometimes you might want to compile packages from source in the following cases.  
 
The Ubuntu repositories contain thousands of packages and with 3rd party repositories you can get even more. However, sometimes you might want to compile packages from source in the following cases.  
 
* Package is not available in the repositories  
 
* Package is not available in the repositories  
第10行: 第8行:
 
* You want to test a patch to help a developer fixing a bug
 
* You want to test a patch to help a developer fixing a bug
 
* You want to try your hand at compiling programs from scratch.  
 
* You want to try your hand at compiling programs from scratch.  
 
 
== Background ==
 
== Background ==
 
You might need to compile software depending on the language in which the program is written. Applications which require compiling are usually written in ''C'' and ''C++''. If this is the case, you will need to install a compiler '''gcc''' which can be obtained by installing the '''build-essential''' package. Normally, you can do this by typing the following in a terminal:
 
You might need to compile software depending on the language in which the program is written. Applications which require compiling are usually written in ''C'' and ''C++''. If this is the case, you will need to install a compiler '''gcc''' which can be obtained by installing the '''build-essential''' package. Normally, you can do this by typing the following in a terminal:
第16行: 第13行:
 
sudo apt-get install build-essential
 
sudo apt-get install build-essential
 
</nowiki></pre>
 
</nowiki></pre>
 
 
If the program is written in Java, you need a java run-time and compiler. You can install java by following the instructions on the [wiki.ubuntu.com/RestrictedFormats Java] page. Sometimes, a java based build tool '''ant''' is required. This package is available from the Ubuntu repositories.
 
If the program is written in Java, you need a java run-time and compiler. You can install java by following the instructions on the [wiki.ubuntu.com/RestrictedFormats Java] page. Sometimes, a java based build tool '''ant''' is required. This package is available from the Ubuntu repositories.
 
 
If the program is written in an interpreted language like python or perl, you simply need the interpreter, which are installed by Ubuntu by default. You should be able to run the program directly, and in most cases you can install the additional python or perl programs required to run the package from the Ubuntu Repositories. Please see the note at the end of this article about interpreted languages.   
 
If the program is written in an interpreted language like python or perl, you simply need the interpreter, which are installed by Ubuntu by default. You should be able to run the program directly, and in most cases you can install the additional python or perl programs required to run the package from the Ubuntu Repositories. Please see the note at the end of this article about interpreted languages.   
 
 
=== Do you really need to compile? ===
 
=== Do you really need to compile? ===
 
If you are attempting to compile packages because you think the package is not available in the repository, please search for packages at the [http://packages.ubuntu.com/ Ubuntu Package Search], and also look at the 3rd party repositories listed at http://ubuntulinux.nl/source-o-matic. You can also search for packages available to you from the command line by issuing following command:  
 
If you are attempting to compile packages because you think the package is not available in the repository, please search for packages at the [http://packages.ubuntu.com/ Ubuntu Package Search], and also look at the 3rd party repositories listed at http://ubuntulinux.nl/source-o-matic. You can also search for packages available to you from the command line by issuing following command:  
 
<code><nowiki>apt-cache search <a keyword from the package></nowiki></code>  
 
<code><nowiki>apt-cache search <a keyword from the package></nowiki></code>  
 
 
=== Obtaining the Sources ===
 
=== Obtaining the Sources ===
 
Generally, the source packages will be available from the author's website in an archived format. Common archive formats are ''tar.gz'', ''tar.bz2'' and ''zip''. You can unarchive them by using the following commands.  
 
Generally, the source packages will be available from the author's website in an archived format. Common archive formats are ''tar.gz'', ''tar.bz2'' and ''zip''. You can unarchive them by using the following commands.  
第32行: 第25行:
 
unzip file.zip
 
unzip file.zip
 
</nowiki></pre>
 
</nowiki></pre>
 
 
Sometimes, you may also need to obtain cutting edge code using CVS. You will need to install the '''cvs''' utility by issuing a <code><nowiki>sudo apt-get install cvs</nowiki></code>. For example, to retrieve the NetworkManager sources, issue the following commands.  
 
Sometimes, you may also need to obtain cutting edge code using CVS. You will need to install the '''cvs''' utility by issuing a <code><nowiki>sudo apt-get install cvs</nowiki></code>. For example, to retrieve the NetworkManager sources, issue the following commands.  
 
<pre><nowiki>
 
<pre><nowiki>
第39行: 第31行:
 
</nowiki></pre>
 
</nowiki></pre>
 
If you are installing from cvs, please see the note at the end of this article.  
 
If you are installing from cvs, please see the note at the end of this article.  
 
 
== Three Stages to Compiling Packages ==
 
== Three Stages to Compiling Packages ==
 
 
Most programs that need to be installed from source on Linux can be installed by using the ''./configure; make; make install'' steps. The most difficult step is usually the ''./configure'' step, after which the remaining steps should be easy to complete.   
 
Most programs that need to be installed from source on Linux can be installed by using the ''./configure; make; make install'' steps. The most difficult step is usually the ''./configure'' step, after which the remaining steps should be easy to complete.   
 
 
=== configure ===
 
=== configure ===
 
<code><nowiki>configure</nowiki></code> is a script that is used for the following things:
 
<code><nowiki>configure</nowiki></code> is a script that is used for the following things:
第50行: 第39行:
 
* It is used to enable/disable various options in the compiled program
 
* It is used to enable/disable various options in the compiled program
 
* It can change the path that the program will be installed in
 
* It can change the path that the program will be installed in
 
 
You can look at the various options that your specific configure script provides you with by executing:
 
You can look at the various options that your specific configure script provides you with by executing:
 
<pre><nowiki>
 
<pre><nowiki>
 
./configure --help | less
 
./configure --help | less
 
</nowiki></pre>
 
</nowiki></pre>
 
 
For example, by default the configure script installs to ''/usr/local''. If you want to change the path, you would execute the configure script as shown below:  
 
For example, by default the configure script installs to ''/usr/local''. If you want to change the path, you would execute the configure script as shown below:  
 
<pre><nowiki>
 
<pre><nowiki>
 
./configure --PREFIX=/opt
 
./configure --PREFIX=/opt
 
</nowiki></pre>
 
</nowiki></pre>
 
 
If you are really compiling from scratch, always read through the README and INSTALL files. You will see which pieces of software it needs, usually these are libraries and they usually have a different name in these files than in the packaging system. Then proceed to look for the package using apt-cache tool. Another trick to use if you are compiling a package already in the repositories is to install the build dependencies of the package using the command below:
 
If you are really compiling from scratch, always read through the README and INSTALL files. You will see which pieces of software it needs, usually these are libraries and they usually have a different name in these files than in the packaging system. Then proceed to look for the package using apt-cache tool. Another trick to use if you are compiling a package already in the repositories is to install the build dependencies of the package using the command below:
 
<pre><nowiki>
 
<pre><nowiki>
第66行: 第52行:
 
</nowiki></pre>
 
</nowiki></pre>
 
This will ensure that you have all the dependencies of the package are installed, and hopefully the configure script will not complain about your system having old version of the dependencies installed, in which case you will have to compile the dependencies also.  
 
This will ensure that you have all the dependencies of the package are installed, and hopefully the configure script will not complain about your system having old version of the dependencies installed, in which case you will have to compile the dependencies also.  
 
 
==== Troubleshooting the configure ====
 
==== Troubleshooting the configure ====
 
 
Even after following the above steps, the configure steps might fail.  
 
Even after following the above steps, the configure steps might fail.  
 
* You always need the <code><nowiki>-dev</nowiki></code> package when compiling.
 
* You always need the <code><nowiki>-dev</nowiki></code> package when compiling.
第82行: 第66行:
 
* Try compiling the software disabling the feature which is failing using the ''./configure --disable-FEATURE'' option.  
 
* Try compiling the software disabling the feature which is failing using the ''./configure --disable-FEATURE'' option.  
 
* If you are unable to troubleshoot on your own, you can always ask for help in #ubuntu
 
* If you are unable to troubleshoot on your own, you can always ask for help in #ubuntu
 
 
If no ''configure'' file exists at all, you might want to check for the existence of a ''configure.ac'' file (although you should, first of all, read the INSTALL and README files very carefully!).
 
If no ''configure'' file exists at all, you might want to check for the existence of a ''configure.ac'' file (although you should, first of all, read the INSTALL and README files very carefully!).
 
If ''configure.ac'' exists, chances are that the developer has forgot to create the final ''configure''. To create it yourself, you need the '''autoconf''' package:
 
If ''configure.ac'' exists, chances are that the developer has forgot to create the final ''configure''. To create it yourself, you need the '''autoconf''' package:
第93行: 第76行:
 
</nowiki></pre>
 
</nowiki></pre>
 
and, if you're lucky, a working ''configure'' will be created.
 
and, if you're lucky, a working ''configure'' will be created.
 
 
=== Compiling the Package ===
 
=== Compiling the Package ===
 
 
Hopefully, the configure stage should have been completed by now and the actual compiling can begin. This can be done simply be calling:
 
Hopefully, the configure stage should have been completed by now and the actual compiling can begin. This can be done simply be calling:
 
<pre><nowiki>
 
<pre><nowiki>
 
make
 
make
 
</nowiki></pre>
 
</nowiki></pre>
 
 
Now if all goes well you should have a working copy of the program in the sources directory. You should try it out by running it:
 
Now if all goes well you should have a working copy of the program in the sources directory. You should try it out by running it:
 
<pre><nowiki>
 
<pre><nowiki>
 
src/program_name
 
src/program_name
 
</nowiki></pre>
 
</nowiki></pre>
 
 
If the program fails at this stage, this is probably a bug in the program. You should contact the author, and notify the author of the errors.
 
If the program fails at this stage, this is probably a bug in the program. You should contact the author, and notify the author of the errors.
 
 
=== Installing the Package ===
 
=== Installing the Package ===
 
 
If everything works well and you want to install the program just type:
 
If everything works well and you want to install the program just type:
 
<pre><nowiki>
 
<pre><nowiki>
 
sudo make install
 
sudo make install
 
</nowiki></pre>
 
</nowiki></pre>
 
 
However, a better solution at this stage is to create a .deb file using CheckInstall, as it makes removing the package at a later stage very easy.  
 
However, a better solution at this stage is to create a .deb file using CheckInstall, as it makes removing the package at a later stage very easy.  
 
 
== Notes ==
 
== Notes ==
 
 
* If the program is a kernel module you will also need gcc-3.4 (on breezy) and the kernel-headers package. They latter can be installed with <code><nowiki>sudo apt-get install gcc-3.4 linux-headers-$(uname -r)</nowiki></code>. Please note that kernel modules have to be recompiled after each kernel upgrade (and new headers have to be downloaded too). For compiling a complete kernel, look at the KernelCompileHowto.  
 
* If the program is a kernel module you will also need gcc-3.4 (on breezy) and the kernel-headers package. They latter can be installed with <code><nowiki>sudo apt-get install gcc-3.4 linux-headers-$(uname -r)</nowiki></code>. Please note that kernel modules have to be recompiled after each kernel upgrade (and new headers have to be downloaded too). For compiling a complete kernel, look at the KernelCompileHowto.  
 
* If you are trying to recompile an existing debian package, use <code><nowiki>dpkg-buildpackage -rfakeroot</nowiki></code> and if any packages are missing, the build script will tell you the name of the package.
 
* If you are trying to recompile an existing debian package, use <code><nowiki>dpkg-buildpackage -rfakeroot</nowiki></code> and if any packages are missing, the build script will tell you the name of the package.
 
* If you are compiling from cvs, you will generally need to obtain the sources from cvs, and run another additional step of running the '''autogen.sh''' script to generate the configure script for you.  
 
* If you are compiling from cvs, you will generally need to obtain the sources from cvs, and run another additional step of running the '''autogen.sh''' script to generate the configure script for you.  
 
 
=== Interpreted Languages  ===
 
=== Interpreted Languages  ===
 
Usually interpreted languages will fail to run if a required module is not installed.  
 
Usually interpreted languages will fail to run if a required module is not installed.  
第134行: 第107行:
 
apt-cache search python | grep mysql
 
apt-cache search python | grep mysql
 
</nowiki></pre> usually helps in getting the required python module.  
 
</nowiki></pre> usually helps in getting the required python module.  
 
 
=== Comments ===
 
=== Comments ===
 
 
For a newbie, you may consider CompilingEasyHowTo more user-friendly.
 
For a newbie, you may consider CompilingEasyHowTo more user-friendly.
 
 
Also, You may find CheckInstall is helpful.
 
Also, You may find CheckInstall is helpful.
 
 
* Shouldn't checkinstall be the ''default'' method described in this documentation?  Is there any reason why someone new to compiling would not want to use it?
 
* Shouldn't checkinstall be the ''default'' method described in this documentation?  Is there any reason why someone new to compiling would not want to use it?
 
* Also, can't file-roller be used to unarchive the files?  No one should have to memorize "-jxvf".  Please recommend simpler easier-to-use tools whenever possible.  Advanced users can find the command line shortcuts on a million other documents on the web.  *This* document should just provide a clear method to get people started with an emphasis on Ubuntu and getting that obscure software package running easily.
 
* Also, can't file-roller be used to unarchive the files?  No one should have to memorize "-jxvf".  Please recommend simpler easier-to-use tools whenever possible.  Advanced users can find the command line shortcuts on a million other documents on the web.  *This* document should just provide a clear method to get people started with an emphasis on Ubuntu and getting that obscure software package running easily.
第146行: 第115行:
 
** Also adding a gui step when the rest of compilation is done in a shell complicates matters.
 
** Also adding a gui step when the rest of compilation is done in a shell complicates matters.
 
*** Is this guide for developers or users?
 
*** Is this guide for developers or users?
 
 
----
 
----
 
[[category:CategoryDocumentation]]
 
[[category:CategoryDocumentation]]
  
 
[[category:UbuntuHelp]]
 
[[category:UbuntuHelp]]

2007年11月30日 (五) 16:32的版本


The Ubuntu repositories contain thousands of packages and with 3rd party repositories you can get even more. However, sometimes you might want to compile packages from source in the following cases.

  • Package is not available in the repositories
  • Package in the repositories may be too old
  • Package in the repositories has a feature in program disabled due to some reasons
  • Package in the repositories may have a bug which has been fixed by the author of the package.
  • You want to test a patch to help a developer fixing a bug
  • You want to try your hand at compiling programs from scratch.

Background

You might need to compile software depending on the language in which the program is written. Applications which require compiling are usually written in C and C++. If this is the case, you will need to install a compiler gcc which can be obtained by installing the build-essential package. Normally, you can do this by typing the following in a terminal:

sudo apt-get install build-essential

If the program is written in Java, you need a java run-time and compiler. You can install java by following the instructions on the [wiki.ubuntu.com/RestrictedFormats Java] page. Sometimes, a java based build tool ant is required. This package is available from the Ubuntu repositories. If the program is written in an interpreted language like python or perl, you simply need the interpreter, which are installed by Ubuntu by default. You should be able to run the program directly, and in most cases you can install the additional python or perl programs required to run the package from the Ubuntu Repositories. Please see the note at the end of this article about interpreted languages.

Do you really need to compile?

If you are attempting to compile packages because you think the package is not available in the repository, please search for packages at the Ubuntu Package Search, and also look at the 3rd party repositories listed at http://ubuntulinux.nl/source-o-matic. You can also search for packages available to you from the command line by issuing following command: apt-cache search <a keyword from the package>

Obtaining the Sources

Generally, the source packages will be available from the author's website in an archived format. Common archive formats are tar.gz, tar.bz2 and zip. You can unarchive them by using the following commands.

tar -xvf file.tar.gz
tar -xvf file.tar.bz2
unzip file.zip

Sometimes, you may also need to obtain cutting edge code using CVS. You will need to install the cvs utility by issuing a sudo apt-get install cvs. For example, to retrieve the NetworkManager sources, issue the following commands.

cvs -d :pserver:anonymous@anoncvs.gnome.org:/cvs/gnome login
cvs -d :pserver:anonymous@anoncvs.gnome.org:/cvs/gnome co NetworkManager 

If you are installing from cvs, please see the note at the end of this article.

Three Stages to Compiling Packages

Most programs that need to be installed from source on Linux can be installed by using the ./configure; make; make install steps. The most difficult step is usually the ./configure step, after which the remaining steps should be easy to complete.

configure

configure is a script that is used for the following things:

  • It checks that your computer fulfills all the necessary requirements to build the package
  • It can help you change the default paths it looks for it's requirements
  • It is used to enable/disable various options in the compiled program
  • It can change the path that the program will be installed in

You can look at the various options that your specific configure script provides you with by executing:

./configure --help | less

For example, by default the configure script installs to /usr/local. If you want to change the path, you would execute the configure script as shown below:

./configure --PREFIX=/opt

If you are really compiling from scratch, always read through the README and INSTALL files. You will see which pieces of software it needs, usually these are libraries and they usually have a different name in these files than in the packaging system. Then proceed to look for the package using apt-cache tool. Another trick to use if you are compiling a package already in the repositories is to install the build dependencies of the package using the command below:

sudo apt-get build-dep <package>

This will ensure that you have all the dependencies of the package are installed, and hopefully the configure script will not complain about your system having old version of the dependencies installed, in which case you will have to compile the dependencies also.

Troubleshooting the configure

Even after following the above steps, the configure steps might fail.

  • You always need the -dev package when compiling.
  • For compiling GNOME applications, the package gnome-devel is usually required; for KDE applications, use kde-devel
  • The names of C and C++ libraries always start with lib, so if ./configure complains about missing library foo, you need the libfoo-dev package.
  • If a configure script asks for "X includes", you should probably install the following packages: xlibs-dev, xlibs-static-dev,x-window-system-dev
  • Another technique to determine which package to use is using the auto-apt tool or the apt-file tool (auto-apt might be faster).
sudo apt-get install auto-apt
sudo auto-apt update
auto-apt search missing-file.h
Using apt-file is identical to the above, if you substitute every occurrence of "auto-apt" with "apt-file".
  • Try compiling the software disabling the feature which is failing using the ./configure --disable-FEATURE option.
  • If you are unable to troubleshoot on your own, you can always ask for help in #ubuntu

If no configure file exists at all, you might want to check for the existence of a configure.ac file (although you should, first of all, read the INSTALL and README files very carefully!). If configure.ac exists, chances are that the developer has forgot to create the final configure. To create it yourself, you need the autoconf package:

sudo apt-get install autoconf

After it is installed, you can type

autoconf

and, if you're lucky, a working configure will be created.

Compiling the Package

Hopefully, the configure stage should have been completed by now and the actual compiling can begin. This can be done simply be calling:

make

Now if all goes well you should have a working copy of the program in the sources directory. You should try it out by running it:

src/program_name

If the program fails at this stage, this is probably a bug in the program. You should contact the author, and notify the author of the errors.

Installing the Package

If everything works well and you want to install the program just type:

sudo make install

However, a better solution at this stage is to create a .deb file using CheckInstall, as it makes removing the package at a later stage very easy.

Notes

  • If the program is a kernel module you will also need gcc-3.4 (on breezy) and the kernel-headers package. They latter can be installed with sudo apt-get install gcc-3.4 linux-headers-$(uname -r). Please note that kernel modules have to be recompiled after each kernel upgrade (and new headers have to be downloaded too). For compiling a complete kernel, look at the KernelCompileHowto.
  • If you are trying to recompile an existing debian package, use dpkg-buildpackage -rfakeroot and if any packages are missing, the build script will tell you the name of the package.
  • If you are compiling from cvs, you will generally need to obtain the sources from cvs, and run another additional step of running the autogen.sh script to generate the configure script for you.

Interpreted Languages

Usually interpreted languages will fail to run if a required module is not installed.

  • Perl modules are usually referenced as foo::bar (for example xml::parser). However, the package is called libfoo-bar-perl (libxml-parser-perl). Searching via apt-cache can help if this naming is a bit different, for example:
apt-cache search foo | grep bar
apt-cache search xml | grep parser
  • Python modules are named python-module, so the python mysql module would be python-mysqldb. The mapping is less consistent here and apt-cache will help a lot. Doing a search like
apt-cache search python | grep mysql
usually helps in getting the required python module.

Comments

For a newbie, you may consider CompilingEasyHowTo more user-friendly. Also, You may find CheckInstall is helpful.

  • Shouldn't checkinstall be the default method described in this documentation? Is there any reason why someone new to compiling would not want to use it?
  • Also, can't file-roller be used to unarchive the files? No one should have to memorize "-jxvf". Please recommend simpler easier-to-use tools whenever possible. Advanced users can find the command line shortcuts on a million other documents on the web. *This* document should just provide a clear method to get people started with an emphasis on Ubuntu and getting that obscure software package running easily.
    • Actually "tar xf file" is sufficient. GNU tar has (for some time) autodetected compression and worked around it. Possibly "tar xvf file" if the user wants verbose output.
    • Also adding a gui step when the rest of compilation is done in a shell complicates matters.
      • Is this guide for developers or users?