我已经研究 libstdc++ 标头几年了,我总是对它混合制表符和空格的奇怪程度感到惊讶,例如:
template<typename _Up, typename... _Args>
<tab >constexpr
<tab >_Optional_payload_base(std::initializer_list<_Up> __il,
<tab ><tab ><tab > _Args&&... __args)
<tab >: _M_payload(__il, std::forward<_Args>(__args)...),
<tab > _M_engaged(true)
<tab >{ }
<...>
<tab > _Empty_byte _M_empty;
_Up _M_value;
<tab >};
template<typename _Up>
<tab >union _Storage<_Up, false>
<tab >{
Run Code Online (Sandbox Code Playgroud)
为什么即使在同一行中也会随机混合制表符和空格?
当使用非 8 个选项卡显示设置查看时,它会变得更加混乱(我猜很多用户可能都有......)。
libc++ 标头 OTOH 看起来很好(没有选项卡 AFAIR)...
显然,如果它只是关于制表符与空格,那么这将是一个基于意见的问题,但这是两者的混合......我认为这从来都不是一个好主意,并且除了 libstdc++ 之外没有在任何地方看到它,我认为......
所以,我发现自己需要在我的C++程序中使用libc.但是,我不喜欢将它全局放在全局命名空间中.理想情况下,我想libc中的全部强行插入std::命名空间,所以我不得不做std::memcpy,而不是memcpy.
这可能吗?如何?如果需要,我愿意使用特定于编译器的宏(我只针对MS VC++ 10.0和GCC 4.6).
编辑:我的字面意思是"强制声明为std" - 这样它们在没有std ::前缀的情况下是不可调用的.另外,我包括cstdio,不是stdio.h.
谢谢!
我使用STL的'随机'生成二项分布的随机数.当范围很大时,它变得非常慢.对于范围40,生成100个数字需要12秒.对于更大的范围,时间会急剧增加(我需要大约10000的范围).它似乎不依赖于概率参数.我正在使用g ++ 4.5.0.
#include <iostream>
#include <random>
using namespace std;
vector<int> v;
default_random_engine gen(123);
binomial_distribution<int> rbin(40,0.7);
int main(){
v.reserve(2000);
for(int i=0; i<100;++i){
v.push_back(rbin(gen));
}
}
Run Code Online (Sandbox Code Playgroud)
输出:
50.~/.../fs/> g++ -std=c++0x q.cpp
51.~/.../fs/> time ./a.out
real 0m12.102s
user 0m12.094s
sys 0m0.002s
52.~/.../fs/>
Run Code Online (Sandbox Code Playgroud)
我可以使用正态近似,但它对于概率参数的极值是不好的.
'-O3'选项时间变为~2秒.使用g ++ 4.6.3时,问题完全消失了 - 几乎没有时间依赖于范围,100个数字的生成需要5ms.
我正在尝试运行一个名为GlimmerHMM的程序,但是当我尝试调用该程序时,我收到此错误:
./glimmerhmm_linux
./glimmerhmm_linux: error while loading shared libraries: libstdc++.so.5: cannot open shared object file: No such file or directory
Run Code Online (Sandbox Code Playgroud)
所以我尝试下载和安装:
sudo apt-get install libstdc++5
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
html2text libmail-sendmail-perl libsys-hostname-long-perl
Use 'apt-get autoremove' to remove them.
The following NEW packages will be installed: libstdc++5
0 upgraded, 1 newly installed, 0 to remove and 183 not upgraded.
Need to get 255 kB …Run Code Online (Sandbox Code Playgroud) 请考虑以下示例代码:
#include <iostream>
using namespace std;
int main()
{
istreambuf_iterator<char> eos;
istreambuf_iterator<char> iit(cin.rdbuf());
int i;
for (i = 0; iit != eos; ++i, ++iit) {
cout << *iit;
}
cout << endl << i << endl;
}
Run Code Online (Sandbox Code Playgroud)
以及包含以下内容的输入文件:"foo\xffbar":
$ hexdump testin
0000000 66 6f 6f ff 62 61 72
0000007
Run Code Online (Sandbox Code Playgroud)
现在使用clang libc ++ vs gnu libstdc ++进行测试:
$ make test
clang++ -std=c++11 -stdlib=libc++ -Wall -stdlib=libc++ -o bug-libcc bug.cpp
clang++ -std=c++11 -stdlib=libc++ -Wall -stdlib=libstdc++ -o bug-libstd bug.cpp
./bug-libcc < testin
foo …Run Code Online (Sandbox Code Playgroud) 我正在进行一些研究,并希望编辑libstdc ++库中的一些源代码进行实验.具体而言,我对试验并行排序算法感兴趣.有没有一个地方我可以找到文档来轻松编辑和构建源代码?
我试过构建各种版本的libstdc ++库,但没有成功.似乎大多数新版本都需要构建整个gcc包,这是一个更漫长的过程,特别是如果我要编辑和试验libstdc ++中的一些文件.
我也一直无法找到包含并行排序算法的源文件.我似乎只能找到定义函数的头文件,而不是源代码本身.任何建议或文档链接将不胜感激.
我们想要创建一个共享库(.so)来定位所有发行版,包括旧发行版.代码用C++编写并使用C++ 11特性,因此编译器必须至少为gcc 4.7.我们注意到,如果我们在安装了gcc 4.7.2的Linux机器上编译我们的代码(例如,Ubuntu 12.10),那么.so生产的版本1(GNU/Linux),而在较旧的OS(例如,CentOS 5.6)上版本是"版本1(SYSV)" - 具有GNU/Linux更新版本的库不能用于旧版本的操作系统.
所以我们尝试了在CentOS 5.6机器上安装gcc 4.7的方法,使用这个编译器编译我们的代码并与libstdc ++(-static-libstdc ++)静态链接 - 这产生了一个可以在我们找到的每个linux上使用的.so.
这适用于32位.但是,当我们在64位操作系统(CentOS)上遵循相同的方法时,这个错误导致我们尝试链接到的现有libstdc ++.是在没有-fPIC的情况下编译的.
所以我们尝试使用"-with-pic"选项编译gcc 4.7.2源代码,但是我们无法链接到新的libstdc ++.a - 错误是:
/opt/centos/devtoolset-1.1/root/usr/libexec/gcc/x86_64-CentOS-linux/4.7.2/ld:/usr/local/lib/libFoo.so:找不到符号_ZNSs7_M_copyEPcPKcm @ GLIBCXX_3的版本节点. 4 /opt/centos/devtoolset-1.1/root/usr/libexec/gcc/x86_64-CentOS-linux/4.7.2/ld:未能设置动态节大小:错误值collect2:错误:ld返回1退出状态
我们用Google搜索编译libstdc ++与-fPIC可能有问题,但为什么它适用于32位而不适用于64位操作系统?是否有另一种建议的方法来为所有Linux发行版创建一个.so?
这是我试图在嵌入式Linux系统上运行的一些示例测试代码:
#include <iostream>
int main(int argc, char *argv[])
{
char c = 'A';
int i = 7;
std::cout << "Hello World from C++" << std::endl;
std::cout << "c=" << c << std::endl;
std::cout << "i=" << i << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
嵌入式系统是Microblaze,它是在Xilinx FPGA上运行的32位RISC软核处理器.请不要因为你的许多标准Linux知识仍然适用而被推迟.处理器配置为带有MMU的LSB,而我正在使用的Linux版本(由Xilinx提供的PetaLinux)期望相同.我正在使用提供的GNU编译器; Microblaze似乎在GCC正式支持.
我遇到的问题是,当stdlib需要与整数进行交互时,会出现段错误.这是输出:
Hello World from C++
c=A
Segmentation fault
Run Code Online (Sandbox Code Playgroud)
请注意,char处理得很好.这段代码的C等效也可以正常工作:
#include <stdio.h>
int main(int argc, char *argv[])
{
char c = 'A';
int i = 7;
printf("Hello World from C\n");
printf("c=%c\n", c);
printf("i=%i\n", i);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
... …
为什么这不起作用:
#include <regex>
int main() {
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编译为:
clang++ -std=c++11 -stdlib=libstdc++ temp.cpp
temp.cpp:1:10: fatal error: 'regex' file not found
#include <regex>
^
1 error generated.
clang++ --version
Apple LLVM version 7.0.0 (clang-700.1.76)
Target: x86_64-apple-darwin14.5.0
Thread model: posix
Run Code Online (Sandbox Code Playgroud)
如果我能够stdlib成为libc++然后再编译.正则表达式c++11,但clang本身似乎没有问题-std=c++11 -stdlib=libstdc++.至少在我的机器上,它看起来像我可以使用的东西/usr/include/regex.h,但这不是标准的,除了我想使用的正则表达式以外的东西(例如std :: to_string).
这已经想出的原因是因为我想链接到第三方库(对此我没有源),这是符合作为std::string,而不是std::__1::basic_string,但我代码使用std::regex和std::to_string.我不确定是否要引入对boost的依赖.
由于libstdc ++缺陷导致编译失败,或者此行为是否与Transactional Memory TS(n4514)兼容?
#include <type_traits>
static_assert(std::is_function_v<void() transaction_safe>, "");
int main() {}
Run Code Online (Sandbox Code Playgroud) libstdc++ ×10
c++ ×9
gcc ×4
linux ×3
c++11 ×2
libc++ ×2
clang ×1
indentation ×1
libc ×1
microblaze ×1
namespaces ×1
random ×1
stl ×1
type-traits ×1
ubuntu ×1