CompilingEasyHowTo

来自Ubuntu中文
Oneleaf留言 | 贡献2007年5月30日 (三) 18:50的版本 (新页面: == Ubuntu 编译轻松上手 == 原文出处:https://wiki.ubuntu.com/CompilingEasyHowTo 原文作者:[https://wiki.ubuntu.com UbuntuWiki] 授权许可: * [http://creativecommons...)
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)
跳到导航跳到搜索

Ubuntu 编译轻松上手

原文出处:https://wiki.ubuntu.com/CompilingEasyHowTo

原文作者:UbuntuWiki

授权许可:

翻译人员:MillenniumDark

校正人员:Leal

贡献人员:

适用版本:所有版本 文章状态:翻译完成



Let's say you are a moderately experienced Linux user, and you want to install an application off the Internet but it doesn't have a nice package that works on your system. A lot of users, even quite experienced ones, have issues with going from the tarball to the installed program because they just don't know the fairly easy steps required to get the job done. But it's only easy if you already know how to do it! So, here's a quick guide how to install stuff from developer sites.

让我们假定你是一个具有一定经验的Linux使用者,并且你想从Internet上安装一个应用程序,但是没有能在你的系统上工作的不错的 软件包。大量用户,甚至具有相当经验的用户,从tarball(译者注:这里指用tar打包压缩的源码)到安装完毕的程序的过程中都会 遇到问题,仅仅是因为他们不了解完成这一任务的相当简单的几步。但是只有你已经知道如何去做,它才显得简单!所以,这里是一个 从开发者站点安装东西的快速指南。

Step 1: Prep your system for building packages

第一步:为制作软件包准备系统

By default, Ubuntu does not come with the tools required. You need to install the package build-essential to work with most of these packages. And since you may want to get code some projects with no released version, you should install the packages cvs and subversion as well if you think you'll need them. You should then build a common directory for yourself where you'll be building these packages. I recommend creating /usr/local/src, but really you can put it anywhere you want. Make sure this directory is writable by your primary user account, by running sudo chown username /usr/local/src and just to be safe sudo chmod u+rwx /usr/local/src. After you've done this, you're set up to start getting the programs you need.

默认情况下,Ubuntu并不预装需要的工具。你需要安装build-essential包,以便处理大部分的软件包。既然你可能想要得到一些不提 供发行版的项目的代码,你应该同时安装cvs和subversion包————如果你觉得你需要它们。接着你应该为你自己建立一个普通的目录, 你将在这个目录下制作软件包。我建议创建/usr/local/src,并运行sudo chown username /usr/local/src,以及 sudo chmod u+rwx /usr/local/src(后者只是出于安全考虑)。做完这个,你可以开始安装了,先设法获得你需要的程序(源码)。

Step 2: Getting the software you want

第二步:获得所需软件软件


Most of the software you'll generally want comes from released tarballs. These are just compressed archives in the form of .tar.gz or .tar.bz2 -- they are just like .zip files on Windows or .sit on MacOSX if that analogy helps you. If the program you want to install comes in this form, you should download it into the /usr/local/src directory we made in step one. If your tarballs is a .gz to extract the files you'll run tar xzvf tarballname.tar.gz and for bz2 the similar tar xjvf tarballname.tar.bz2 command.

In the rarer case of getting a program from a cvs or subversion repository, the developers will generally provide instructions on how to do this on their website. If you already installed the packages listed on step one, you just need to change to your /usr/local/src directory (cd /usr/local/src) and run the commands that are listed. The procedure will vary from program to program, so I can't help you here but with the given packages the instructions they prove should work smoothly.

大多数你通常需要的软件来自释放的tarball。它们只是一些压缩文档,通常的格式为.tar.gz或.tar.bz2————好比Windows上的.zip 文件和MacOSX上的.sit(如果这样的类比对你有帮助的话)。若你想装的软件的源码是这种格式的话,宜将其下载到我们在第一步 里建立的/usr/local/src目录。如果你的tarball是个.gz文件,运tar xzvf tarballname.tar.gz以解压缩,对.bz2文件,使用类似的tar xjvf tarballname.tar.bz2命令。

