How to use GCC with Microsoft Visual Studio?

101,457

Solution 1

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

  1. Start Visual Studio and say File > New Project.

  2. In the Visual C++ section, select Makefile Project

  3. 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.

Solution 2

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:

  1. Use Clang/LLVM; “Clang for Windows” includes instructions to install Clang/LLVM as a platform toolset in Visual Studio.
  2. Use Clang to target Windows with Clang/C2 (Clang frontend with Microsoft Code Generation). enter image description here

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.

enter image description here

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/

Solution 3

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
Share:
101,457
MasterGberry
Author by

MasterGberry

Updated on July 09, 2022

Comments

  • MasterGberry
    MasterGberry almost 2 years

    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.)

  • Ben Voigt
    Ben Voigt over 11 years
    I'm pretty sure you can use the nmake tool supplied with Visual C++ and give it a rule for building with gcc. No need to find a third-party make.
  • Warren Young
    Warren Young over 11 years
    @BenVoigt: Sure, but Makefiles written for MS nmake (as opposed to Bell Labs nmake) won't be portable to Unix, as the OP wants. MS nmake interprets commands as cmd.exe does, whereas POSIX make uses /bin/sh by default. So, an rm command in your clean target for a *ix Makefile has to be del in an nmake .mak file. On top of that, nmake is crippled compared to GNU and BSD make, which have taken over the *ix world. Plus, the OP already specified that he's using Cygwin.
  • user2023370
    user2023370 over 8 years
    @WarrenYoung Following Option 2, is it possible to step debug? I added my hello.cpp to the Source Files directory within Visual Studio, added a breakpoint, and hit F5. I am then informed that the exe and relevant cygwin dlls were built without symbols; while for Windows dlls: Cannot find or open the PDB file.
  • Warren Young
    Warren Young over 8 years
    @user2023370: First, this is really a separate question, having to do with Visual C++, not Unix, Makefiles, or portability. At minimum, you need to add /g to your command line options to get a debug build. You might need a non-default /M option, too; RTFM. Second, trying to mix Visual C++ and Cygwin is probably a losing proposition from the start, if only because of the GPL license involved. If you want to use Cygwin, use Cygwin. If you want to use Visual C++ for Windows development instead, don't try to link in Cygwin.
  • Denise Skidmore
    Denise Skidmore almost 8 years
    CMake may help set up links and include folders for cross compile, but will not prevent you from using language features or libs that don't cross compile.
  • Royi
    Royi over 7 years
    Any update to that answer given Visual Studio 2015 / 2017?
  • mireazma
    mireazma over 3 years
    Option 2 -- creating new Makefile project, no wizard shows in msvs 2019.
  • IInspectable
    IInspectable almost 3 years
    The question is asking about Visual Studio. This proposed answer talks about Visual Studio Code. As such, it isn't.
  • pal
    pal almost 3 years
    @IInspectable i'm afraid you're mistaken