标签: c++

Linux(和 Unix?)中 C++ 中最常见的字符串编码

为了创建一个在 Windows 和 Linux 之间源代码级可移植并能很好地处理国际化的 C++ 程序,恕我直言,需要考虑三种主要编码:

  • C++ 源代码的编码。
  • 外部数据的编码。
  • 字符串和文字的编码。

对于 C++ 源代码,实际上没有任何替代 UTF-8 和 BOM 的方法,至少如果标准输入和宽字符串文字应该在 Windows 平台上工作。没有 BOM 的 UTF-8 导致 Microsoft 的 Visual C++ 编译器为源代码假定 Windows ANSI 编码,这对于通过 UTF-8 输出很好std::cout,在有限的程度上起作用(Windows 控制台窗口在这里有很多错误)。但是,然后通过输入std::cin不起作用。

而对于外部数据UTF-8似乎是事实上的标准。

但是,内部文字和字符串呢?在这里,我的印象是编码为 UTF-8 的窄字符串是 Linux 中的常见约定。但最近有两个不同的人提出了不同的说法,一个声称 Linux 中国际应用程序中内部字符串的通用约定是 UTF-32,另一个只是声称 Unix 和 Linux 在这方面存在一些未指明的差异。

作为一个在业余爱好基础上稍微摆弄一下旨在抽象出 Windows/Linux 在这方面的差异的微型库的人,我……不得不具体问一下

  • 在程序中表示字符串的常见 Linux 约定是什么?

我很确定有一个普遍的约定,这种约定非常普遍,以至于这个问题有一个真正的答案™。

一个示例显示例如如何在 Linux 上传统地反转字符串(直接使用 UTF-8 很复杂,但大概是由 Linux 中的事实上的标准函数完成的?),也很好,即,作为问题,这个 C++ 程序的 Linux 传统版本是什么(给定的代码适用于 Latin-1 作为 C++ 窄文本执行字符集):

#include …
Run Code Online (Sandbox Code Playgroud)

character-encoding conventions string c++

7
推荐指数
2
解决办法
2万
查看次数

为什么要将多个 .cpp 文件编译为同一个可执行文件?

你为什么要这样做

g++ -Wall -I/usr/local/include/thrift *.cpp -lthrift -o something
Run Code Online (Sandbox Code Playgroud)

代替:

g++ -Wall -I/usr/local/include/thrift -c Something.cpp -o something.o
g++ -Wall -I/usr/local/include/thrift -c Something_server.cpp -o server.o
g++ -Wall -I/usr/local/include/thrift -c your_thrift_file_constants.cpp -o constants.o
g++ -Wall -I/usr/local/include/thrift -c your_thrift_file_types.cpp -o types.o
Run Code Online (Sandbox Code Playgroud)

进而:

g++ -L/usr/local/lib -lthrift *.o -o Something_server
Run Code Online (Sandbox Code Playgroud)

第一步与第二个序列的作用基本相同,我说得对吗?

此外,为了使他们一致应该somethingSomething_server在第一线?

compiling c c++

6
推荐指数
1
解决办法
711
查看次数

为什么我的 C++ 程序被操作系统杀死

我已经在我的虚拟 ubuntu 11.04 上为学校项目编译了小型 C++ 应用程序。但是,当我尝试在大约 20 秒后在一些更大的数据集上运行它时,该程序被操作系统杀死。

被杀(SIGKILL)

我需要找出原因并解决问题。可能是我没有足够的内存来运行程序?注意:我需要找到足够大的数据集以使其运行几分钟。

process signals c++

6
推荐指数
1
解决办法
1万
查看次数

Fedora 27 /usr/bin/ld: 找不到 -lstdc++

我有 Fedora 27。我正在从源代码构建一些东西。(如果重要的话,它是https://github.com/xmrig/xmrig-nvidia)。

Make 进行链接,然后失败并显示以下消息:

/usr/bin/ld: cannot find -lstdc++
collect2: error: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)

安装了 libstdc++ 和 libstdc++-devel 包。为了以防万一,现在还安装了它们的 32 位版本。我仍然收到消息。