在罕见的情况下,是从cvs或者subversion仓库取得程序,一般开发者会在他们的网站上提供指导的。若已安装第一步列出的软件包,只需转到 /usr/local/src目录(cd /usr/local/src),运行列出的命令。具体的步骤因程序而异,所以这里我没法帮你,但是只要有上述软件包,开发者提供的 指导应该能顺利运行。

Step 3: Resolving Dependencies.

第三步:解决依赖性问题

One nice thing about modern Linux distributions is that they take care of dependencies for the user. That is to say, if you want to install a program, the apt-get program will make sure it installs all the libraries and other dependant programs when you do that, so installing a program is never more difficult that just specifying what you want and it does the rest. Unfortunately with tarballs this is not the case, and you'll have to do it manually. It's this stage that trips up even some fairly experienced users who often give up in frustration for not being able to figure out what they need to get.

To prepare, install the package apt-file, and then run sudo apt-file update. This will download a list of all the available packages and all of the files those packages contain, which as you might expect can be a very large list. The apt-file program has some interesting functions, the two most useful are apt-file search which searches for a particular filename, and apt-file list which lists all the files in a given package.

To check the dependencies of your program, change into the directory you created in step two (cd /usr/local/src). Extracting the tarball or downloading from cvs/subversion will have made a subdirectory under /usr/local/src that contains the source code. This newly-created directory will contain a file called "configure", which is a script to make sure that the program can be compiled on your computer. To run it, run the command ./configure See footnote [[FootNote(if you run ./configure without any options, you will use the default settings for the program. Most programs have a range of settings that you can enable or disable, if you are intersted in this check the README and INSTALL files, check the developer documentation and many cases ./configure --help will list some of the key configurations you can do. A very common options is to use ./configure --prefix=/usr which will install your application into /usr instead of /usr/local as my instructions do. )]] below. This command will check to see if you've got all the programs needed to install the program -- in most cases you will not, and it will error out with a message about needing a program.

If this happens, the last line of output will be something like configure: error: Library requirements (gobbletygook) not met, blah blah blah stuff we don't care about. But right above that it will list a filename that it cannot find, in my experience most cases will list a filename ending in ".pc". What you need to do then is to run apt-file search missingfilename.pc which will tell you which Ubuntu package the missing file is in. You can then simply install the package using sudo apt-get install requiredpackage. Then try running ./configure again, and see if it works. If you get to a bunch of text that finishes with config.status: creating Makefile followed by no obvious error messages, you're ready for the next steps.

现代Linux发行版有一个值得称道的地方,就是它们替用户处理依赖性问题。这就是说,如果你想安装程序,apt-get程序将确保在你安装的时候,安装 了所有需要的库以及其他依赖程序,安装一个程序再简单不过了,你永远只需要指定所要按照的程序,apt-get程序会完成剩下的工作。不幸的是, 处理tarball完全不是这回事,你得手工解决依赖性问题。正是这一阶段绊倒了一些相当有经验的使用者,他们常常由于无法搞清楚所需的依赖程序,而在 沮丧中放弃。

作为预备,安装apt-file包,接着运行sudo apt-file update。这将下载所有可用的软件包以及这些软件包所含的所有文件的列表,这可能是个大 家伙。apt-file有一些有趣的功能,最有用的两个是apt-file search(按文件名搜索)和apt-file list(列出给定的软件包中的所有文件)。

要检查你的出现的依赖性,转到你第二步里创建的目录(cd /usr/local/src)。解压缩tarball或者下载自cvs/subversion的文件,这会在 /usr/local/src下建立一个含有源码的子目录。这一新建目录会包含一个叫做"configure"的文件,那是一个确保程序可以在你的电脑上编译的脚本。 运行命令./configure来运行这个脚本[[FootNote(若不加任何参数运行 ./configure,你将使用程序的默认设置。大多数程序有一些你可以启用或禁用的设定,若对此感兴趣,察看README和INSTALL 文件,察看开发者的文档,许多情况下,./configure --help将列出一些可以修改的关键配置。一个非常常用的参数是./configure --prefix=/usr, 这将把你的应用程序安装到/usr,而不是如我的说明,装到/usr/local。)]]。这个命令会检查你是否有安装程序所需的所有程序————大多数情况下答案是否定的, 那它就会在运行中遇到错误,并提示需要某个程序。

