Why can't I compile this program with clang? - c++

I have downloaded Windows snapshot build from here. I just run that executable file & Write following simple C++ program
#include <iostream>
int main()
{
std::cout<<"Welcome to Clang\n";
}
clang++ 999.cpp
999.cpp:1:10: fatal error: 'iostream' file not found
#include <iostream>
^
1 error generated.
What is going wrong here? What I am missing? What should I do so that I can successfully compile my C & C++ programs on Windows using clang?

There's no windows platform support for clang's C++ standard library: http://libcxx.llvm.org/
This is why you can' t simply compile this code in windows: it doesn't find the headers you're referring to.
Edit:
Following Tom's remark, you can however give clang a build target:
clang -target i686-pc-windows-win32 ....
When a win32 target is given, clang will use msvc's library if it's installed, and if the environment variable INCLUDE was not set to another location.
Note however that llvm website gives a disclaimer about it:
Clang mostly works on Windows, but does not currently understand all
of the Microsoft extensions to C and C++. Because of this, clang
cannot parse the C++ standard library included with Visual Studio, nor
parts of the Windows Platform SDK. However, most standard C programs
do compile.

You need to have Visual Studio installed.
Clang uses its libs and headers.

Related

Problem with using clang as compiler for code::blocks

I installed llvm for clang, because I wanted to use clang for code::blocks as compiler, since I need compiler that supports c++20, so I installed llvm, the bin was added in environmental variables, even the code::blocks detected llvm as compiler, however I get error when i want to compile my code:
-------------- Build file: "no target" in "no project" (compiler: unknown)---------------
clang++.exe -c C:\Users\Temirlan\labs\lab4\rpn.cpp -o C:\Users\Temirlan\labs\lab4\rpn.o
clang++.exe -o C:\Users\Temirlan\labs\lab4\rpn.exe C:\Users\Temirlan\labs\lab4\rpn.o
C:\Users\Temirlan\labs\lab4\rpn.cpp:180:10: fatal error: 'iostream' file not found
#include <iostream>
^~~~~~~~~~
1 error generated.
Do you know what is the problem or maybe the picture will help?
photo of compiler executables in code::blocks
I got error of "fatal error: 'iostream' file not found"
All modern compilers support C++20 (to slightly varying extent): both Clang, GCC, and MSVC. So this shouldn't affect your choice (but I do think that Clang is the best option).
Clang can be set up in different ways: (in order of personal preference)
With GCC's standard C++ library, libstdc++. Install MSYS2, then use it to install both Clang and GCC: pacman -S mingw-w64-ucrt-x86_64-gcc mingw-w64-ucrt-x86_64-clang. Then use C:/msys64/ucrt64/bin/clang++.exe as the compiler. (There's also MINGW64 variant instead of UCRT64, read about the difference here).
With its own standard C++ library, libc++. Install MSYS2, then use it to install libc++-flavored Clang: pacman -S mingw-w64-clang-x86_64-clang. Then use C:/msys64/clang64/bin/clang++.exe as the compiler.
With MSVC's standard C++ library, aka MSVC STL. Install the official Clang build, and install Visual Studio.
Note that the first two options don't involve downloading the official Clang build. The official build wants the MSVC STL by default, which you don't have, since you didn't install VS. (And if you do install it, you might as well use it instead of CodeBlocks.)
The official Clang build can be made to work with other standard libraries, but they need to be installed separately, and you need to persuade it with some compiler flags. It's easier to install the MSYS2's version, which already uses the correct flags by default.

MinGW - Header incomplete <netfw.h>

i am developing a networked application in C++ that should be able to add a rule to the windows firewall. I am following this example using the MingW g++ compiler.
The other examples are working without a problem, but this particular example won't work.
The exact problem i am facing, that NetFwRule is not defined in the <netfw.h> header provided by MinGW. When i try to compile that example with MSVC (in Visual Studio) it works flawlessly, since NetFwRule is defined in the header provided by MSVS. However it is absolutely neccessary for the project to use the MinGW g++ compiler.
This particular code snippet as shown in the example link won't work, since NetFwRule is not defined:
// Create a new Firewall Rule object.
hr = CoCreateInstance(
__uuidof(NetFwRule), // <- Problem in MinGW g++, but in MSVC it just works
NULL,
CLSCTX_INPROC_SERVER,
__uuidof(INetFwRule),
(void**)&pFwRule
);
So what needs to be done in order to make this work using MinGW?
FYI: I have MinGW 32 bit with posix thread for the i686 architecture installed.
g++ --version gives g++ (i686-posix-dwarf-rev0, Built by MinGW-W64 project) 8.1.0
The error message is: 'NetFwRule' was not declared in this scope
In fact it really is not declared, since the <netfw.h> of MinGW does not declare NetFwRule. MSVC however does.
Your GCC was too old.
You can get a newer version via MSYS' pacman, or download a standalone build, for example from http://winlibs.com/ which will work just as well if you don't need MSYS2.
Okay, I have tried to use MinGW 64 bit just as IInspectable suggested, but that does not work either - the netfw.h header still does not provide NetFwRule. However i have installed g++ along with MSYS2 (the version that rustyx is using), which just works as expected. So if anyone faces the same issue I would suggest to use MinGW along with MSYS2 and not standalone.

