装好的Ubuntu系统默认已自带gcc(C编译器),但不带g++。为了能编译C++程序,我们需要安装g++。在不使用Synaptic Package Manager的情况下,推荐如下2种方式安装:
1) 只安装单独的g++包。
arac@test:~/Downloads/cmake-3.1.0$ g++ --version
The program 'g++' can be found in the following packages:
* g++
* pentium-builder
Try: sudo apt-get install <selected package>
按这个提示执行命令则可: (注意:需要联网哦,因为apt-get...)
arac@test:~/Downloads/cmake-3.1.0$ sudo apt-get install g++
2) 安装build-essential包。
它是一个 meta package,包含了很多与编译有关的软件包。用命令 apt-cache depends "build-essential" 可以查看其依赖项:
Ubuntu论坛上http://packages.ubuntu.com/zh-cn/precise/build-essential,有说明每个包的功能:
如果你需要进行软件的编译,需要gcc/g++等,都需要用到这个。当然你也可以分别单独安装gcc/g++的软件包,用build-essential 只不过方便管理罢了!
安装方法:
arac@test:~/Downloads/cmake-3.1.0$ sudo apt-get install build-essential
出现的错误:
arac@test:~$ sudo apt-get install build-essential
Reading package lists... Done
Building dependency tree
Reading state information... Done
Package build-essential is not available, but is referred to by another package.
This may mean that the package is missing, has been obsoleted, or is only available from another source
E: Package build-essential has no installation candidate
试了:
arac@test:~/Downloads/cmake-3.1.0$ sudo apt-get update
arac@test:~/Downloads/cmake-3.1.0$ sudo apt-get install build-essential
依旧不行。Google了一下,说是需要清理package cache,可以这样解决:
This are a lot and clean of package cache
Open a Terminal from the menu Applications → Accessories → Terminal and type:
sudo dpkg --configure -a
sudo apt-get -f install
sudo apt-get --fix-missing install
sudo apt-get clean
sudo apt-get update
sudo apt-get upgrade
sudo apt-get dist-upgrade
sudo apt-get install build-essential
sudo apt-get clean
sudo apt-get autoremove
我照做了,但是第一步就无法进行:
arac@test:/$ sudo dpkg --configure -a
Setting up rtl8188eu-dkms (0+git20130406) ...
Loading new rtl8188eu-0+git20130406 DKMS files...
First Installation: checking all kernels...
Building only for 3.13.0-32-generic
Building initial module for 3.13.0-32-generic
Error! Bad return status for module build on kernel: 3.13.0-32-generic (x86_64)
Consult the make.log in the build directory
/var/lib/dkms/rtl8188eu/0+git20130406/build/ for more information.
dpkg: error processing rtl8188eu-dkms (--configure):
subprocess installed post-installation script returned error exit status 10
Errors were encountered while processing:
rtl8188eu-dkms
这个错误是涉及到我在这之前安装过的USB无线网卡驱动程序。打开build/下面的make.log文件:
DKMS make.log for rtl8188eu-0+git20130406 for kernel 3.13.0-32-generic (x86_64)
Wed Dec 24 19:36:53 CST 2014
make ARCH=x86_64 CROSS_COMPILE= -C /lib/modules/3.13.0-32-generic/build M=/var/lib/dkms/rtl8188eu/0+git20130406/build modules
make[1]: Entering directory `/usr/src/linux-headers-3.13.0-32-generic'
CC [M] /var/lib/dkms/rtl8188eu/0+git20130406/build/core/rtw_cmd.o
In file included from /var/lib/dkms/rtl8188eu/0+git20130406/build/core/rtw_cmd.c:23:0:
/var/lib/dkms/rtl8188eu/0+git20130406/build/include/osdep_service.h: In function ‘thread_enter’:
/var/lib/dkms/rtl8188eu/0+git20130406/build/include/osdep_service.h:1397:2: error: implicit declaration of function ‘daemonize’ [-Werror=implicit-function-declaration]
cc1: some warnings being treated as errors
make[2]: *** [/var/lib/dkms/rtl8188eu/0+git20130406/build/core/rtw_cmd.o] Error 1
make[1]: *** [_module_/var/lib/dkms/rtl8188eu/0+git20130406/build] Error 2
make[1]: Leaving directory `/usr/src/linux-headers-3.13.0-32-generic'
make: *** [modules] Error 2
这些完全就是之前我尝试安装网卡时不成功的尝试,留下的后遗症。我大概明白什么是package cache了,但是到它提示的路径下面/var/lib/dkms/直接删除了dkms文件夹,重试“sudo dpkg --configure -a”就会重新自动又生成出来/var/lib/dkms/,怎么才能清除?看到这篇http://oss.org.cn/html/47/n-67447.html关于Linux下卸载软件的方法,我尝试了:
arac@test:/var$ sudo dpkg -r dkms 这样就算卸载啦
(Reading database ... 144327 files and directories currently installed.)
Removing dkms ...
Processing triggers for man-db ...
arac@test:/var$ sudo dpkg -r rtl8188eu-dkms
(Reading database ... 144303 files and directories currently installed.)
Removing rtl8188eu-dkms ...
/var/lib/dpkg/info/rtl8188eu-dkms.prerm: 9: /var/lib/dpkg/info/rtl8188eu-dkms.prerm: dkms: not found
卸载后。重试:
arac@test:~/Downloads/cmake-3.1.0$ sudo apt-get update
….
Hit http://cn.archive.ubuntu.com precise-backports/universe Translation-en
Fetched 5,851 kB in 17s (344 kB/s)
W: Failed to fetch bzip2:/var/lib/apt/lists/partial/security.ubuntu.com_ubuntu_dists_precise-security_main_source_Sources Hash Sum mismatch
W: Failed to fetch bzip2:/var/lib/apt/lists/partial/security.ubuntu.com_ubuntu_dists_precise-security_main_i18n_Translation-en Hash Sum mismatch
E: Some index files failed to download. They have been ignored, or old ones used instead.
没关系,继续:
arac@test:~/Downloads/cmake-3.1.0$ sudo apt-get install build-essential
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following extra packages will be installed:
dpkg-dev fakeroot g++ g++-4.6 libalgorithm-diff-perl libalgorithm-diff-xs-perl libalgorithm-merge-perl libdpkg-perl libstdc++6-4.6-dev libtimedate-perl
Suggested packages:
debian-keyring g++-multilib g++-4.6-multilib gcc-4.6-doc libstdc++6-4.6-dbg libstdc++6-4.6-doc
The following NEW packages will be installed:
build-essential dpkg-dev fakeroot g++ g++-4.6 libalgorithm-diff-perl libalgorithm-diff-xs-perl libalgorithm-merge-perl libdpkg-perl libstdc++6-4.6-dev libtimedate-perl
0 upgraded, 11 newly installed, 0 to remove and 147 not upgraded.
Need to get 9,475 kB of archives.
After this operation, 28.8 MB of additional disk space will be used.
Do you want to continue [Y/n]? y
Get:1 http://cn.archive.ubuntu.com/ubuntu/ precise/main libstdc++6-4.6-dev amd64 4.6.3-1ubuntu5 [1,660 kB]
Get:2 http://cn.archive.ubuntu.com/ubuntu/ precise/main g++-4.6 amd64 4.6.3-1ubuntu5 [6,954 kB]
Get:3 http://cn.archive.ubuntu.com/ubuntu/ precise/main g++ amd64 4:4.6.3-1ubuntu5 [1,442 B]
Get:4 http://cn.archive.ubuntu.com/ubuntu/ precise/main libtimedate-perl all 1.2000-1 [41.6 kB]
Get:5 http://cn.archive.ubuntu.com/ubuntu/ precise-updates/main libdpkg-perl all 1.16.1.2ubuntu7.5 [182 kB]
Get:6 http://cn.archive.ubuntu.com/ubuntu/ precise-updates/main dpkg-dev all 1.16.1.2ubuntu7.5 [468 kB]
Get:7 http://cn.archive.ubuntu.com/ubuntu/ precise-updates/main build-essential amd64 11.5ubuntu2.1 [5,816 B]
Get:8 http://cn.archive.ubuntu.com/ubuntu/ precise/main fakeroot amd64 1.18.2-1 [87.2 kB]
Get:9 http://cn.archive.ubuntu.com/ubuntu/ precise/main libalgorithm-diff-perl all 1.19.02-2 [50.7 kB]
Get:10 http://cn.archive.ubuntu.com/ubuntu/ precise/main libalgorithm-diff-xs-perl amd64 0.04-2build2 [12.4 kB]
Get:11 http://cn.archive.ubuntu.com/ubuntu/ precise/main libalgorithm-merge-perl all 0.08-2 [12.7 kB]
Fetched 9,475 kB in 5s (1,700 kB/s)
Selecting previously unselected package libstdc++6-4.6-dev.
(Reading database ... 144049 files and directories currently installed.)
Unpacking libstdc++6-4.6-dev (from .../libstdc++6-4.6-dev_4.6.3-1ubuntu5_amd64.deb) ...
Selecting previously unselected package g++-4.6.
Unpacking g++-4.6 (from .../g++-4.6_4.6.3-1ubuntu5_amd64.deb) ...
Selecting previously unselected package g++.
Unpacking g++ (from .../g++_4%3a4.6.3-1ubuntu5_amd64.deb) ...
Selecting previously unselected package libtimedate-perl.
Unpacking libtimedate-perl (from .../libtimedate-perl_1.2000-1_all.deb) ...
Selecting previously unselected package libdpkg-perl.
Unpacking libdpkg-perl (from .../libdpkg-perl_1.16.1.2ubuntu7.5_all.deb) ...
Selecting previously unselected package dpkg-dev.
Unpacking dpkg-dev (from .../dpkg-dev_1.16.1.2ubuntu7.5_all.deb) ...
Selecting previously unselected package build-essential.
Unpacking build-essential (from .../build-essential_11.5ubuntu2.1_amd64.deb) ...
Selecting previously unselected package fakeroot.
Unpacking fakeroot (from .../fakeroot_1.18.2-1_amd64.deb) ...
Selecting previously unselected package libalgorithm-diff-perl.
Unpacking libalgorithm-diff-perl (from .../libalgorithm-diff-perl_1.19.02-2_all.deb) ...
Selecting previously unselected package libalgorithm-diff-xs-perl.
Unpacking libalgorithm-diff-xs-perl (from .../libalgorithm-diff-xs-perl_0.04-2build2_amd64.deb) ...
Selecting previously unselected package libalgorithm-merge-perl.
Unpacking libalgorithm-merge-perl (from .../libalgorithm-merge-perl_0.08-2_all.deb) ...
Processing triggers for man-db ...
Setting up libtimedate-perl (1.2000-1) ...
Setting up libdpkg-perl (1.16.1.2ubuntu7.5) ...
Setting up dpkg-dev (1.16.1.2ubuntu7.5) ...
Setting up fakeroot (1.18.2-1) ...
update-alternatives: using /usr/bin/fakeroot-sysv to provide /usr/bin/fakeroot (fakeroot) in auto mode.
Setting up libalgorithm-diff-perl (1.19.02-2) ...
Setting up libalgorithm-diff-xs-perl (0.04-2build2) ...
Setting up libalgorithm-merge-perl (0.08-2) ...
Setting up libstdc++6-4.6-dev (4.6.3-1ubuntu5) ...
Setting up g++-4.6 (4.6.3-1ubuntu5) ...
Setting up g++ (4:4.6.3-1ubuntu5) ...
update-alternatives: using /usr/bin/g++ to provide /usr/bin/c++ (c++) in auto mode.
Setting up build-essential (11.5ubuntu2.1) ...
arac@test:/var$
安装顺利完成。