Visual Studio 中的 Cygwin
我正在尝试使用 Cygwin 将我为课堂编写的旧程序从 Ubuntu 中的 KDev 移植到 Windows Visual Studio 2008,作为个人学习练习。我将包含路径配置为包含 C:\cygwin\usr\include
但它无法正确读取 .h 文件。
也就是说,我很好奇如何使用 Cygwin 在 Visual Studio 环境中使用 unix sockets.h
功能。有没有人曾经做过这个工作或者有更简单的方法来做到这一点?
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\include
but it doesn't read the .h files properly.
Namely I'm curious as to how one would go about using unix sockets.h
functionality in a Visual Studio environment using Cygwin. Has anybody ever got this working or have an easier way to go about doing this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
有多种方法可以实现这一目标,具体取决于您的具体目标。最简单的方法可能只是创建一个 Visual Studio“makefile”项目,该项目触发自定义生成命令来运行您已生成的 makefile。但这会让您无法享受 Visual Studio 作为 IDE 的许多好处,所以我猜这并不是您真正想要的。
如果您想要一个更完全集成的解决方案,您将需要做两件事。首先,您需要更改所有包含/库路径以避免 Microsoft 路径并转而使用 Cygwin 路径。您可以通过从菜单中选择“工具->选项”,然后从出现的窗口左侧的树中选择“项目和解决方案->VC++目录”来完成此操作。您可以选择更改可执行文件、标头、库等的搜索目录。对于您想要执行的操作,我建议删除所有内容并仅添加 cygwin 目录。
其次,您必须告诉 Visual Studio 使用 gcc/g++ 编译器。这有点棘手。 VS 支持自定义文件类型的自定义构建规则...但它似乎为 Microsoft 编译器硬连线了 C++。除了使用您自己的自定义文件扩展名之外,我真的不知道解决这个问题的好方法。您可以尝试 .c(C 文件)和 .cc(C++ 文件)的标准 unix 扩展名,但我怀疑 Visual Studio 会自动识别这些扩展名。你可能不得不选择一些完全陌生的东西。
如果您在解决方案资源管理器中右键单击您的项目并选择“自定义构建规则”,您将获得一个界面,可让您为所选的文件扩展名创建自定义构建规则。从那里开始,界面相对简单。
这可能无法完全满足您的需求,但它可能与您使用 Visual Studio 所能获得的效果差不多。
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.
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 everything and adding in just the cygwin directories.
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.
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.
简而言之,不要这样做。这只是浪费时间。我尝试了好几次,但总是失败。大多数情况下,我对许多链接错误感到沮丧,并且无法使用 VS 作为调试器。
您可以使用 Visual Studio 编辑和浏览源代码。这很好,因为 VS 提供了最好的 C/C++ 智能感知功能(例如,自动完成、快速转到定义/声明)。但是,在 Visual Studio 2008 中使用 cygwin 工具链非常困难。Visual Studio 2008 并非设计用于与其他工具链一起使用。具体来说,您需要更改 (1) 标头、(2)库、(3) 编译器和 (4) 链接器。然而,这通常非常困难,或者您需要权衡 Visual Studio 的优秀功能。
Visual Studio最强大的功能是它的调试能力,例如完全集成的调试环境和非常简单的观察窗口(例如,您可以直接在观察窗口中看到STL向量的元素)。但是,如果您要更改基本工具链,则无法执行此操作(尽管我非常怀疑甚至可以使用 Visual Studio 和 cygwin 工具链安全地构建)。
不幸的是,当前的 Visual Studio 2008 不适用于 cygwin/MinGW。
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.
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.
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).
Unfortunately, current Visual Studio 2008 is not for cygwin/MinGW.
这是一个老问题,但由于它在 Google 搜索中首先出现(对于 SO),我想分享一下,看起来最新的 Visual Studio 版本确实支持这一点。
有关说明,请参阅此博客文章:
https://blogs.msdn.microsoft.com/vcblog/2017/07/19/using-mingw-and-cygwin-with-visual-cpp-and-open-folder/
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.
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/