简体   繁体   English

g++ 无法构建 Hello World

[英]g++ can't build Hello World

my problem that I simply can't compile Hello World programm with g++我的问题是我根本无法用 g++ 编译 Hello World 程序

    #include <iostream>

    using namespace std;

    int main(void) {

        cout << "Hello World!" << endl;
        return 0;
    }

Compiler gave me message:编译器给了我消息:

evgeny@debian:~/Documents/Programming$ g++ test.cpp -o test
In file included from /usr/include/c++/4.9/clocale:42:0,
                 from /usr/include/x86_64-linux-gnu/c++/4.9/32/bits/c++locale.h:41,
                 from /usr/include/c++/4.9/bits/localefwd.h:40,
                 from /usr/include/c++/4.9/ios:41,
                 from /usr/include/c++/4.9/ostream:38,
                 from /usr/include/c++/4.9/iostream:39,
                 from test.cpp:1:
/usr/include/locale.h:29:25: fatal error: bits/locale.h: No such file or directory
 #include <bits/locale.h>
                         ^
compilation terminated.

I've installed libstdc++-developer, tried to use -I/way-to-the-missing-lib, different architecture keys etc., but it doesn't work as well.我已经安装了 libstdc++-developer,尝试使用 -I/way-to-the-missing-lib、不同的体系结构密钥等,但效果不佳。

Architecture info Linux debian 3.16.0-4-amd64 #1 SMP Debian 3.16.43-2+deb8u5 (2017-09-19) x86_64 GNU/Linux架构信息 Linux debian 3.16.0-4-amd64 #1 SMP Debian 3.16.43-2+deb8u5 (2017-09-19) x86_64 GNU/Linux

OS Debian 8.9操作系统 Debian 8.9

g++ version g++ (Debian 4.9.2-10) 4.9.2 g++ 版本 g++ (Debian 4.9.2-10) 4.9.2

installed packages list:已安装的软件包列表:

evgeny@debian:~$ dpkg --list | grep c++                                                                                                                                                          
ii  lib32stdc++-4.9-dev                   4.9.2-10                                   amd64        GNU Standard C++ Library v3 (development files)                                                
ii  lib32stdc++6                          4.9.2-10                                   amd64        GNU Standard C++ Library v3 (32 bit Version)                                                   
ii  libflac++6:amd64                      1.3.0-3                                    amd64        Free Lossless Audio Codec - C++ runtime library                                                
ii  libgpgme++2                           4:4.14.2-2+deb8u2                          amd64        c++ wrapper library for gpgme                                                                  
ii  libsigc++-2.0-0c2a:amd64              2.4.0-1                                    amd64        type-safe Signal Framework for C++ - runtime                                                   
ii  libstdc++-4.9-dev:amd64               4.9.2-10                                   amd64        GNU Standard C++ Library v3 (development files)                                                
ii  libstdc++6:amd64                      4.9.2-10                                   amd64        GNU Standard C++ Library v3                                                                    
ii  libx32stdc++-4.9-dev                  4.9.2-10                                   amd64        GNU Standard C++ Library v3 (development files)                                                
ii  libx32stdc++6                         4.9.2-10                                   amd64        GNU Standard C++ Library v3 (x32)

Any ideas?有任何想法吗? I hope solution is obvious, thank you in advance.我希望解决方案很明显,在此先感谢您。

If you use -m32 (which in 2017 is a strange thing to do, since most Linux are 64 bits), you need 32 bits libraries, such as lib32stdc++-7-dev package (or lib32stdc++-6-dev ; it depends upon your version of g++ ) on Debian.如果您使用-m32 (这在 2017 年是一件奇怪的事情,因为大多数 Linux 是 64 位),您需要 32 位库,例如lib32stdc++-7-dev包(或lib32stdc++-6-dev ;这取决于您的Debian 上的g++版本。

BTW, naming your executable test is very poor taste, it collides with the standard (perhaps builtin) test(1) .顺便说一句,命名您的可执行test是非常糟糕的品味,它与标准(可能是内置的) test(1) 相冲突。

I recommend compiling for your native architecture (probably x86-64) with all warnings and debug info, eg using我建议使用所有警告和调试信息为您的本机架构(可能是 x86-64)进行编译,例如使用

 g++ -Wall -Wextra -g test.cpp -o mytest

and of course, the resulting executable mytest is likely to be 64 bits.当然,生成的可执行文件mytest可能是 64 位的。 You could check with the file mytest command.您可以使用file mytest命令进行检查。

BTW, your g++ 4.9 is quite old.顺便说一句,你的g++ 4.9 已经很老了。 Why don't you upgrade it?你为什么不升级它? You could install GCC5 or GCC6, eg with aptitude install g++-6 ;您可以安装 GCC5 或 GCC6,例如使用aptitude install g++-6 in october 2017 the current version of GCC is GCC7 2017 年 10 月 GCC 的当前版本是GCC7

Today you'll better learn at least C++11 (don't lose your time learning something older).今天你最好至少学习C++11 (不要浪费时间学习更老的东西)。 Older standards are obsolete.较旧的标准已过时。 So it is better to use at least GCC6 if you want a good support of that standard.因此,如果您想要对该标准的良好支持,最好至少使用 GCC6。

Of course, you need the C++ standard library development package.当然,你需要C++标准库开发包。 On my Debian/Sid it is libstdc++-6-dev在我的 Debian/Sid 上,它是libstdc++-6-dev

The build-essential metapackage is for building essential utilities coded in C, not in C++. build-essential元包用于构建用 C 而非 C++ 编码的基本实用程序。
You need to explicitly install several C++ related packages (and you don't tell which you have installed).您需要显式安装几个与 C++ 相关的包(并且您不知道安装了哪些)。

If you know some Debian package coded in C++ (eg fish ), you might use the aptitude build-dep command on it;如果你知道一些用 C++ 编码的 Debian 包(例如fish ),你可以在它上面使用aptitude build-dep命令; it would install properly all the packages needed to build it, and that would include necessary C++ stuff.它将正确安装构建它所需的所有包,这将包括必要的 C++ 东西。 So you could try aptitude build-dep fish所以你可以试试aptitude build-dep fish

In october 2017 the stable version of Debian is Debian stretch 9.2; 2017 年 10 月 Debian 的稳定版本是 Debian stretch 9.2; if you use an older variant, you should consider upgrading your distribution.如果您使用较旧的变体,则应考虑升级您的发行版。

你能在'/usr/inlcude/bits'目录下找到locale.h文件吗,编译错误提示文件丢失。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM