How to use CMAKE for C++11 on windows? - visual-studio

My C++11 project is currently using CMAKE, XCODE, CLANG on OSX. I wish to compile this code on Windows.
Plan is to use the same cmake settings files on windows. Best case would be to use CMAKE to generate VS projects which uses Clang or gcc for C++11 .
Seems to me, that Visual Studio is just not going to fully support C++11 for a while. So we should all try to find a general solution for cross platform C++11.
How would one use CMAKE to generate projects/makefiles which would compile C++11 code on windows?

CMake's Visual Studio generator will always use the cl compiler of Visual C++.
What you request would require writing a new Generator for CMake. That is, the problem cannot be solved by writing a clever CMakeLists.txt, but has to be solved by adding a feature to the CMake core binary itself. I agree that this could be useful once Clang achieves a suitable level of Windows support, but at the point of this writing, it is probably too early for that.
You might want to take a look at the experimental compile-features mechanism for CMake. This is not yet part of CMake 3.0, but is planned to be integrated with one of the next releases. The idea is that you just specify which C++11 features you need and CMake takes care of configuring the compiler accordingly (or gives an error if the compiler does not support the feature at all).

You can create VS projects which use clang by specifying the LLVM toolset when you generate.
http://public.kitware.com/Bug/view.php?id=14863
Eg:
cmake.exe .. -T LLVM-vs2010

Related

CUDA: How to link a specific obj, ptx, cubin from a separate compilation?

I have a fairly large CUDA/C++ project that compiles to a static library. The toolchain is CUDA Toolkit 9.0/9.2 and VS 2017. I cannot change the company toolchain. Our most expensive kernel was hit by a nvcc compiler regression introduced in the 9.0 Toolkit. I have filed this with the Nvidia developer's website, and received confirmation of the regression. That was about a year ago, and the ticket is still open. Maybe the 10.0 Toolkit will fix it.
But I cannot wait. So my plan is to compile just this one specific kernel using the 8.0 nvcc compiler and v140 (VS 2015) compiler. It is a single .hpp file with __device__ decorator for the kernel declaration, and a .cu file with the definition. The kernel does not call other kernels; it is a rather simple kernel.
From the v140 Native Tools Command Prompt, I executed:
nvcc -x cu -arch=sm_61 -dc kernel.cu
And obtained a kernel.obj file. I have read the NVCC documentation on CUDA Compiler Driver NVCC. I confess to not entirely understanding. There are several compilation phases, and I do not see which is the correct course for my case.
My question is how to link this object file into my greater static library? If someone could point me to the correct series of commands, or better yet, how to include this into the VS Project, presumably with kernel.hpp and kernel.obj, I would be most grateful.
Following Njuffa's comment above, the simplest solution is create a static library using the earlier, performant toolchain for that kernel (VS 2015 & CUDA 8.0 Tookit). Then link that library into the greater project with the later toolchain. I did so with success.
I created a CUDA 8.0 template project in VS 2015 with only the kernel source and header. The compilation target set to static library. This created a .lib file. The .lib file and header are then added to the C++ linker settings of the greater project, using VS 2017 and CUDA 9.0. All test executables using this static library pass. This is a much simpler solution than trying to recompile using an intermediate compilation format ( ptx, cubin, etc.)
Although ultimately, the real solution was to refactor the kernel to use shared memory more efficiently, negating the need for the older nvcc version.

How to configure nvidia CUDA for VIsual Studio 2017 [duplicate]

