使用Cygwin编译C程序; 执行错误
我正在攻读计算机科学硕士课程。 课程使用C语言,如果我们使用Windows,老师希望我们使用Cygwin来编译程序。
我已下载并安装了 Cygwin 并且我已确保已安装 GCC 编译器。
但我不知道从这里该去哪里。 我需要编译一个具有基本包含的单个源文件。
#include <stdio.h>
让我们假设该文件位于我的桌面上(实际上不是,但为了争论)。 如何从 bash shell 导航到桌面? 我假设一旦我导航到 bash 中的正确位置,我只需执行:
gcc myProgram.c -o myProgram
更新:按照下面发布的不同说明,我能够编译该程序; 我为此感谢你。 但是当我执行生成的二进制文件时,我得到以下结果。 如何编译或执行该程序才不会出现错误? 再次谢谢你。
此应用程序无法启动,因为找不到 cygwin1.dll。 重新安装应用程序可能会解决此问题。
I'm enrolled in a masters computer science course. The course is using C and the instructor wants us to use Cygwin to compile programs if we are using windows.
I've downloaded and installed Cygwin and I've ensured that I've installed the GCC compiler.
But I don't know where to go from here. I need to compile a single source file that has a basic include.
#include <stdio.h>
Lets assume the file is on my desktop (it is not, but for the sake of argument). How do I navigate to the desktop from the bash shell? I assume once I've navigated to the correct location in bash, I simply execute:
gcc myProgram.c -o myProgram
Update: Following different instructions posted below, I was able to compile the program; I thank you for that. But when I execute the resulting binary I get the following. How can I compile or execute this program so I don't get the error? Again, thank you.
This application has failed to start because cygwin1.dll was not found. Re-installing the application may fix this problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
此文件(cygwin1.dll)是类似于 qt 依赖项的 cygwin 依赖项。您必须将此文件和此类消息错误中出现的类似文件从“cygwin/bin”复制到您的程序的文件夹中。这也是运行所必需的另一台没有 cygwin 的计算机!
This file (cygwin1.dll) is cygwin dependency similar to qt dependency.you must copy this file and similar files that appear in such messages error, from "cygwin/bin" to folder of the your program .Also this is necessary to run in another computer that have NOT cygwin!
当您在 cygwin 中启动时,您位于 $HOME 中,就像在 UNIX 中一样,默认情况下它映射到 c:/cygwin/home/$YOURNAME。 所以你可以把所有东西都放在那里。
您还可以通过 /cygdrive/c/ 从 cygwin 访问 c: 驱动器(例如 /cygdrive/c/Documents anb Settings/yourname/Desktop)。
when you start in cygwin, you are in your $HOME, like in unix generally, which maps to c:/cygwin/home/$YOURNAME by default. So you could put everything there.
You can also access the c: drive from cygwin through /cygdrive/c/ (e.g. /cygdrive/c/Documents anb Settings/yourname/Desktop).
关于您关于缺少 cygwin1.dll 的更新问题。
从 Cygwin 终端检查,
如果它不存在(我对此表示怀疑),则您的安装未正确完成。
然后,检查您的路径,
这将给出
:
分隔的路径列表。 它必须包含/usr/bin
。 如果您发现缺少添加它,最后,
Regarding your updated question about the missing cygwin1.dll.
From the Cygwin terminal check,
If it is not present (I doubt that), your installation is not properly done.
Then, check your path with,
This will give
:
separated list of paths. It MUST contain/usr/bin
. If you find that missing add it with,Finally,
总而言之,以下是一些使用 Cygwin 和 Windows Vista 导航到目录并编译代码的命令:
在提示符下,使用
cd
更改到适当的目录:$ cd /cygdrive/c/Users/nate/Desktop
使用
ls
列出目录中的文件:$ls
prog.c
使用
gcc
命令编译文件在此目录中:$ gcc prog.c -o prog
如果不这样做看到任何错误,您应该能够运行生成的程序:
$ ./prog
更新:
对于“
Cygwin1 .dll 未找到
”错误,我喜欢 Nik 的回答。 您还可以查看 找不到有关 cygwin1.dll 的相关帖子,建议将c:\cygwin\bin\
添加到您的 Windows 路径。对于 Windows XP 和 Vista,有有关如何更改 Windows PATH 变量的说明我认为这是相似的。
c:\cygwin\bin
添加到列表中,确保用分号将其与之前的任何项目分隔开Just to summarize, here are some commands that navigate to a directory and compile code using Cygwin and Windows Vista:
At the prompt, use
cd
to change to the appropriate directory:$ cd /cygdrive/c/Users/nate/Desktop
Use
ls
to list the files in the directory:$ ls
prog.c
Use the
gcc
command to compile a file in this directory:$ gcc prog.c -o prog
If you don't see any errors, you should be able to run the resulting program:
$ ./prog
Update:
For the "
Cygwin1.dll not found
" error, I like Nik's answer. You might also check out this related post about cygwin1.dll not found, which suggests addingc:\cygwin\bin\
to your Windows PATH.There are instructions on how to change the Windows PATH variable for Windows XP, and on Vista I think it's similar.
c:\cygwin\bin
to the list, making sure to separate it from any previous items with a semicolon查找(即
cd
到)通常是您的
C:\
另请参阅 使用 Cygwin,Lifehacker 介绍(2006 年 6 月) 以及PhysioNet 上的生物医学页面。
Look for (that is,
cd
to)that will usually be your
C:\
Also look at Using Cygwin, the Lifehacker introduction (June/2006) and, this biomed page at PhysioNet.
关于
cygwin1.dll
not found 错误,我使用了至少 8 年的解决方案是将 Cygwin bin 目录添加到 My 中的%PATH%
的末尾电脑-> 属性-> 高级-> 环境变量。 我将它们添加到路径的末尾,因此在我的正常工作中,最后搜索它们,最大限度地减少冲突的可能性(事实上,我一直没有遇到冲突问题)。当您调用 Cygwin Bash Shell 时,这些目录会被添加到
%PATH%
之前,因此一切都可以在该环境中按预期运行。当不在 Cygwin shell 中运行时,我的
%PATH%
是:这样,例如,当我不在 Cygwin Shell 中时,首先找到 ActiveState Perl 的 perl,但是当我在 Cygwin Shell 中时,首先找到 Cygwin perl在 Cygwin Shell 中工作。
Regarding the
cygwin1.dll
not found error, a solution I have used for at least 8 years is to add the Cygwin bin directories to the end of my%PATH%
in My Computer -> Properties -> Advanced -> Environment Variables. I add them to the end of the path so in my normal work, they are searched last, minimizing the possibility of conflicts (in fact, I have had no problems with conflicts in all this time).When you invoke the Cygwin Bash Shell, those directories get prepended to the
%PATH%
so everything works as intended in that environment as well.When not running in Cygwin shell, my
%PATH%
is:This way, for example, ActiveState Perl's perl is found first when I am not in a Cygwin Shell, but the Cygwin perl is found when I am working in the Cygwin Shell.
如果您不熟悉 bash,您可以继续在标准 Windows 命令(即 DOS)shell 中工作。
为此,您必须将 C:\cygwin\bin (或您的本地替代项)添加到 Windows PATH 变量。
完成此操作后,您可以:
1) 打开命令 (DOS) shell
2) 将目录更改为代码所在位置(c:,然后 cd path\to\file)
3) gcc myProgram.c -o myProgram
正如 nik 的回复中提到的,“使用 Cygwin”文档是了解更多信息的好地方。
If you are not comfortable with bash, you can continue to work in a standard windows command (i.e. DOS) shell.
For this to work you must add C:\cygwin\bin (or your local alternative) to the Windows PATH variable.
With this done, you may:
1) Open a command (DOS) shell
2) Change the directory to the location of your code (c:, then cd path\to\file)
3) gcc myProgram.c -o myProgram
As mentioned in nik's response, the "Using Cygwin" documentation is a great place to learn more.
如果你只是执行 gcc program.c -o program -mno-cygwin ,它会编译得很好,你不需要将 cygwin1.dll 添加到你的路径中,你可以继续将可执行文件分发到不支持的计算机上没有安装 cygwin,它仍然会运行。 希望这可以帮助
If you just do gcc program.c -o program -mno-cygwin it will compile just fine and you won't need to add cygwin1.dll to your path and you can just go ahead and distribute your executable to a computer which doesn't have cygwin installed and it will still run. Hope this helps
cygwin 下的 Windows 路径
C:\src
变为/cygdrive/c/src
Windows path
C:\src
under cygwin becomes/cygdrive/c/src
使用 Cygwin 编译 C 程序
我们将使用 Cygwin 上的 gcc 编译器来编译程序。
1) 启动 Cygwin
切换到您为此类创建的目录
2) 通过输入cd c:/windows/desktop
来编译程序
3) 通过输入gcc myProgram.c -o myProgram
gcc 命令调用 gcc 编译器来编译您的 C 程序。
Compiling your C program using Cygwin
We will be using the gcc compiler on Cygwin to compile programs.
1) Launch Cygwin
2) Change to the directory you created for this class by typing
cd c:/windows/desktop
3) Compile the program by typing
gcc myProgram.c -o myProgram
the command gcc invokes the gcc compiler to compile your C program.
您最好在 cygwin shell 内编辑文件。 通常启动时它有默认的用户目录。 您可以从 shell 编辑文件,执行“vi somefile.c”或“emacs somefile.c”等操作。 假设 vi 或 emacs 安装在 cygwin 中。
如果您想在桌面上归档,则必须转到类似于(在 XP 上)“/cygwindrive/c/Documents\ and\ Settings/Frank/Desktop”的路径(如果内存正常)。 只需 cd 到该路径,然后对该文件运行命令即可。
You might be better off editing a file inside of cygwin shell. Normally it has default user directory when you start it up. You can edit a file from the shell doing something like "vi somefile.c" or "emacs somefile.c". That's assuming vi or emacs are installed in cygwin.
If you want to file on your desktop, you'll have to go to a path similar (on XP) to "/cygwindrive/c/Documents\ and\ Settings/Frank/Desktop" (If memory serves correctly). Just cd to that path, and run your command on the file.
CYGWIN 非常酷! 您可以从其他系统(例如 Linux)编译程序,它们会工作。 我指的是通信程序,甚至是网络服务器。
这是一个技巧。 如果您正在 Windows 文件资源管理器中查看文件,则可以在 bash 窗口中键入“cd”,然后从资源管理器的地址栏拖动到 cygwin 窗口,完整路径将被复制! 顺便说一句,这也适用于 Windows 命令 shell。
另外:虽然“cd /cygdrive/c”是正式路径,但它也接受“cd c:”作为快捷方式。 您可能需要在拖入路径的其余部分之前执行此操作。
应该会自动找到 stdio.h 文件,就像在传统系统上一样。
Cygwin is very cool! You can compile programs from other systems (Linux, for example), and they will work. I'm talking communications programs, or web servers, even.
Here is one trick. If you are looking at your file in the Windows File Explorer, you can type "cd " in your bash windows, then drag from explorer's address bar into the cygwin window, and the full path will be copied! This works in the Windows command shell as well, by the way.
Also: While "cd /cygdrive/c" is the formal path, it will also accept "cd c:" as a shortcut. You may need to do this before you drag in the rest of the path.
The stdio.h file should be found automatically, as it would be on a conventional system.