如果上述情况发生了,输出的最后一行会类似下面的:configure: error: Library requirements (gobbletygook) not met,后面还有一些 我们不关心的废话。但就在上面它会列出找不到的文件名称,据我的经验,大多数情况下,会列出一个以".pc"结尾的文件。你接着要做的是运行 apt-file search 找不到的文件名称.pc,这会告诉你在哪个Ubuntu包里有缺少的文件。接下来,你只需sudo apt-get install 所需包安装 即可。接着再试试运行./configure,看看行不行。如果你得到一堆以config.status: creating Makefile结尾的文本,并且 config.status: creating Makefile后面明显没有错误信息,那你就可以进入一步了。

Step 4: Build and install.

第四步:制作和安装

If you got this far, you've done the hardest part already. Now all you need to do is run the command make, which does the actual building (compiling) of the program. If it's a large program or if you've got a very slow computer, go and get a cup of coffee or something. When you're done, run sudo make install and it will do the final stage of installation for you. When finished (it shouldn't take long), your application will be installed to /usr/local/bin and you should be able to run it without problems.

如果你已经到这里了,那你已经完成最艰难的部分了。现在你需要做的只是运行命令make,这会完成程序的实际制作(编译)。若程序很大或你的电脑非常 缓慢,去喝一杯咖啡之类的。完成后,运行sudo make install,它会为你完成最后阶段的安装。完成后(这该不会花多久),你的程序将被安装到 /usr/local/bin,你应该可以顺利地运行它。

Step 5: Postscript

附言

If this all seems way too hard for you, don't fret. You're using Ubuntu Linux after all, and it has all of the programs that you actually need to get your work done already packaged for you. If there isn't a package out there, the odds are that you really don't need the program and that within a few months someone will have packaged it for you anyhow. The only programs you actually need to build and compile like this are programs that are new and perhaps not yet stable or ready for your destkop -- if you think this procedure is too hard well maybe you ought to reconsider why you want to do this and just wait a few months for the next stable release.

If your desired package is quite important and you think it deserves to be in Ubuntu properly, perhaps contact the Masters of the Universe and see if they can do the hard work for you -- if they package something than anyone can install it without having to go through this procedure. But if you can get through all this, you're well on your way to becoming an expert Linux user -- you'd be surprised how easy all this seems after you've done it just a few times. Good luck!

Notes: Easy meaning "easier than tearing your hair out and then screaming about how much Linux sucks while running around the room". Not actually easy.

如果这些对你而言太难了,别为此烦恼。毕竟你用的是Ubuntu Linux,它已经把所有你实际需要的软件打包了。如果还没有软件包,它认为,除了一些特殊情形,你并不真的需要这个软件,而且不管怎样,几个月内有人会为你打包的。事实上,你需要这样制作和编译的程序仅仅是那些新的(也许现在还不稳定或者还没为你的 桌面准备好)————若嫌此编译过程过难,好,也许你该重新考虑一下为什么你想这样做,然后只要等几个月以后的下一个稳定的发行版。


若所求软件包非常重要,并且你认为它值得适当地出现在Ubuntu中,也许你可以联系 Masters of the Universe,看看他们能否为你做这项艰难 的工作———— 如果他们将某个软件打包了,那任何人就不用编译即可安装了。但是,如果你可以顺利完成上面的编译,在通向专家级Linux用户的道路上,你将走的更好——-在你编译了几次后,你会为所有的这一切如此容易而感到惊讶的。祝你好运!

注:容易指“比你扯下头发,一边绕着屋子跑一边尖叫Linux多么糟糕容易“。并不是真的很容易。