Visual Studio 2017 RC includes much tighter CMake integration, allowing one to skip the intermediate step of generating project/solution files and use CMake effectively as the project file itself. There is sufficient documentation from Microsoft for using these features with regular C++ files, and there is sufficient documentation on this website (example) for making CUDA and Cmake play nicely, when it comes to linking CUDA code to C++ code.
What I can't find information on is how to make CMake, Visual Studio 2017 RC, and CUDA 8.0 all play nicely. This is a difficult problem, because 2017RC has no integration for the CUDA SDK anyways, and I was hoping to use 2017RC so that my C++ interface to the CUDA code could use C++14 and/or C++17. I'm working on the beginning of a large project that will primarily involve writing a static CUDA library that is accessed through C++: so, I'd like to get the CMake to take care of compiling my CUDA sources into a static library, and for it to help with feeding the linking information to Visual Studio. So far, I haven't had any success with using FindCUDA's various features to accomplish this, but I'm assuming that's due to a misunderstanding on my part. I've read through the documentation on separable compilation from Nvidia, but that wasn't helpful for figuring out CMake.
Further, whenever I try to use CMake in VS2017RC, I still end up with the various vcxproj files that CMake likes to spit out. Is this due to an error on my part? How do I edit the build command arguments, or CMakeLists.txt, to get the functionality demonstrated here to work?
The very short (and only at the time of writing) answer is that you can't. CUDA 8 doesn't support VS2017. Only VS2015 is presently supported.
You can always find the compiler/IDE versions which the release version of CUDA supports here
Edit to add that the CUDA 9 release will add official support for VS2017.
All you need to do is set the CUDA_HOST_COMPILER variable to a supported compiler for example the visual studio 2015 compiler.
In my case this is:
C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/amd64/cl.exe
As both runtime libraries are binary compatible you can use the 2015 compiler within CUDA and compile all the rest of the application with the 2017 compiler.

How to use GCC with Microsoft Visual Studio?

I am creating a very large project (a few thousand lines) and so would rather not use Notepad++. An IDE would make it so much easier. I have experience with Microsoft Visual Studio and love it. Is there some easy way to use Cygwin's GCC from within Microsoft Visual Studio?
Alternately, are there any other good Windows IDEs for GCC besides NetBeans and Eclipse? (I hate both of them with a passion.)
There are several ways to go here:
Option 1: Create a Custom Build Tool
Visual Studio 2005 and newer will let you register custom build tools. They tell the IDE how to transform files of one form (e.g. a .cpp file) into another form (e.g. an .obj file).
So far as I know, no one has done this yet for GCC. And, doing it yourself requires writing COM code, which is probably too deep a pool to dive into just for a single project. You'd have to have a compelling reason to take this project on.
You then have to manually adjust each project to tell it to use the custom build tool instead of the default, since you're using a file name extension (.cpp, probably) that Visual C++ already knows about. You'll run into trouble if you try to mix the VC++ and g++ compilers for a single executable built from multiple modules.
On the plus side, if you were looking to start an open source project, this sounds like a good one to me. I expect you'd quickly gather a big user base.
Option 2: Makefile Project
Start Visual Studio and say File > New Project.
In the Visual C++ section, select Makefile Project
Fill out the Makefile Project Wizard:
Build command line: make
Clean commands: make clean
Rebuild command line: make clean all
You can leave the Output (for debugging) field alone if you've named your executable after the project name and it lands where Visual Studio expects to find it.
Leave the rest of the fields alone unless you know what they are and why you want to change them. As an example, you might choose to pass a -D flag on the Preprocessor definitions line to get separate debug and release outputs. If you know you want this, you know how to set it up, so I'm not going to make this long answer even longer in order to explain it.
You'll be asked the same set of questions for the Release build. If you want to bother with separate debug and release builds, you'd make any changes here.
Having done all this, you still have to create the Makefile, and add a make.exe to your PATH. As with the debug vs. release question, going into that level of detail would push this answer off topic.
As ugly as this looks, it's still easier than creating custom build tools. Plus, you say you need to port to Unix eventually, so you're going to need that Makefile anyway.
Option 3: Cross-Platform Development
You say you want to port this program to Unix at some point, but that doesn't mean you must use GCC on Windows now. It is quite possible to write your program so that it builds under Visual C++ on Windows and GCC/Makefiles on *ix systems.
There are several tools that make this easier. One very popular option is CMake, which is available as an installation time option in newer versions of Visual Studio. There are many alternatives such as SCons and Bakefile.
Clang
You can use the Clang compiler with Visual Studio to target Android, iOS, and Windows.
If you are targeting Android, you can use the Clang/LLVM compiler that ships with the Android NDK and toolchain to build your project. Likewise, Visual Studio can use Clang running on a Mac to build projects targeting iOS. Support for Android and iOS is included in the “Mobile Development with C++” workload. For more information about targeting Android or iOS check out our posts tagged with the keywords “Android” and “iOS”.
If you are targeting Windows, you have a few options:
Use Clang/LLVM; “Clang for Windows” includes instructions to install Clang/LLVM as a platform toolset in Visual Studio.
Use Clang to target Windows with Clang/C2 (Clang frontend with Microsoft Code Generation).
GCC
If your project targets Linux or Android, you can consider using GCC. Visual Studio’s C++ Android development natively supports building your projects with the GCC that ships with the Android NDK, just like it does for Clang. You can also target Linux – either remotely or locally with the Windows Subsystem for Linux – with GCC.
Check out our post on Visual C++ for Linux Development for much more info about how to use Visual Studio to target Linux with GCC. If you are specifically interested in targeting WSL locally, check out Targeting WSL from Visual Studio.
Source: https://devblogs.microsoft.com/cppblog/use-any-c-compiler-with-visual-studio/
I'm from the future.
I keep (poking at) a C/C++ toolchain using Visual Code on Win/Lin/Mac and MinGW installed from Choclatey.
(This was done for my sanity - install GDB and GCC however you want)
I've run it with GCC and GDB with IntelliSense using MS's own weird JSON makefiles.
Someday, someone (you?) will write a Gradle or Python script to generate these; for now the examples online in the docs seem to work.
It seems to require three types of JSON thing;
a single IntelliSense configuration for the whole workspace
a Debugging Configuration entry for each binary you want to debug
these can invoke the build tasks
a Build Task per-artifact
I don't think that there's a "require" or "dependency" thingie-mah-bob; sorry