How to use clang for windows using mingw headers/libs

I downloaded the clang for windows binary package from the website. It provides some nice VS/MSBuild integration by allowing to build VS projects using clang instead of MSVC. However, I notice that it still uses the MSVC C Library and also the MSVC linker (link.exe). Also, including any C++ STL headers like string or iostream causes build errors.
My question is: Is it possible to use full clang/llvm toolchain along with some non-Microsoft libraries (like libc++, mingw etc.) to build a native Windows binary? Doing all of this from within VS is a bonus but even from command-line would be fine.

Easiest method to configure clang++ to use g++/gcc STL headers in Cygwin?

I'm running the most up to date versions of clang++ (v3.1) and g++/gcc (v4.7.3) in Cygwin (32-bit). Everything is using the installed default configuration. This is a fresh install of Cygwin in Windows 8.
My issue is that clang++ cannot find the installed g++ STL headers to compile my project.
#include <stdlib.h>
#include <mutex>
#include <thread>
int main() {
std::mutex myMutext;
return 0;
}
This sample code results in this error when compiled. Notice the libc stdlib.h header compiles without error. It's that isn't found. I've tried other STL headers as a test, same error.
clang++ -c -o test.o test.cpp
test.cpp:2:10: fatal error: 'mutex' file not found
#include <mutex>
^
1 error generated.
After some searching it seems the suggested options are recompiling the entire clang project and adding the header paths to its source or manually adding all the g++ STL header paths to my makefile both of which seem kind of hacky.
There has to be an easier option, right?
I don't know if you have solve it, but I have a simpler solution: just make s symlink of 4.7.3 to 4.5.3 and compile your code with -lstdc++ option, all will done.
clang++ will be build (compile-time) against specific versions of libstdc++ by default (IIRC). It will not check the system for newer versions at runtime.
The libstdc++ the clang-3.1 package of cygwin seems to use is gcc-4.5.3, as indicated by the output of clang++ -v test.cc. gcc-4.5.3 is not installed in your enviroment, however.
Your options, none of which are great:
Downgrade gcc to 4.5.3 via the setup. However your C++11 code will still not compile, due to lack of support in the libstc++ that comes with gcc-4.5.3.
Build clang yourself, against the newer gcc.
Build clang yourself, with libc++. However, it is highly unlikely Windows is (fully) supported by it.
Use gcc-4.7 instead. The Windows support is far more mature in gcc right now anyway (and don't get me started about cross-compilers).
(Aside: you'll need -std=c++11 to compile your code)

Minimal GCC C++ Compiler

I'm trying to minimize the resources required to compile and run c++ code with GCC. I downloaded DevC++'s portable version but I'm looking more towards what files do I need to ONLY run gcc via command prompt and compile against all standard libraries. I'm not interested in Windows applications, only command prompt.
Is there an already stripped version of GCC out there? And if not, would anyone be able to lend me a hand
*I know of CygWin, and MinGW already, I'm looking for the bare minimals to using the cpp compiler for GCC. Like Tiny C where the entire functioning compiler and libraries is under 200 kilobytes, I'm looking to emulate that workflow with a cpp compiler.
MinGW
MinGW, a contraction of "Minimalist GNU for Windows", is a minimalist development environment for native Microsoft Windows applications.
Primarily intended for use by developers working on the native MS-Windows platform, [...] MinGW includes:
A port of the GNU Compiler Collection (GCC), including C, C++, ADA and Fortran compilers;
GNU Binutils for Windows (assembler, linker, archive manager)
A graphical and a command-line installer for MinGW and MSYS deployment on MS-Windows