C++ Visual Studio 中的 Cygwin

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1926311/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 21:36:51  来源:igfitidea点击:

Cygwin in Visual Studio

c++visual-studio-2008socketscygwin

提问by WarrenB

I'm trying to port an old program I wrote for class from KDev in Ubuntu to Windows Visual Studio 2008 using Cygwin as a personal learning exercise. I have the include path configured to include C:\cygwin\usr\includebut it doesn't read the .h files properly.

我正在尝试将我为课堂编写的旧程序从 Ubuntu 中的 KDev 移植到 Windows Visual Studio 2008,使用 Cygwin 作为个人学习练习。我将包含路径配置为包含,C:\cygwin\usr\include但它没有正确读取 .h 文件。

Namely I'm curious as to how one would go about using unix sockets.hfunctionality in a Visual Studio environment using Cygwin. Has anybody ever got this working or have an easier way to go about doing this?

即我很好奇人们将如何sockets.h使用 Cygwin 在 Visual Studio 环境中使用unix功能。有没有人做过这个工作或有更简单的方法来做这件事?

采纳答案by Russell Newquist

There are several ways to go about this that could be made to work, depending upon your exact goals. The simplest way is probably just to create a Visual Studio "makefile" project that fires off a custom build command to run a makefile you've built. But that keeps you away from a lot of the nice benefits of Visual Studio as an IDE, so I'm guessing that's not really what you're after.

有几种方法可以解决这个问题,具体取决于您的确切目标。最简单的方法可能只是创建一个 Visual Studio“makefile”项目,该项目触发自定义构建命令来运行您构建的 makefile。但这使您无法享受 Visual Studio 作为 IDE 的许多好处,所以我猜这并不是您真正想要的。

If you want a more fully integrated solution, you're going to need to do two things. First of all, you're going to need to change out all of your include/library paths to avoid the Microsoft ones and go after the Cygwin ones instead. You can do this by selecting "Tools->Options" from the menu, then choosing "Projects and Solutions->VC++ Directories" from the tree on the left hand side of the window that comes up. You'll have options to change the search directories for executables, headers, libraries, etc. For what you're trying to do, I'd suggest removing everythingand adding in just the cygwin directories.

如果您想要一个更完全集成的解决方案,您将需要做两件事。首先,您将需要更改所有包含/库路径,以避免使用 Microsoft 的路径,转而使用 Cygwin 的路径。您可以通过从菜单中选择“工具”->“选项”,然后从出现的窗口左侧的树中选择“项目和解决方案->VC++ 目录”来完成此操作。您可以选择更改可执行文件、头文件、库等的搜索目录。对于您要执行的操作,我建议删除所有内容并仅添加 cygwin 目录。

Second, you'll have to tell Visual Studio to use the gcc/g++ compiler. This is a bit trickier. VS supports custom build rules for custom file types... but it seems to have C++ hardwired in for the Microsoft compiler. I don't really know a great way around that except to use your own custom file extension. You can try the standard unix extensions of .c (C files) and .cc (C++ files), but I suspect Visual Studio will automatically pick up on those. You may have to go with something totally foreign.

其次,您必须告诉 Visual Studio 使用 gcc/g++ 编译器。这有点棘手。VS 支持自定义文件类型的自定义构建规则……但它似乎为 Microsoft 编译器硬连接了 C++。除了使用您自己的自定义文件扩展名之外,我真的不知道有什么好的方法可以解决这个问题。您可以尝试 .c(C 文件)和 .cc(C++ 文件)的标准 unix 扩展名,但我怀疑 Visual Studio 会自动选择这些。你可能不得不选择完全陌生的东西。

If you right click on your project in the Solution Explorer and select "Custom Build Rules" you'll be given an interface that will let you create your custom build rules for the file extension you've chosen. The interface is relatively straightforward from there.

如果您在解决方案资源管理器中右键单击您的项目并选择“自定义构建规则”,您将获得一个界面,让您可以为您选择的文件扩展名创建自定义构建规则。从那里开始,界面相对简单。

This might not get you exactly what you wanted, but it's probably about as close as you're going to get with Visual Studio.

这可能无法完全满足您的需求,但它可能与您将使用 Visual Studio 获得的效果非常接近。

回答by minjang

Simply speaking, don't do that. It would be just waste of time. I tried it several times, but always failed. Mostly, I was frustrated by many linking errors, and also was unable to use VS as a debugger.

简单地说,不要那样做。那只会是浪费时间。我尝试了几次,但总是失败。大多数情况下,我对许多链接错误感到沮丧,也无法将 VS 用作调试器。

You can use Visual Studio for editing and browsing source code. It is nice because VS provides the best C/C++ intellisense features (e.g., Auto completion, fast go to definition/declaration). But, it is very hard to use cygwin tool chains with Visual Studio 2008. Visual Studio 2008 is not designed to work with other tool chains.Specifically, you need to change (1) headers, (2) libraries, (3) compiler and (4) linker. However, it is generally very hard, or you need to trade off with the nice features of Visual Studio.

您可以使用 Visual Studio 来编辑和浏览源代码。这很好,因为 VS 提供了最好的 C/C++ 智能感知功能(例如,自动完成、快速转到定义/声明)。但是,将 cygwin 工具链与 Visual Studio 2008 一起使用非常困难。Visual Studio 2008 并非设计为与其他工具链一起使用。具体来说,您需要更改 (1) 头文件、(2) 库、(3) 编译器和 (4) 链接器。但是,这通常非常困难,或者您需要权衡 Visual Studio 的出色功能。

The strongest feature of Visual Studio is its debugging ability such as fully integrated debugging environment and very easy watch windows (e.g., you can see STL vector's element directly in watch windows). However, you can't do this if you would change fundamental tool chain (although I am very suspicious it is even possible to safely build with Visual Studio and cygwin tool chains).

Visual Studio 最强大的功能是它的调试能力,例如完全集成的调试环境和非常简单的观察窗口(例如,您可以直接在观察窗口中看到 STL 向量的元素)。但是,如果您要更改基本工具链,则不能这样做(尽管我非常怀疑它甚至可以使用 Visual Studio 和 cygwin 工具链安全地构建)。

Unfortunately, current Visual Studio 2008 is not for cygwin/MinGW.

不幸的是,当前的 Visual Studio 2008 不适用于 cygwin/MinGW。

回答by John Thoits

This is an old question, but since it comes up first (for SO) on a Google search I wanted to share that it looks like the latest Visual Studio versions do support this.

这是一个老问题,但由于它首先出现在 Google 搜索中(对于 SO),我想分享一下,它看起来像最新的 Visual Studio 版本确实支持这一点。

For instructions, refer to this blog post: https://blogs.msdn.microsoft.com/vcblog/2017/07/19/using-mingw-and-cygwin-with-visual-cpp-and-open-folder/

有关说明,请参阅此博客文章:https: //blogs.msdn.microsoft.com/vcblog/2017/07/19/using-mingw-and-cygwin-with-visual-cpp-and-open-folder/