windows 如何在 Microsoft Visual Studio 中使用 GCC?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14768073/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use GCC with Microsoft Visual Studio?
提问by MasterGberry
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?
我正在创建一个非常大的项目(几千行),所以不想使用 Notepad++。IDE 会让它变得更容易。我有使用 Microsoft Visual Studio 的经验并且喜欢它。是否有一些简单的方法可以在 Microsoft Visual Studio 中使用 Cygwin 的 GCC?
Alternately, are there any other good Windows IDEs for GCC besides NetBeans and Eclipse? (I hate both of them with a passion.)
或者,除了 NetBeans 和 Eclipse 之外,还有其他适用于 GCC 的优秀 Windows IDE 吗?(我非常讨厌他们两个。)
采纳答案by Warren Young
There are several ways to go here:
有几种方法可以到这里:
Option 1: Create a Custom Build Tool
选项 1:创建自定义构建工具
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).
Visual Studio 2005 和更新版本将允许您注册自定义构建工具。它们告诉 IDE 如何将一种形式的.cpp
文件(例如文件)转换为另一种形式(例如.obj
文件)。
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.
据我所知,还没有人为 GCC 做过这件事。而且,自己动手需要编写 COM 代码,这可能太深了,无法仅针对单个项目深入研究。你必须有一个令人信服的理由来接受这个项目。
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.
然后您必须手动调整每个项目以告诉它使用自定义构建工具而不是默认构建工具,因为您使用的.cpp
是 Visual C++ 已经知道的文件扩展名(可能)。如果您尝试为从多个模块构建的单个可执行文件混合使用 VC++ 和 g++ 编译器,则会遇到麻烦。
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
选项 2:Makefile 项目
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
启动 Visual Studio 并说文件 > 新建项目。
在 Visual C++ 部分,选择 Makefile Project
填写 Makefile 项目向导:
- 构建命令行:make
- 清理命令:make clean
- 重建命令行: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.
如果您在项目名称之后命名了可执行文件并且它位于 Visual Studio 期望找到它的位置,则可以单独保留输出(用于调试)字段。
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 definitionsline 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.
保留其余字段,除非您知道它们是什么以及为什么要更改它们。例如,您可以选择-D
在预处理器定义行上传递一个标志以获得单独的调试和发布输出。如果你知道你想要这个,你知道如何设置它,所以我不会为了解释它而延长这个长答案。
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.
完成所有这些后,您仍然需要创建Makefile
,并将 a 添加make.exe
到您的PATH
. 与调试与发布问题一样,进入该级别的详细信息会使这个答案偏离主题。
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.
尽管这看起来很丑陋,但它仍然比创建自定义构建工具更容易。另外,你说你最终需要移植到 Unix,所以Makefile
无论如何你都需要它。
Option 3: Cross-Platform Development
选项 3:跨平台开发
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.
你说你想在某个时候把这个程序移植到 Unix,但这并不意味着你现在必须在 Windows 上使用 GCC。很可能编写您的程序,使其在 Windows 上的 Visual C++ 和 *ix 系统上的 GCC/Makefiles 下构建。
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 SConsand Bakefile.
有几种工具可以使这更容易。一个非常流行的选项是CMake,它在较新版本的 Visual Studio 中可用作安装时间选项。有许多替代方案,例如SCons和Bakefile。
回答by pal
I'm from the future.
我来自未来。
I keep (poking at) a C/C++ toolchain using Visual Codeon Win/Lin/Mac and MinGW installed from Choclatey. (This was done for my sanity - install GDB and GCC however you want)
我在 Win/Lin/Mac 上使用Visual Code和从Choclaty安装的 MinGW保持(戳)一个 C/C++ 工具链。(这是为了我的理智 - 根据需要安装 GDB 和 GCC)
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 docsseem to work.
我已经使用 MS 自己的奇怪的 JSON 生成文件通过 IntelliSense 使用 GCC 和 GDB 运行它。总有一天,有人(你?)会写一个 Gradle 或 Python 脚本来生成这些;现在文档中的在线示例似乎有效。
It seems to require three types of JSON thing;
它似乎需要三种类型的 JSON 东西;
- a single IntelliSenseconfiguration for the whole workspace
- a Debugging Configurationentry for each binary you want to debug
- these can invoke the build tasks
- a Build Taskper-artifact
- I don't think that there's a "require" or "dependency" thingie-mah-bob; sorry
- 整个工作区的单个IntelliSense配置
- 您要调试的每个二进制文件的调试配置条目
- 这些可以调用构建任务
- 每个工件
的构建任务
- 我不认为有“要求”或“依赖”之类的东西;对不起