我能做些什么来解决这个问题?谢谢!

fedora make libraries c++ ld

6
推荐指数
1
解决办法
3084
查看次数

为什么/usr/include 中有多个头文件副本?

我一直在浏览我的 /usr/include 文件夹,试图熟悉布局,我注意到头文件有多个副本(至少按名称,我实际上并没有区分它们以查看它们是否准确副本)在 /usr/include 的几个子目录中找到。对于标准 C 和 C++ 头文件以及 POSIX/LSB 标准头文件尤其如此。

一些示例包括(注意 ./ 指的是 /usr/include):

./asm-generic/unistd.h
./linux/unistd.h
./unistd.h
./x86_64-linux-gnu/sys/unistd.h
./x86_64-linux-gnu/bits/unistd.h
./x86_64-linux-gnu/asm/unistd.h

./stdlib.h
./x86_64-linux-gnu/bits/stdlib.h
./c++/7/stdlib.h
./c++/7/tr1/stdlib.h

./c++/7/cmath
./c++/7/ext/cmath
./c++/7/tr1/cmath

./asm-generic/termios.h
./linux/termios.h
./x86_64-linux-gnu/sys/termios.h
./x86_64-linux-gnu/bits/termios.h
./x86_64-linux-gnu/asm/termios.h
./termios.h

./linux/time.h
./time.h
./x86_64-linux-gnu/sys/time.h
./x86_64-linux-gnu/bits/time.h
Run Code Online (Sandbox Code Playgroud)

为什么是这样?为什么某些 C 标准头文件会出现在 C++ 位置?

我只安装了一个编译器(GCC 7)。

c c++ linux-headers

6
推荐指数
1
解决办法
2282
查看次数

列出用 gcc 编译的项目使用的头文件以及层次结构

我目前有一个跨越两个不同源代码控制系统的 C++ 项目。如果没有将第一个系统的完整源代码签入第二个系统,我计划只签入构建的支持库。我的问题是我需要来自我在第二个源代码管理系统中使用的第一个源代码管理系统的头文件,以便我可以编译。有没有一种简单的方法可以强制 gcc 输出我在 Solaris 下编译时使用的所有头文件的名称?有没有办法生成头文件依赖关系的层次结构,以便我可以看到哪些文件包含哪些其他头文件?层次结构是否有可能知道 #pragma 一次,以便依赖关系树不包含多次未包含的重复项?

solaris gcc c++

5
推荐指数
1
解决办法
3612
查看次数

如何将 clang++ 设置为 C++ 编译器?

我正在尝试将 clang++ 设置为 Fedora 18 上的系统 C++ 编译器。我已经安装了 clang 并将其添加到其中,alternatives但它似乎不起作用。

[user@localhost ~]$ alternatives --display c++
c++ - status is auto.
 link currently points to /usr/bin/clang++
