7

我正在尝试使用clang++冲突的命名空间来编译我的 C++ 代码,但不断收到此错误。我的 main.cpp 文件是一个简单的 Hello World 程序(用于调试)。

我感觉问题出在我在集群上编译的 GCC 或 clang 版本上。关于如何追踪这个问题的任何想法?或故障排除步骤?

[aebrenne@hpc src]$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/data/apps/gcc/4.8.1/libexec/gcc/x86_64-unknown-linux-gnu/4.8.1/lto-wrapper
Target: x86_64-unknown-linux-gnu
Configured with: ./configure --with-gmp=/data/apps/gmp/5.1.2 --with-mpfr=/data/apps/mpfr/3.1.2 --with-mpc=/data/apps/mpc-1.0.1 --enable-threads=posix --with-as=/data/apps/binutils/2.23.2/bin/as --mandir=/data/apps/gcc/4.8.1/man --pdfdir=/data/apps/gcc/4.8.1/pdf --htmldir=/data/apps/gcc/4.8.1/html --enable-languages=c,c++,fortran,ada,go,java,lto,objc,obj-c++ --prefix=/data/apps/gcc/4.8.1
Thread model: posix
gcc version 4.8.1 (GCC) 

[aebrenne@hpc src]$ clang++ --version
clang version 3.4 (trunk 193367)
Target: x86_64-unknown-linux-gnu
Thread model: posix
[aebrenne@hpc src]$ 


[aebrenne@hpc src]$ cat main.cpp 
#include <iostream>

int main() {
    std::cout << "Starting test..." << std::endl;

    return 0;
}
[aebrenne@hpc src]$ clang++ -std=c++11 -Wall -g -I/data/apps/gcc/4.8.1/include/c++/4.8.1  main.cpp 
In file included from main.cpp:1:
In file included from /data/apps/gcc/4.8.1/include/c++/4.8.1/iostream:39:
In file included from /data/apps/gcc/4.8.1/include/c++/4.8.1/ostream:38:
In file included from /data/apps/gcc/4.8.1/include/c++/4.8.1/ios:38:
In file included from /data/apps/gcc/4.8.1/include/c++/4.8.1/iosfwd:39:
In file included from /data/apps/gcc/4.8.1/include/c++/4.8.1/bits/stringfwd.h:40:
/data/apps/gcc/4.8.1/include/c++/4.8.1/bits/memoryfwd.h:50:15: error: expected '{'
namespace std _GLIBCXX_VISIBILITY(default)
              ^
/data/apps/gcc/4.8.1/include/c++/4.8.1/bits/memoryfwd.h:50:15: error: C++ requires a type specifier for all declarations
namespace std _GLIBCXX_VISIBILITY(default)
              ^~~~~~~~~~~~~~~~~~~
/data/apps/gcc/4.8.1/include/c++/4.8.1/bits/memoryfwd.h:50:35: error: expected expression
namespace std _GLIBCXX_VISIBILITY(default)
                                  ^
/data/apps/gcc/4.8.1/include/c++/4.8.1/bits/memoryfwd.h:50:43: error: expected ';' after top level declarator
namespace std _GLIBCXX_VISIBILITY(default)
                                          ^
4

5 回答 5

8

因为这是按照以下方式搜索的第一个结果:

error: expected unqualified-id before 'namespace'
namespace std _GLIBCXX_VISIBILITY(default)

我承认我得到了这个错误,因为我在回溯中提到的那个之前的头文件中缺少了一个右大括号。

希望这可以帮助某人。

于 2014-11-17T23:20:12.820 回答
1

我遇到过同样的问题。

问题是我只有以下内容CPATH

<gcc-install-path>/include/c++/<gcc-version>

在同一目录中greping for之后_GLIBCXX_VISIBILITY,似乎宏是在子目录中定义的,特定于主机。就我而言,这是x86_64-unknown-linux-gnu. 添加它以CPATH解决问题。我的新CPATH是:

<gcc-install-path>/include/c++/<gcc-version>:<gcc-install-path>/include/c++/<gcc-version>/<machine-specific-headers>

对于我的示例机器,这转化为:

export CPATH=~/f/i/gcc-4.8.4/include/c++/4.8.4:~/f/i/gcc-4.8.4/include/c++/4.8.4/x86_64-unknown-linux-gnu

我希望这对某人有所帮助。

于 2016-02-24T11:13:35.090 回答
0

为 GCC 定义函数的属性主题、命名空间传递的参数类型、调用协议等等。宏 _GLIBCXX_VISIBILITY(默认)是属性的重新定义(可见性_(默认))在 Windows 中未定义...在 Linux 中开启!至于另一个OC我不知道。您必须将此宏重新定义为空并在所有包含之前设置它们。所以发生错误。但这很奇怪...如果您设置面向STL平台,则必须进行适当的重新定义!您需要手动操作。至于我,我喜欢属性。它们允许控制任何编译器和被授权者不可用的某些方面的正确行为。例如,根据格式控制参数类型和在函数 printf 中传递的变量数量。如果传递的类型不同或数量不足,则会发生编译错误!没有什么能提供这样的机会!!!在 Linux 上使用属性。这是个好主意...

于 2014-06-07T06:04:48.700 回答
0

我正在使用 clang++ 4.2,它对我有用,奇怪

clang++ -std=c++11 -Wall -g main.cpp 

如果你只是摆脱 -I/data/apps/gcc/4.8.1/include/c++/4.8.1

默认情况下不工作吗?

于 2013-11-05T19:30:58.977 回答
0

我遇到了同样的问题。

我混淆了 gcc 4.8 头文件和系统头文件(较低版本,gcc 4.4.6,_GLIBCXX_VISIBILITY 未定义)。

利用:

clang++ -std=c++11 -Wall -g -I/data/apps/gcc/4.8.1/include/c++/4.8.1  main.cpp -v -H

查看所有“包含”详细信息。

就我而言:

. /include/c++/4.8.2/iostream
.. /usr/lib64/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/x86_64-redhat-linux/bits/c++config.h
... /include/bits/wordsize.h
... /usr/lib64/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/x86_64-redhat-linux/bits/os_defines.h
.... /include/features.h
..... /include/stdc-predef.h
..... /include/sys/cdefs.h
...... /include/bits/wordsize.h
..... /include/gnu/stubs.h
...... /include/gnu/stubs-64.h
... /usr/lib64/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/x86_64-redhat-linux/bits/cpu_defines.h
.. /include/c++/4.8.2/ostream
... /include/c++/4.8.2/ios
.... /include/c++/4.8.2/iosfwd
..... /include/c++/4.8.2/bits/stringfwd.h
...... /include/c++/4.8.2/bits/memoryfwd.h
In file included from test.cpp:1:
In file included from /include/c++/4.8.2/iostream:39:
In file included from /include/c++/4.8.2/ostream:38:
In file included from /include/c++/4.8.2/ios:38:
In file included from /include/c++/4.8.2/iosfwd:39:
In file included from /include/c++/4.8.2/bits/stringfwd.h:40:
/include/c++/4.8.2/bits/memoryfwd.h:50:15: error: expected '{'
namespace std _GLIBCXX_VISIBILITY(default)

修复了这个:

clang++ -std=c++11 -isystem /include/c++/4.8.2/ -isystem /include/c++/4.8.2/x86_64-baidu-linux-gnu/ test.cpp -v -H

为此 _GLIBCXX_VISIBILITY 在 /include/c++/4.8.2/x86_64-baidu-linux-gnu/bits/c++_config.h 中定义

于 2018-11-16T08:35:28.657 回答