Visual Studio 中的 Cygwin

2021-12-11 00:00:00 sockets cygwin visual-studio-2008 c++

我正在尝试将我为课堂编写的旧程序从 Ubuntu 中的 KDev 移植到 Windows Visual Studio 2008,使用 Cygwin 作为个人学习练习.我将包含路径配置为包含 C:cygwinusrinclude 但它没有正确读取 .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:cygwinusrinclude but it doesn't read the .h files properly.

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

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?

推荐答案

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

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.

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

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.

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

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.

这可能无法完全满足您的需求,但它可能与您使用 Visual Studio 获得的效果差不多.

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.

相关文章