所以我只是从自制安装gcc,我有一个简单的代码:
#include <cmath>
#include <iostream>
#include <stdio.h>
int main()
{
const int size = 256;
double sinTable[size];
#pragma omp parallel for
for(int n=0; n<size; ++n)
sinTable[n] = std::sin(2 * M_PI * n / size);
#pragma omp parallel for
for(int n=0; n<10; ++n)
{
printf(" %d", n);
}
printf(".n");
// the table is now initialized
}
然而,当我编译时,我失败了:
dhcp-18-189-47-44:openmp_code myname$ gcc-4.8 tmp2.cpp
Undefined symbols for architecture x86_64:
"std::ios_base::Init::Init()", referenced from:
__static_initialization_and_destruction_0(int, int) in ccFbBrPl.o
"std::ios_base::Init::~Init()", referenced from:
__static_initialization_and_destruction_0(int, int) in ccFbBrPl.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status
但是,如果我将其更改为g++-4.8,那么它就成功了…
dhcp-18-189-47-44:openmp_code myname$ g++-4.8 tmp2.cpp -fopenmp
我想知道为什么会发生....
你正在用'gcc'编译c++代码(为什么?),所以你需要链接到它的标准c++库。将-stdc++
添加到build命令中。当用g++编译时,它知道自动链接到这个库。
-
编辑
另一方面,您的代码与c++标准库无关。你有
#include <iostream>
,但你不用它。我认为(虽然没有检查),如果你注释掉这个包含,你的原始构建与'gcc'应该通过。
你在问为什么你的c++程序不能用C编译器编译,但可以用c++编译器正确编译?
那么这个反问句就是我的答案。
C编译器不会在c++运行时做链接之类的事情。你需要运行时。链接器错误显示c++运行时没有被链接。因为你使用的是C编译器