Including C++ 11 headers with Clang / LLVM - c++

I have installed clang and llvm from source, and am trying to compile some C++ code using features of the new standard.
I have found that while for example the use of for ranges e.g. for (i : vector) works fine, I am having trouble (cannot find header file) when I need to import a header e.g. <unordered_set> or <tuple>.
Do I need to use the new libc++ to use these headers, or is there just a simple build change I need to make? At the moment I have just built clang and llvm into a folder in my home directory, and am calling clang++ from there.

See http://clang.llvm.org/get_started.html.
If you intend to work on Clang C++ support, you may need to tell it how to find your C++ standard library headers. If Clang cannot find your system libstdc++ headers, please follow these instructions:
gcc -v -x c++ /dev/null -fsyntax-only to get the path.
Look for the comment "FIXME: temporary hack: hard-coded paths" in clang/lib/Frontend/InitHeaderSearch.cpp and change the lines below to include that path.

While the standard library comes with distributions of your compiler, when you're building it yourself, you still need to build the standard library itself. Some of its components may be header-only, but not all of them are.
So you do need to at least download the library, if not build it. Clang can use GCC's libstdc++, but they also have their libc++ project.

Related

Compiling cpp using LLVM 11.0.0 on Windows 10 mashine

LLVM (Clang) newbie question. I have installed the LLVM 11.0.0 on a clear Windows 10 mashine. What do I have to do to get an a.out for -target armv7a-none-eabi?
#include <stdio.h>
#include <iostream>
#include <vector>
int main(void) {
int counter = 0;
counter++;
printf("counter: %d\n", counter);
printf("c++14 output:");
std::vector<int> vect{1, 2, 3, 4, 5};
for (auto & el : vect)
std::cout << "-" << el << std::endl;
return counter;
}
Please write in detail what do I have to do, where to get needed headers, what to put in PATH, etc...
Important:
I need to cross-compile and get an output for -target armv7a-none-eabi
no Visual Studio on that mashine installed
Typically, when installing LLVM for Windows, the path variable is adjusted automatically, so you don't have to modify it. Of course, when installing LLVM, you have to make sure to install all files that are relevant for your build target (in your case: armv7a-none-eabi).
What you have to do is the following:
Run a shell (for example PowerShell) in a terminal.
Change to the folder that contains your source file.
Type clang -target armv7a-none-eabi myfile.cpp (provided you file's name is myfile.cpp) and press enter.
After hat, you have a a.exe file.
The bad news is that you can't really do exactly what you're asking for very easily. Unless you are terribly ambitious or tasked with creating the developer toolchain that can do what you ask, you don't need to bother with the rest below. Switch to a platform like Ubuntu linux that has easily-installed (gcc) cross toolchains, or ask your BSP vendor for a Windows cross toolchain based on clang.
The release tarballs provided by LLVM community at https://releases.llvm.org/ are best suited for native builds. Yes, they include a compiler, assembler, linker, C++ library. They all work together well as a native toolchain (but will refer to the native host C library, and the native host linker by default). Yes, it's true that the compiler/assembler that are included do support all of the LLVM targets. But you don't want just a compiler, you want a toolchain. A complete cross-target C/C++ toolchain will typically include:
OS headers that declare types and functions
C library headers that refer to the OS headers and declare types and functions, library/shared object archive that define functions.
C++ library headers that declare templates, types and functions, library/shared object archive that define functions.
target linker
binutils: standalone assembler, objcopy, objdump, archiver, etc.
With #2 above you could make a call to printf() and the linker would be able to find its implementation in the C library archive. Your linked ./a.out executable could then run that printf code and expect it to emit something to some console or semihosted output.
I am trying to cross-compile from windows to ARM. When I compile with
clang, I get an error missing stdio.h. When I compile with clang++ I
got warning: unable to find a Visual Studio installation. What to do?
When I compile with -target armv7a-none-eabi, I got missing stdio.h.
armv7a-none-eabi describes a target that has no operating system. If we had a stdio.h that declared printf, that would get us part of the way. But what should happen in the printf() implementation for this particular target?
What most users want is a vendor to provide the C library headers and archive -- a commercial or open source distributor who packages up a C library. Usually they're bundled with the toolchain itself. Sometimes the development board and corresponding toolchain come in one big bundle/BSP.
Since you asked for armv7a-none-eabi specifically, I would strongly recommend that you find a vendor to give you what you want. Especially if you need to use a Windows host.
If you aren't stuck on Windows, or are willing to use WSL: Debian and Ubuntu provide cross toolchains and C libraries (including ones like libnewlib-arm-none-eabi). Unfortunately, I don't think there's any clang-based cross toolchain that would leverage this C library.
While you could try to bind libnewlib-arm-none-eabi with a clang tarball from https://releases.llvm.org/, it won't be particularly easy. I would start off by reviewing https://releases.llvm.org/11.0.0/tools/clang/docs/UsersManual.html#configuration-files - create a config file that references the relevant include path(s) and library path(s).
Once you have the config file prototyped, start small and build:
try to compile an object file for your target from int foo(void) { return 33; }.
try to build an executable from a .c file with int main(void) {}
try to build an executable from a .c file with a call to printf().
If you get through those three steps, congratulations, you probably had to figure out a lot of interesting challenges. If you want std::cout and friends to work, you will need to build a C++ library for your target. You can use libc++/libc++abi or libstdc++. C++ libraries on baremetal/freestanding targets are probably not very common. That said, there's lots of C++ library content that has little or no system dependencies. Especially the C++98/03 content that focused on STL - they probably only depend on the system heap allocator. Your example only shows std::vector<> and std::cout, so it's probably doable.

In C++, what is wx.h?

The existing code is calling some sort of wx header file and my DEV C++ compiler just says there's no such file.
Code:
#include<wx/wx.h>
Compiler error:
[Error] wx/wx.h: No such file or directory
So my question is -
What is wx.h
How do I install it in my compiler so that I can use it?
Do I need to change my compiler or DEV C++ would do fine?
What is wx.h
It is the header file of a library. The GitHub project should have some instructions on how to fetch the dependencies and how to install them.
How do I install it in my compiler so that I can use it?
Normally you have to download the dependency, build it (following some instructions), and then you need to use the header in your project and link the library. The exact steps depend on each library and each compiler.
Do I need to change my compiler or DEV C++ would do fine?
In principle, no. Some libraries only work with some compilers, though.
Note that Dev-C++ is not a compiler, it is an IDE that comes with a port of GCC (as far as I know).
It seems that you are using WxWidgets framework but your compiler doesn't know where to find its headers, and apparently also libs which you would face with on a next step.
You, need to add to your compiler flags the output of wx-config --cxxflags. And also to your linker flags the output of wx-config --libs.
Assumption is of course that WxWidgets is installed on your PC

MinGW standard library missing any.h?

I recently downloaded mingw from http://www.mingw.org/ and installed its c++ compiler and dependencies, which include installing standard library headers. I have also successfully compiled a hello world program, compilation is fine, and common old headers are there and work fine (such as string.h).
However, when I attempted to #include <any>, it threw an error that any.h does not exist. And sure enough, when I look in mingw\include\, there is no any.h (and I see other things missing, such as variant.h).
I've looked through the mingw package manager, and I have the standard library stuff installed, and there's nothing else relevant to install. Does minGW support C++17? If so, how can I get these newer header files? It seems like this should be something really obvious.
You need mingw-w64, which provides more recent GCC versions:
http://mingw-w64.org
You can find a 7.2.0 download here:
http://mingw-w64.org/doku.php/download/mingw-builds
The any feature requires C++17 support, so if your compiler implementation actually does support it, you probably want to specify it on the command line like this:
g++ -std=c++17 a.cpp
Of course, this requires a modern C++ compiler, like that you can get from nuwen.net.

Why doesn't Clang come with standard library headers?

I downloaded Clang 3.6.2 from this website and am trying to set it up with Code::Blocks under Windows. Unfortunately, it fails to compile a simple "hello world" program on the grounds that it doesn't know where iostream is.
Looking through the install folder, it does not appear to include a standard library with it. Why? And how do I get it?
The standard library is NOT part of the compiler itself. It is part of the runtime environment on a particular platform. Sure, some organisations put together a "kit" with all the necessary parts to build an application - there may even be someone that packages a Clang compiler with a suitable runtime.
In general, you should be able to download the Windows SDK and get the relevant header files there - and if you use clang-cl, it should be largely compatible with the MSVC compiler [or provide clang or clang++ with the correct -fms-compatibility or whatever it is called].
Or as suggested in the other answer, use libcxx, but it's not 100% complete for Windows.
They do have a c++ standard library: libcxx.llvm.org. But it's not fully supported on the windows platform.

clang++, boost::spirit and c++11

I'm using clang++ (clang-421.0.60), packaged with Xcode 4.6, and came across an issue with boost::spirit. If I compile without any flags, everything compile fine. If I compile with '-std=c++11', then I get the following error (on including of "boost/spirit/include/qi.hpp"):
In file included from test_spirit11.cpp:1:
In file included from /usr/local/include/boost/spirit/include/qi.hpp:16:
In file included from /usr/local/include/boost/spirit/home/qi.hpp:14:
In file included from /usr/local/include/boost/spirit/home/qi/action.hpp:14:
In file included from /usr/local/include/boost/spirit/home/qi/action/action.hpp:21:
/usr/local/include/boost/spirit/home/support/action_dispatch.hpp:21:10: fatal error:
'type_traits' file not found
#include <type_traits>
The problem is that the default library used (stdlibc++) has type_traits defined as 'tr1/type_traits', whereas boost::spirit expects just 'type_traits'. I can of course fix this problem by doing:
clang++ -std=c++11 -stdlib=libc++ <...>
While I would love to use libc++, the practicality of doing so is difficult (many libraries still use and depend on stdlibc++). Thus, I am forced to not use libc++.
Does anyone have any suggestions on how to deal with this? I really wish that either more library maintainers support libc++ or that Apple provided a newer version of stdlibc++. It's been a major frustration to have access to new c++11 features, but not be able to fully use them due to lack of library support.
The problem is that the default library used (stdlibc++)
It's called libstdc++
has type_traits defined as 'tr1/type_traits', whereas boost::spirit expects just 'type_traits'.
<tr1/type_traits> is not the same thing, it's a different header entirely. boost::spirit wants the C++11 header <type_traits> which is a different header (though they do contain some similar functionality, in different namespaces.)
The problem is probably that you're using the libstdc++ that comes with Apple's ancient version of GCC (4.2) which doesn't support C++11.
If you want to use C++11 you either need to use clang with libc++ or install a newer GCC to get a newer libstdc++. Apple won't provide a newer GCC for licensing reasons, but you can install it yourself and tell Xcode how to find the headers and libs.
If you don't want to use C++11 features in boost you can disable it.
Edit boost clang.hpp to manage features.
For example to disable type_traits file not found error you can add to the end:
#define BOOST_NO_VARIADIC_TEMPLATES