Can I use CLang and libstdc++ (emmintrin.h) - c++

The following code
#include <iostream>
int main(void)
{
std::cout << "Hello World!" << std::endl;
return 0;
}
compiles fine with both g++ and clang
g++ test.cpp
clang -I /usr/lib/gcc/x86_64-linux-gnu/4.7/include -I /usr/lib/gcc/x86_64-linux-gnu/4.7/include-fixed -lstdc++ test.cpp
where /usr/lib/gcc/x86_64-linux-gnu/4.7/ points to the include files of the libstdc++
However, when I add an additional #include <emmintrin.h>
the clang compilations fails with
In file included from test.cpp:2:
In file included from /usr/lib/gcc/x86_64-linux-gnu/4.7/include/emmintrin.h:36:
In file included from /usr/lib/gcc/x86_64-linux-gnu/4.7/include/xmmintrin.h:36:
/usr/lib/gcc/x86_64-linux-gnu/4.7/include/mmintrin.h:46:72: warning: unknown attribute '__artificial__' ignored [-Wunknown-attributes]
extern __inline void __attribute__((__gnu_inline__, __always_inline__, __artificial__))
^
/usr/lib/gcc/x86_64-linux-gnu/4.7/include/mmintrin.h:52:72: warning: unknown attribute '__artificial__' ignored [-Wunknown-attributes]
extern __inline void __attribute__((__gnu_inline__, __always_inline__, __artificial__))
...
/usr/lib/gcc/x86_64-linux-gnu/4.7/include/xmmintrin.h:120:19: error: use of undeclared identifier '__builtin_ia32_divss'; did you mean '__builtin_ia32_minss'?
return (__m128) __builtin_ia32_divss ((__v4sf)__A, (__v4sf)__B);
...
According to http://en.cppreference.com/w/cpp/header, the `xmmintrin.h' is not part of the interface of the standard C++ library and hence I guess it contains Gnu specific stuff. Can I somehow compile this code using clang?
If not, how can I easily install the clang libc++?
sudo aptitude install libc++-dev doesn't find a package (while this does work on Ubuntu 16.04)
I'm using
"Debian GNU/Linux 7 (wheezy)"
gcc version 4.7.2 (Debian 4.7.2-5) // Target: x86_64-linux-gnu
clang version 3.6.0 (trunk 224504) // Target: x86_64-unknown-linux-gnu

Apparently, my clang installation was incomplete.
After downloading llvm and clang (v3.7.1 since v4.0.1 required gcc4.8 while I only have 4.7) and building it locally clang -lstdc++ test.cpp works fine in both cases.

Related

Compile with c++17 mac