CLang libc, libc++ on Windows with debugging symbols compatible with Visual Studio

I'm trying to find info and I don't see it on clang web site.
I'm thinking to try to use it on windows, but I have no clue if it has it's own libc or it uses broken libc from MS?
another question: if i compile code with clang, will I be able to use visual studio as a debugger, e.g. is clang capable of emitting debugging symbols in MS format (this is the reason I don't want to use gcc; and this is something that intel compiler can do, but it uses MS's libc).
In short, I'd like to be able to use visual studio as a debugger, but I need at the same time decent real c compiler with normal lib c.
or, perhaps, there are commercial alternatives. I've read that dinkum sells commercial libc for Win32 and others, but I have no clue what's the price and how to get it.
You have asked two completely different questions. I will answer the one about using Visual Studio as a debugger.
This is not currently possible. Microsoft has not released any documentation or code necessary to produce files in their PDB format, which is what Visual Studio consumes. There has been some reverse engineering efforts, but results of those have not yet made their way into general Open Source tools.
Neither GCC nor Clang are capable of producing PDB files, and hence do not work with Microsoft's debugger. Some of the commercial compilers have support for generating or consuming PDB, but not the Free/Open compilers like GCC and Clang.
You can use other IDEs on Windows which support the DWARF debugging format, used by GCC and Clang. Such compilers include Code::Blocks and Eclipse CDT.

gcc compiled code on visual studio

Assume I have source code for a lib/tool that works on gcc compiler. Can I use the same code and compile it in visual studio. will that be possible at all? If it is possible, how do I do it?
if you are just using all the standard C/C++ library like stdio.h, stdlib.h, etc. it should work fine. Pure console program should work fine. If you used some GUI related library (especially if you are porting over from unix to window) then it might not work.
To do so, you can simply just create a new project in visual studio and add the existing source code into the project workspace. Compile it, if you encounter any error, just post here or try solve if you know how
It depends on your code, GCC support a variant of C (C99) which Visual Studio doesn't support yet.
If your trying to compile a Unix program on Windows you best bet will be to use Cygwin.
Check this question for pointers on using Cygwin in Visual Studio.

Resources