7

I am trying to compile my C++ code using clang++ but keep getting this error with the conflicting namespace. My main.cpp file is a simple Hello World program (for debugging).

I have a feeling the issue is with my version of GCC or clang that I compiled on my cluster. Any ideas as to how to trace this issue down? Or steps to troubleshoot?

[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)
                                          ^
Adam
  • 510
  • 1
  • 5
  • 21
  • 1
    Looks to me like `_GLIBCXX_VISIBILITY()` is a macro that expands into something correctly under g++, but clang++ is not seeing the definition for it, so the preprocessor has left that string in there and clang++ sees it as an identifier, which is unexpected. – twalberg Nov 05 '13 at 21:21

5 Answers5

10

As this is the first result for searches along the lines of:

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

I'll confess that I got this error by missing a closing-brace in a header file included before the one mentioned in the backtrace.

Hopefully this helps someone.

Samizdis
  • 1,591
  • 1
  • 17
  • 33
  • Just what was my problem, thanks! I copied function declaration from .cpp file to .h file, but accidentally left a bracket at the end, like that: int some_function(){ Deleting bracket and adding semicolon fixed issue. Stupid compiler couldn't say so from the beginning... – akrasuski1 Jan 05 '15 at 21:01
  • 1
    In my case, I was missing a semi-colon on the last function of my header file... C++ being C++. – igormcoelho Aug 20 '21 at 01:33
1

I had the same issue.

The problem was that I had only the following as CPATH:

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

After greping for _GLIBCXX_VISIBILITY in the same directory, it seems that the macro is defined in a sub-directory, specific for the host machine. In my case, this is x86_64-unknown-linux-gnu. Adding that to CPATH solved the problem. My new CPATH is:

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

For my example machine this translates to:

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

I hope that helps someone.

stanm
  • 3,201
  • 1
  • 27
  • 36
0

I am using clang++ 4.2 and it worked for me, strange

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

What if you just get rid of the -I/data/apps/gcc/4.8.1/include/c++/4.8.1

Doesn't it work by default?

Montaldo
  • 863
  • 8
  • 16
  • no this does not work, nor desired because I will be using the system's version of GCC and _not_ the one I have specified (4.8.1) in my environment. I am using the [module's](http://modules.sourceforge.net) system but for the purpose of debugging I am directly including it via the command line. – Adam Nov 05 '13 at 19:34
0

for GCC define subject of attribute for function, namespaces passed arguments types, call protocol and so no. Macros _GLIBCXX_VISIBILITY(default) is redefinition of attribute( visibility_(default)) In Windows is not defined... in Linux is on! As to another OC I don't know. You must redefine this macros as empty and set them before all includes. So on error occurs. But it's strange... If you setup STL platform oriented the appropriate redefinition must be!!! You need it to manually. As to me I like attributes. They allow control some aspects not available any compiler and grantee correct behavior. For example control arguments type and number of vars passed in function printf according to format.And if you passed different type or not enough number args error occurs as a compile error!!! Nothing else gives such an opportunity!!! Use attributes on Linux. It's good an idea...

0

I hit the same issue.

I mix up gcc 4.8 headers and system headers(lower version,gcc 4.4.6, _GLIBCXX_VISIBILITY not defined).

use:

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

to see all the "include" details.

In my case:

. /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)

Fixed with this:

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

For that _GLIBCXX_VISIBILITY is defined in /include/c++/4.8.2/x86_64-baidu-linux-gnu/bits/c++_config.h

ysf
  • 21
  • 2
  • same issue:https://stackoverflow.com/questions/10308167/when-enabling-c11-with-stdlibc-4-7-clang-error-out-while-gcc-compiles-fine – ysf Nov 16 '18 at 08:39