using Clang in windows 10 for C/C++ - c

I am currently learning C, taking a CS50 online class via EDX/Harvard. They have us using Clang inside the CS50 IDE/Sandbox which is cloud-based. They haven't explained how to use Clang outside of that tho. Thus I am wondering; How do I setup clang in windows 10 ? as well as for use with VisualStudio Code?

On Windows, Clang is not self-sufficient, and is supposed to be used in combination with an other compiler: either MinGW (GCC) or MSVC. Clang is going to use the standard library (and other libraries/headers) of that compiler, since it doesn't ship with ones of its own.
If you want to use it with MSVC and have it installed, running clang-cl instead of cl should just work.
But since you mentioned VSC, I assume you don't want MSVC. Then...
If you want to use it with MinGW and have it installed, use clang --target=x86_64-w64-windows-gnu instead of gcc, and it should also just work. (That's assuming your MinGW produces 64-bit apps. Replace x86_64 with i686 if it's 32-bit.)
If you don't have MinGW yet, you can get a fresh version from MSYS2. Then you have an option to install their unofficial build of Clang instead of the regular one, which has an advantage of using --target=x86_64-w64-windows-gnu automatically (so you don't have to write it manually), but also takes up considerably more drive space, and used to be a bit unstable for me in the past.

Expanding on HolyBlackCat's answer. The simplest way to get up and running using clang is to download Visual Studio (not code) and choose the following toolsets during its installation-
Select "Desktop development with C++"
Under "Desktop development with C++" also select "C++ clang tools for windows"
Click install and clang will be usable to you through the commandline, just like the CS50 terminal. You usually won't have to worry too much about playing with extra cmdline options other than the ones cs50 has taught you.
To create a C project in VS with clang-
Create an empty C++ project (don't worry it's just named C++ but will work just fine with C - with the C compiler - not C++ compiler)
Go to project properties by right clicking on the project name and clicking properties
Set the platform toolset to LLVM - Clang in the project general config
Of course, this is not a silver bullet and will not guarantee an identical experience to development on linux. But if you're a beginner, you most likely will not notice any differences and this is a quick and easy way to get started with C dev on windows.
Also remember, once you have VS + Clang installed by following the above steps - you can also write code in VSCode (though it might need some configuration - specifically, you've to point it to the directory where the header files are) and use the terminal with clang to compile.

Related

MSVC's supported subset of C

So Microsoft's MSVC from Visual Studio 2019 doesn't support C99 (or later), but it does support C89 and some constructs from C99.
Is there an option for the GCC C-compiler (not C++-compiler) to use a standard that would guarantee that the source can also be compiled with MSVC? If it compiles with -std=iso9899:199409 -pedantic under GCC, can MSVC compile it?
Is there a reason you need to care if MSVC can compile it? GCC ("mingw") can target Windows PE object files with ABI compatible with MSVC, so users could build with that, or you could even ship them binary object files/library files to use so they don't need any tooling.
Policing your code base for compatibility with a known-broken/intentionally-broken compiler does not seem like a worthwhile activity unless you actually have reason to want to use that compiler, rather than just allowing users of that compiler to link with your code.
Pretty much the only way to you can ensure it builds with MSVC and GCC is to build the code with both toolsets. In addition to language constructs, there are a number of differences in the handling of compiler-defined preprocessor symbols, differences in what the preprocessor can handle, etc.
Personally I've been doing a lot of work getting C++ code to build with MSVC and Clang, and I've hit many minor issues that have to be fixed to get things to build with both toolsets. The C/C++ language standards help make the code portable, but you still have to run it through more than one toolset to get it to build 'cleanly'.
If you want your code to be robustly portable you also should build it for multiple architectures.
For my GitHub libraries, I build for ARM, ARM64, x86, x64 on MSVC, VS 2015 Update 3/VS 2017/VS 2019, targeting Win32 desktop, UWP, and Xbox One. I also build with clang for Windows for x86 and x64. Each one finds slightly different issues, but the end result is a lot more portable.

How can I duplicate my Linux C environment in Windows with CLION?

I've been coding (C) directly in my production environment for a casual game I'm hacking together. The environment is:
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 16.04.3 LTS
Release: 16.04
Codename: xenial
The GCC version I've been using is as followed:
gcc (Ubuntu 5.4.0-6ubuntu1~16.04.5) 5.4.0 20160609
My Windows environment that I want to make my development environment is:
Windows 10 Pro 64-bit, Intel
So, I'm not an expert on duplicating this for gcc or with C. I have CLION, which does support both C and C++, but pretty much everything out there is tailored for C++ or Visual C++ or whatever. I feel a bit outdated, but want to get a working environment so I can actually compile outside of directly in the shell.
As it stands, I use CLION to do everything, but upload the source files to the shell and then compile. I have tried to install CMake with MinGW, but have failed. Any help is much appreciated!
How can I duplicate my Linux C environment in Windows with CLION?
Since you are developing a game, which requires much more than what the standard C11 programming language defines (read n1570) you probably cannot.
The simplest approach is to install Linux on your laptop (perhaps in some VM).
Alternatively, perhaps the fellow members of your game project are all using some particular toolkit which happens to be ported to Windows (e.g. SDL, Qt, etc...). Then you might try using it.
Notice that CLION is not a compiler but an IDE (it uses an external compiler, perhaps GCC disguised as MinGW).
BTW, even on Windows (and of course on Linux also) you can (and probably should, at first) compile on the command line. You might want to use some build automation tool.

Cross-compiling from Linux to Windows with Clang

I am trying to cross-compile C applications from Linux (64 bit) to Windows (64 bit) using Clang. I read the page on cross-compilation, which wasn't too helpful.
As a simple test, I have the following code in test.c:
#include <stdio.h>
int main()
{
puts("hello world");
return 0;
}
My best guess so far is clang -o test -target x86_64-win64-?ABI? test.c. However, I have no idea what ABI Windows 64 bit uses. When I run clang with the target triple x86_64-win64-abcdefg, it seems to compile fine--that is, it finishes without error and results in something that is a somewhat-valid binary. That doesn't make any sense, considering abcdefg is definitely not a valid ABI. The resulting binary is far too big for such a small program, and Windows seems to think it's a 16-bit program (???). Disassembling it reveals references to "linux" and "gnu", so it would seem Clang is not even trying to compile for Windows.
Targeting win32 (x86_64-win32-???ABI???) results in the following error message:
test.c:1:10: fatal error: 'stdio.h' file not found
#include <stdio.h>
^
1 error generated.
This error, if I'm not mistaken, is the result of it not knowing where to look for system files. I assume Clang does store Windows header files somewhere, since it claims to be able to cross-compile; but where? If it doesn't, is there somewhere I can download them?
Is there a list of all the architectures, systems, and ABI's Clang supports somewhere? The list on the cross-compilation page is not comprehensive.
The page also suggests using -mcpu=..., but a warning suggests that is outdated. Instead, as the warning recommends, I tried -mtune=x86_64. This seems to have no effect. Is this even necessary, considering the architecture is specified in the target triple?
I have seen some literature that suggests I need lld, LLVM's experimental linker. Is this the case? I have had some issues compiling lld, and would like to avoid it if possible.
Your best option to develop Window binaries using clang is Mingw-w64, as you need more than just a compiler to compile for another system. You also need a linker for that system as well as libraries to link against, so basically you need an SDK for the platform you are targeting and Mingw-w64 comes with everything you require.
https://www.mingw-w64.org/downloads/
You can install it on a Linux system or macOS system and cross compile or you can install it directly on a Windows system and compile natively, without the requirement to have anything like the SDK of Visual Studio (VS). Actually the same code should compile with any installation of Mingw-w64 regardless of the system you are using for building it.
Please note that Mingw does not give you a POSIX API on Windows. You will have the standard C/C++ API available that every platform must support and for everything else, you have to use native Windows API just like you'd have to when developing software with VS. As not everyone may understand what I've just said, here's an example:
You can use fopen() to open a file as that is a standard C API function that every platform supports. But you cannot use open() to open a file, as that is a POSIX function defined in unistd.h and this header doesn't natively exist on Windows (not unless you have installed a POSIX subsystem which is not even available for all Windows version).
In Windows you have windows.h and instead of fopen() you can use the function CreateFile(), which despite its name does not always create a file, it can also open existing ones for reading, and then you will get a HANDLE that you need to pass to CloseHandle() once you are done with it (which is like close() on UNIX systems).
If you would like to get a POSIX-like API on Windows with no requirement of users having to install one, so you can share the same code between your Windows and Linux projects, appropriate wrappers do exist for that but that is not related to the compiler or SDK you are using. These are just Windows libraries you are liking against and that implement some fraction of the POSIX API on top of the Windows API; which sometimes comes with caveats. It's the opposite of Wine which implements most of the Windows API on top of POSIX and other native system APIs.
So you see, what makes porting C/C++ code hard is not the language itself but the libraries that act as a layer between your code and the system below it, as they differ from system to system, even between POSIX or POSIX-like systems. There are fundamental differences between Linux, FreeBSD, and macOS, despite the fact that they share a lot of the same API, too. And if you want to test your Windows binaries after the build, you either need a real Windows environment to do so or at least an emulated one like Wine does provide.
The question is a bit old, but I hope this answer will help someone else.
Hypothetically, what you need is a cross-compiler that runs on Linux but builds a Windows executable using Visual Studio provided headers and libraries. The link you provided redirects you to this page which tells you how to generate one. Hypothetically, you can build LLVM version 7 like this:
cmake -G Ninja -H. -B../_bin \
-DLLVM_ENABLE_PROJECTS="llvm;clang;lld" \
-DCMAKE_INSTALL_PREFIX=/opt/llvm-win32 \
-DLLVM_TARGETS_TO_BUILD=X86 \
-DCMAKE_BUILD_TYPE=Release \
-DCLANG_TABLEGEN=/mnt/data/projects/llvm-org/dl/clang-tblgen-7 \
-DLLVM_TABLEGEN=/mnt/data/projects/llvm-org/dl/llvm-tblgen-7 \
-DLLVM_DEFAULT_TARGET_TRIPLE=i686-windows-msvc \
-DLLVM_TARGET_ARCH=i686 \
-DLLVM_TARGETS_TO_BUILD=X86 \
You may have to tweak clang/lib/Driver/ToolChains/MSVC.cpp source file so the shell separator is changed from ; to : because on Unix shells, colon is used for separating multiple paths in variable.
One more thing. LLD defaults to Windows 7 in COFF format. You may either statically or creatively modify Configuration::MajorOSVersion and Configuration::MinorOSVersion in lld/COFF/Config.h if you like to compile for an older Windows. Check Windows Versions for the correct version you like to support. Perhaps, an environment variable OS_VER could carry this information.
But even then, that's just the first step. You still would need Visual Studio installed somewhere. This is where LLVM/clang will look for even basic headers like stdio.h or libcmt.lib. Hypothetically, if you have a Visual Studio 6 or later installed somewhere you could use that. Just make sure to rename all the files and directories to small-case (again, because Linux).
After that, triggering the compiler is simple. Something like:
export VCINSTALLDIR=/mnt/data/projects/VC98
__inc="${VCINSTALLDIR}/include"
__inc="${__inc}:${VCINSTALLDIR}/atl/include"
__inc="${__inc}:${VCINSTALLDIR}/mfc/include"
export INCLUDE=${__inc}
__lib="${VCINSTALLDIR}/lib"
__lib="${__lib}:${VCINSTALLDIR}/mfc/lib"
export LIB="${__lib}"
export OS_VER="4.10" # Windows 98
exec /opt/win32/bin/clang \
-fms-compatibility \
-fms-extensions \
-fmsc-version=1200 \
-fuse-ld=lld \
$*
in a shell script will do. The INCLUDE and LIB variables are honored by LLVM. Save it as something like i686-windows-msvc6-cc and you are good to go. Hypothetically, you can now invoke the compiler on your test source like:
i686-windows-msvc6-cc -o test.exe test.c
Then, boot back into Windows or load your VM and execute test.exe.
Note this is all hypothetical because MSVC terms and conditions don't allow you to fool around with their SDK like this. This could likely also be the reason why no one distributes a cross-compiler like this either. You can of course, try this hypothetically for educational purposes. Your best options is to use the one generated with MinGW instead like Mecki suggested.
I've installed mobaxterm on my windows 10 machine. There is a free version. It provides an xserver. It contains an installation of cygwin and you can start a local terminal. you just type: apt-get install clang and clang is ready to compile and finds stdio.h without complaining.
But if you intend to run the resulting executable not on mobaxterm/cygwin but inside windows itself, you need to compile with mingwin instead.

A simple explanation of what is MinGW

I'm an avid Python user and it seems that I require MinGW to be installed on my Windows machine to compile some libraries. I'm a little confused about MinGW and GCC. Here's my question (from a real dummy point of view):
So Python is language which both interpreted and compiled. There are Linux and Windows implementations of Python which one simply installs and used the binary to a execute his code. They come bundled with a bunch of built-in libraries that you can use. It's the same with Ruby from what I've read.
Now, I've done a tiny bit a of C and I know that one has a to compile it. It has its built-in libraries which seem to be called header files which you can use. Now, back in the school day's, C, was writing code in a vi-like IDE called Turbo-C and then hitting F9 to compile it. That's pretty much where my C education ends.
What is MinGW and what is GCC? I've been mainly working on Windows systems and have even recently begun using Cygwin. Aren't they the same?
A simple explanation hitting these areas would be helpful.
(My apologies if this post sounds silly/stupid. I thought I'd ask here. Ignoring these core bits never made anyone a better programmer.)
Thanks everyone.
MinGW is a complete GCC toolchain (including half a dozen frontends, such as C, C++, Ada, Go, and whatnot) for the Windows platform which compiles for and links to the Windows OS component C Runtime Library in msvcrt.dll. Rather it tries to be minimal (hence the name).
This means, unlike Cygwin, MinGW does not attempt to offer a complete POSIX layer on top of Windows, but on the other hand it does not require you to link with a special compatibility library.
It therefore also does not have any GPL-license implications for the programs you write (notable exception: profiling libraries, but you will not normally distribute those so that does not matter).
The newer MinGW-w64 comes with a roughly 99% complete Windows API binding (excluding ATL and such) including x64 support and experimental ARM implementations. You may occasionally find some exotic constant undefined, but for what 99% of the people use 99% of the time, it just works perfectly well.
You can also use the bigger part of what's in POSIX, as long as it is implemented in some form under Windows. The one major POSIX thing that does not work with MinGW is fork, simply because there is no such thing under Windows (Cygwin goes through a lot of pain to implement it).
There are a few other minor things, but all in all, most things kind of work anyway.
So, in a very very simplified sentence: MinGW(-w64) is a "no-frills compiler thingie" that lets you write native binary executables for Windows, not only in C and C++, but also other languages.
To compile C program you need a C implementation for your specific computer.
C implementations consist, basically, of a compiler (its preprocesser and headers) and a library (the ready-made executable code).
On a computer with Windows installed, the library that contains most ready-made executable code is not compatible with gcc compiler ... so to use this compiler in Windows you need a different library: that's where MinGW enters. MinGW provides, among other things, the library(ies) needed for making a C implementation together with gcc.
The Windows library and MSVC together make a different implementation.
MinGW is a suite of development tools that contains GCC (among others), and GCC is a C compiler within that suite.
MinGW is an implementation of most of the GNU building utilities, like gcc and make on windows, while gcc is only the compiler. Cygwin is a lot bigger and sophisticated package, wich installs a lot more than MinGW.
The only reason for existence of MinGW is to provide linux-like environment for developers not capable of using native windows tools. It is inferior in almost every respect to Microsoft tooolchains on Win32/Win64 platforms, BUT it provides environment where linux developer does not have to learn anything new AND he/she can compile linux code almost without modifications. It is a questionable approach , but many people find that convenience more important than other aspects of the development .
It has nothing to do with C or C++ as was indicated in earlier answers, it has everything to do with the environment developer wants. Argument about GNU toolchains on windows and its nessessety, is just that - an argument
GCC - unix/linux compiler,
MinGW - approximation of GCC on Windows environment,
Microsoft compiler and Intel compiler - more of the same as names suggest(both produce much , much better programs on Windows then MinGW, btw)

C compiler from where?

I want to learn C language (is this something good ?) and i didn't know from where i can download the language to my PC ?
and are this FREE or must pay for ?
Is C a good language? Definitely. Is it the best first language? Depends.
If you are using Windows, you can download Visual C++ 2008 Express Edition SP1 from Microsoft for free.
On Ubuntu, just run
sudo apt-get install build-essential
On Mac OS X, install Xcode from Snow Leopard/Leopard DVD (or download the latest version from Apple developer Web site)
There are quite a few free C compilers for the PC.
As seen above, MS Visual Studio comes in a free version.
However, most introductory C programming materials will work best in a unix-like environment. Two options for such an environment are:
Cygwin, which provides a unix-like environment that can be installed over a windows system.
MinGW32/MSYS, which natively ports GCC and some unix-like development tooling onto Windows, allowing you to use GCC to build native Win32 apps.
For learning you might be better off running a native unix/linux environment. If you already have Windows and don't want to uninstall or dual-boot you can run this under a VM. Several free hypervisors are available that will let you do this.
If you want to use a different development environment you could try Eclipse.
Just go to this link and look for Eclipse IDE for C/C++ Developers (79 MB).
http://www.eclipse.org/downloads/
There is a free compiler called gcc that will compile C code. On Mac OS X and Linux you probably already have it, try typing gcc at a command prompt.
On Windows, you can still use gcc, but you need to use either Cygwin or Mingw.
Or if you want to use an IDE and Microsoft's C compiler you can get a free version of Visual Studio here.
You can use Dev C++ . Very decent tool for beginners and intermediates.
OK (all free):
For Windows
- there is Visual C++ Express
- MinGW (and is command-line based)
You will need the MS Platform SDK as well.
Linux/Sun
- GCC (there are a number of ways to get this distro depending)
OS X
- Apple's Developer Tools (Xcode and others)
It is definitely free to learn and program C, but the answer to your first question "is this something good ?" depends on what your goals are. C is a very good language for some things, but not everything.
System programming is almost always done in C, along with network programs and some applications. C is also the basis for most modern programming languages you will work with, so learning the C syntax can be applied as you go about learning other things. However, if you are looking to make a interactive webpage, you might want to learn PHP. If you are looking to make a desktop application with a GUI, you might want to learn Java.
If you want to just get a start learning about programming, C can help you with that. If that's what you want to do, and dont care much about application right now, I suggest you go to the bookstore and just find a book on learning C for beginners. It should have a CD in the back with a compiler (probably visual studio), and should get you on your way.
This is a an excellent reference of free compilers for many systems.
http://www.thefreecountry.com/compilers/cpp.shtml
Intel provides free non-commercial compilers for Linux. The download includes the excellent Intel debugger & profiler. The free license can be summed up in two points:
My use of software products is for personal non-commercial purposes.
I understand that technical support will be provided by community self-help and user forums (via the Software Support link above), but cannot get committed support with a non-commercial license.
For the projects I work on, I personally prefer Intel Compilers over GNU... Intel seems to do a better job of optimization.
On Windows, I'll suggest Dev CPP. This is free an a very good product. It is also easier for the newbies to learn. I used it a lot. You can download the latest from
http://www.bloodshed.net/devcpp.html
If in Linux, there should be gcc. Use any editor of your choice( In my case vim). Just type vim filename.c in the terminal. This should bring the editor. press 'i' and write in the code. then press 'Esc' followed by ':' and x (This will save the file and exit the editor.
Now type gcc filename.c at the terminal. this should compile it. Now enter ./a.out to execute it.
If you wanted a "portable" compiler, Tiny C Compiler is a decent compiler that you can take with you on a USB stick - it's only a single .exe file or a single folder IIRC. It is cross platform as well, but the biggest downsides are that the warnings are lacking and that it's optimization isn't as good as the bigger compilers out there.
Nonetheless, it's a decent compiler to "play around with" if you don't want to install Visual Studio or Cygwin on Windows.
I think you need to be clear about the distinction between C and C++ before you decide what to do.
On Windows, try either Digital Mars C and C++ compilers or Open Watcom C and C++ products
About.com maintains a large list of c compilers for windows at http://cplus.about.com/od/glossary/a/compilers.htm

Resources