I can't compile with -std=c++17, I got :
error: invalid value 'c++17' in '-std=c++17'
However I update Xcode and clang.
My Clang version is:
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 9.0.0 (clang-900.0.39.2)
Target: x86_64-apple-darwin16.7.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin`
And I load the newest header like optional, I have to do
#include <experimental/optional>
instead of
#include <optional>
Xcode brings its own complete toolchain, including headers and the actual compiler.
Apple LLVM version 9.0.0 (clang-900.0.39.2) (which ships with Xcode 9.2) does not support the usage of the flag -std=c++17 since its too old. The optional header is only included under the folder experimental/. Which is why you need to #include <experimental/optional>
In order to compile your program with c++17 support using the compiler which comes with Xcode 9.2 you need to use the -std=c++1z flag.
Xcode 9.3 will be shipped with Apple LLVM version 9.1.0 (clang-902.0.30) which has support for the -std=c++17 flag. However the optional header is as of today still under the experimental/ subdirectory. This might change during the betas.
Here is what I get with this tests:
#include <experimental/optional>
int main(int, char* []) {
return 0;
}
g++ -std=c++17 -o test test.cpp
error: invalid value 'c++17' in '-std=c++17'
g++ -std=c++1z -o test test.cpp
Did you try the c++1z argument?
Also of note my test compiles without the -std=c++1z argument provided.
I think I'm on a newer version of OSX than you:
Target: x86_64-apple-darwin17.4.0
You should use -std=c++1z as flag.
libc++ with c++17 support since macos 15
-std=c++1z also works on Apple LLVM version 8.1.0 (clang-802.0.42)

Apple Clang; using C++11 with libstdc++

I have an issue when compiling a simple Hello file with an empty function taking initializer_list argument when using both -stdlib=libstdc++ and -std=c++11
If I use only -std=c++11 (which means compiling with libc++)
then the file compiles and prints Hello!
If I comment function_test and I use both -std=c++11 and -stdlib=libstdc++
then the file compiles and prints Hello!
If I keep the function function_test and I use both -std=c++11 and -stdlib=libstdc++
then I get the following error:
$ g++ -stdlib=libstdc++ -std=c++11 -o test test.cpp
test.cpp:1:10: fatal error: 'initializer_list' file not found
#include <initializer_list>
^
1 error generated.
Here is my file
#include <initializer_list>
#include <iostream>
using namespace std;
void function_test(initializer_list<int> something){}
int main(int argc, char * argv[])
{
cout << "Hello!" << endl;
function_test({0});
return 0;
}
Here is my apple clang version
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr
--with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 7.0.2 (clang-700.1.81)
Target: x86_64-apple-darwin15.3.0
Thread model: posix
--with-gxx-include-dir=/usr/include/c++/4.2.1
^^^^^^^
Notice the "4.2". Your libstdc++ is way to old for C++11. Upgrade it to some 5.x version for full C++11 support.
I found out that upgrading to a newer version of libstdc++ is just not possible with apple-llvm(clang). So using some features of C++11 with libstdc++ is not possible. The reason is this one:
Mainline libstdc++ has switched to GPL3, a license which the
developers of libc++ cannot use. libstdc++ 4.2 (the last GPL2 version)
could be independently extended to support C++11, but this would be a
fork of the codebase (which is often seen as worse for a project than
starting a new independent one). Another problem with libstdc++ is
that it is tightly integrated with G++ development, tending to be tied
fairly closely to the matching version of G++.
source:
http://libcxx.llvm.org/docs/
Thanks to all the answers/comments that helped me reach the answer.

clang seems to use the gcc libraries

This is the first time I use clang. What I notices is that any error from clang referencing the std library looks like this:
/usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/ostream:245:7:
^^^ ^^^ ^^^
So it looks like clang links — or at least includes — the gcc libraries.
The command I used: clang++ -c -Wall -Wextra -Werror -g test.cpp -o test.o. (The program had a intentional error just to prove this).
How is this possible? What can I do to make clang use its own libraries (but not break gcc)?
Additional information:
I am on a Ubuntu 14.04 machine.
clang++ --version
Ubuntu clang version 3.5-1ubuntu1 (trunk) (based on LLVM 3.5)
Target: x86_64-pc-linux-gnu
Thread model: posix
g++ --version
g++ (Ubuntu 4.8.2-19ubuntu1) 4.8.2
Copyright (C) 2013 Free Software Foundation, Inc.
I had previously installed several versions (at the same time, used them with update-alternatives) of gcc with apt-get. Right now I have only 4.8 (I have uninstalled the others). Could I have messed up something then? I have never installed clang (I guess it is default with Ubuntu).
Just to clarify: the correct programs compile and run in clang++.
Further tests: I know that gcc didn’t implement yet types like is_trivially_constructible and move operations on iostream in their standard c++11 library (https://gcc.gnu.org/onlinedocs/libstdc++/manual/status.html) and that clang has full c++11 conforming library so I tested those compiling with clang and I got the corresponding gcc errors, which only confirms that clang is using gcc libraries.
A very basic program
#include <iostream>
using namespace std;
int main() {
cout << "Yada Yada" << endl;
return 0;
}
gives this error when compiling with -std=c++1y in clang++:
In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/iostream:39:
...
/usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/cstdio:120:11: error: no member named 'gets' in the global namespace
using ::gets;
~~^
So right now I can’t compile anything with c++1y in clang.
You need to install libc++ and make clang use it with -stdlib=libc++
I had similar issue: GCC (g++) already was installed on my LinuxMint (Ubuntu base) so when compile with clang, was getting an " error: no member named 'gets' in the global namespace using ::gets ".
resolved by installing libc++-dev (sudo apt-get install libc++-dev) and compiling with -stdlib++ (clang++ -g -std=c++1y -stdlib=libc++ helloworld.cpp -o helloworld)
Your real problem is that you're using C++14 (c++1y was the informal name used to refer to it when it wasn't yet fully formed), with a C++ library belonging to GCC 4.8. GCC 4.8 has complete C++11 support, but hardly even started on C++14 features.
This is caused by C++14 removing std::gets, and the GNU C library anticipating on that by not defining gets in the global namespace while the C++ library has not yet caught up and is trying to make it available in the std namespace.
The proper way to solve this does not require using libc++, just to use a C++ library with C++14 support. GLIBCXX 4.9 (aka libstdc++) already suffices.

clang doesn't know std::atomic_bool, but XCode does

I'm trying to compile C++11 code that declares a variable of type std::atomic_bool. This is on Mac OS 10.8.2 with clang:
clang --version
Apple clang version 4.1 (tags/Apple/clang-421.11.66) (based on LLVM 3.1svn)
Target: x86_64-apple-darwin12.2.0
Thread model: posix
clang complains about std::atomic_bool:
clang++ -c -stdlib=libc++ -msse4 -std=c++11 -Wno-unused-parameter -I. -o query.o query.cpp
In file included from query.cpp:1:
[...]
./threadutils.h:33:10: error: no type named 'atomic_bool' in namespace 'std'; did you mean 'atomic_long'?
std::atomic_bool work;
However, the same file compiles fine in an XCode project using the same compiler. So I assume I'm missing something in my manual compiler invocation.
I tried a few variations such as -std=c++0x and -std=gnu++11, to no avail.
I figured it out. Unfortunately I planted a false flag into my question: it didn't work in XCode either, I had a different version of the source file imported there.
The problem was that C++11 defines "a named type atomic_bool corresponding to the specified atomic<bool>", but clang doesn't support that.
Renaming the type from atomic_bool to atomic<bool> fixed it.
I found the same problem. In my case I resolved it by including atomic:
#include <atomic>
static std::atomic_bool varname;
After this, I could call g++ with std=c++11 on a Linux (Ubuntu) as well as compile on XCode.

MinGW error about TITLEBARINFO not declared

I'm using the latest MinGW:
MINGWBASEDIR=C:\mingw
gcc version 4.7.0 (GCC)
gcc version 4.7.0 (GCC)
GNU gdb (GDB) 7.5
GNU ld (GNU Binutils) 2.22
GNU windres (GNU Binutils) 2.22
GNU dlltool (GNU Binutils) 2.22
GNU Make 3.82.90
#define __MINGW32_VERSION 3.20
#define __W32API_VERSION 3.17
This is the source file (test.cpp):
#include <stdio.h>
#include <windows.h>
int main(int Argc,char** Args) {
printf("%d",sizeof(TITLEBARINFO));
}
This is how I compile it:
g++ -c test.cpp
And this is the error message reported by MinGW:
C:\temp>g++ -c test.cpp
test.cpp: In function 'int main(int, char**)':
test.cpp:5:22: error: 'TITLEBARINFO' was not declared in this scope
TITLEBARINFO is supposed to be declared in winuser.h which windows.h includes.
I've checked these 2 header files in mingw/include dir, the definition of TITLEBARINFO is truly there. But I still get this compiler error, how strange!
gcc -c -D_WIN32_WINDOWS=0x0410 test.cpp
MinGW apparently defaults to 0x0400.