/usr/bin/clang++ - priority 1
Current `best' version is /usr/bin/clang++.
[user@localhost ~]$ c++ --version
c++ (GCC) 4.7.2 20121109 (Red Hat 4.7.2-8)
Copyright (C) 2012 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Run Code Online (Sandbox Code Playgroud)

c++尽管 …

fedora c++ alternatives

5
推荐指数
1
解决办法
2万
查看次数

Meson 找不到 Boost 库

我想使用 Meson 构建一个新的 C++ 项目。我需要的第一件事是 Boost 库的依赖项。但是,尽管 Boost 库安装在我的 Arch 系统上(标头和库),Meson 抱怨它找不到它们。

这是介子构建文件:

project('myproj', 'cpp')
boost_dep = dependency('boost')
executable('myproj', 'main.cpp', dependencies : boost_dep)
Run Code Online (Sandbox Code Playgroud)

源文件main.cpp

int main()
{
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我的系统上安装的一些 Boost 文件的部分列表:

$ ls /usr/lib/libboost*|head -n5; ls /usr/include/boost/*|head -n5
/usr/lib/libboost_atomic.a
/usr/lib/libboost_atomic.so
/usr/lib/libboost_atomic.so.1.65.1
/usr/lib/libboost_chrono.a
/usr/lib/libboost_chrono.so
/usr/include/boost/aligned_storage.hpp
/usr/include/boost/align.hpp
/usr/include/boost/any.hpp
/usr/include/boost/array.hpp
/usr/include/boost/asio.hpp
Run Code Online (Sandbox Code Playgroud)

ninja我的项目内命令的输出:

[0/1] Regenerating build files.
The Meson build system
Version: 0.43.0
Source dir: /home/io/prog/myproj/src
Build dir: /home/io/prog/myproj/builddir
Build type: native build
Project name: myproj
Native C++ compiler: …
Run Code Online (Sandbox Code Playgroud)

compiling c++ boost

5
推荐指数
1
解决办法
4694
查看次数

为什么这个 Mingw-w64 包这么大?

可以 安装x86_64-w64-mingw32-gcc吗?我需要一个命令,mingw-w64安装超过 800MB...我在 Debian Buster,但我刚刚尝试的其他 Linux 版本也是如此。

$ sudo apt-get install mingw-w64 -V --no-install-recommends
...
The following NEW packages will be installed:
   binutils-mingw-w64-i686 (2.31.1-11+8.3)
   binutils-mingw-w64-x86-64 (2.31.1-11+8.3)
   g++-mingw-w64 (8.3.0-6+21.3~deb10u1)
   g++-mingw-w64-i686 (8.3.0-6+21.3~deb10u1)
   g++-mingw-w64-x86-64 (8.3.0-6+21.3~deb10u1)
   gcc-mingw-w64 (8.3.0-6+21.3~deb10u1)
   gcc-mingw-w64-base (8.3.0-6+21.3~deb10u1)
   gcc-mingw-w64-i686 (8.3.0-6+21.3~deb10u1)
   gcc-mingw-w64-x86-64 (8.3.0-6+21.3~deb10u1)
   mingw-w64 (6.0.0-3)
   mingw-w64-common (6.0.0-3)
   mingw-w64-i686-dev (6.0.0-3)
   mingw-w64-x86-64-dev (6.0.0-3)
0 upgraded, 13 newly installed, 0 to remove and 2 not upgraded.
Need to get 137 MB of archives.
After this operation, 809 MB of additional disk …
Run Code Online (Sandbox Code Playgroud)

compiling packaging ubuntu debian c++

5
推荐指数
3
解决办法
1668
查看次数

如何在 M1 MacBook pro 上构建 GCC 13?

我想在我的 M1 MacBook Pro 上构建 gcc-13。按照官方文档,我使用git下载源代码,configure并且make.

对于,我参考了链接上Homebrew gcc-12configure的方式。具体是configure

../configure \
--prefix=/Users/xubaoyu/gcc/build \
--disable-nls \
--enable-checking=release \
--with-gcc-major-version-only \
--enable-languages=c,c++ \
--program-suffix=-13 \
--with-system-zlib \
--build=aarch64-apple-darwin22 \
--with-sysroot=/Library/Developer/CommandLineTools/SDKs/MacOSX13.sdk
Run Code Online (Sandbox Code Playgroud)

简单地说make -j 10

然后出现错误:

*** Configuration aarch64-apple-darwin22 not supported
make[2]: *** [configure-stage1-gcc] Error 1
make[1]: *** [stage1-bubble] Error 2
make: *** [all] Error 2
Run Code Online (Sandbox Code Playgroud)

我认为我在构建参数上使用了错误的值,因此我通过以下方式检查 gcc12 gcc-12 -v

Using built-in specs.
COLLECT_GCC=gcc-12
COLLECT_LTO_WRAPPER=/opt/homebrew/Cellar/gcc/12.2.0/bin/../libexec/gcc/aarch64-apple-darwin22/12/lto-wrapper
Target: aarch64-apple-darwin22
Configured with: ../configure --prefix=/opt/homebrew/opt/gcc …
Run Code Online (Sandbox Code Playgroud)

make gcc c++ configure macos

5
推荐指数
1
解决办法
3652
查看次数