简体   繁体   English

无法在 Visual Studio 中运行 C 文件

[英]Can't run C file in Visual Studio

I have a C file that I can run in Linux (Ubuntu) terminal, but when I try to run the C file in Visual Studio 2019 on Windows 10, it can't run, it says can't open source file ( unistd.h , sys/wait.h ,...). I have a C file that I can run in Linux (Ubuntu) terminal, but when I try to run the C file in Visual Studio 2019 on Windows 10, it can't run, it says can't open source file ( unistd.hsys/wait.h ,...)。 Why Linux (Ubuntu) can run but Visual Studio can't?为什么 Linux (Ubuntu) 可以运行但 Visual Studio 不能? Thanks!谢谢!

when I try to run the C file with Visual Studio 2019 on Windows 10, it can't run, it says can't open source file (unistd.h, sys/wait.h,...).当我尝试在 Windows 10 上使用 Visual Studio 2019 运行 C 文件时,它无法运行,它说无法打开源文件(unistd.h、sys/wait.h、...)。 Why Linux (Ubuntu) can run but Visual Studio can't?为什么 Linux (Ubuntu) 可以运行但 Visual Studio 不能? Thanks!谢谢!

Because the header files <unistd.h> (see eg syscalls(2) , fork(2) etc...) and <sys/wait.h> (see waitpid(2) ) are specific to the POSIX standard, and sadly Windows is not POSIX compliant.因为 header 文件<unistd.h> (参见例如syscalls(2)fork(2)等...)和<sys/wait.h> (参见waitpid(2) )是特定于POSIX标准的,遗憾的是Windows 不符合 POSIX。 I heard of Windows Subsystem for Linux which is rumored to be some compatibility layer.我听说了 Linux 的Windows 子系统,传闻它是某种兼容层。

Windows is rumored to have its own Windows API .传闻Windows有自己的Windows API

The C standard has a small and rather poor API, eg fopen , fprintf & malloc (it does not know about directories or processes). C 标准有一个小而相当差的 API,例如fopenfprintfmalloc (它不知道目录或进程)。 Read Modern C and some C standard like n1570 or newer.阅读现代 C和一些 C 标准,如n1570或更新版本。 You could code in C (using a cross-compiler ) some micro-controller like Arduino - they don't have any operating systems or files.您可以在 C(使用交叉编译器)中编写一些微控制器,例如Arduino - 他们没有任何操作系统或文件。

BTW, you don't run the file, you compile it, and on Linux you'll better use gcc -Wall -Wextra -g to compile it and get warnings and debug info.顺便说一句,您不运行文件,而是编译它,在 Linux 上,您最好使用gcc -Wall -Wextra -g来编译它并获取警告和调试信息。 Once your program has been debugged with GDB and Valgrind (and perhaps even with theaddress sanitizer ), you could ask the GCC compiler to optimize it, by adding -O3 .使用GDBValgrind (甚至可能使用address sanitizer )调试您的程序后,您可以通过添加-O3让 GCC 编译器对其进行优化。

A possible approach might be to use some framework library which has been ported to both Linux and Windows.一种可能的方法可能是使用已移植到 Linux 和 Windows 的框架库。 You could try using GTK .您可以尝试使用GTK It is gives you a common API (a documented set of types, declared functions and header files) which is implemented on Linux, Windows and MacOSX. It is gives you a common API (a documented set of types, declared functions and header files) which is implemented on Linux, Windows and MacOSX. With some care, a set of C files using GTK on Linux could be recompiled on Windows (with GTK available on it). With some care, a set of C files using GTK on Linux could be recompiled on Windows (with GTK available on it).

If you can afford learning C++ (see this reference and read Programming -- Principles and Practice Using C++ ) you could consider using C++ frameworks like Qt or POCO . If you can afford learning C++ (see this reference and read Programming -- Principles and Practice Using C++ ) you could consider using C++ frameworks like Qt or POCO . Notice that learning C++ takes months of efforts.请注意,学习 C++ 需要几个月的努力。

My recommendation is to study for inspiration the source code of existing open source software , like GNU bash , GNU make , ninja , RefPerSys , or FLTK , etc. You would learn a lot by contributing to one of them.我的建议是研究现有开源软件的源代码以获取灵感,例如GNU bashGNU makeninjaRefPerSysFLTK等。通过贡献其中一个,您会学到很多东西。

Try downloading GNU GCC, and run it from the command line.尝试下载 GNU GCC,然后从命令行运行它。

$ gcc -Wall file.c -o file

$ ./file

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法在Visual Studio 2012中编译C - Can't compile C in visual studio 2012 在Visual Studio 2010中无法获得使用C后端运行的orcc代码 - can't get orcc code to run using C backend with visual studio 2010 C代码无法在Windows上运行(Visual Studio 2013) - C code won't run on windows (Visual Studio 2013) 为什么我不能使用Visual Studio在C语言中读取文本文件? - Why can't I read a text file in C using Visual Studio? 可以使用Visual Studio通过ARM脚本运行C代码吗? - Can Visual Studio be used to run C code with ARM scripts? 如何在 C(visual studio 代码)中运行多个文件? - How can i run multiple files in C (visual studio code)? 无法在视觉 c 中读取文件 - Can't read file in visual c 我是一名学生,当我包含像 stdio.h 这样的库时,我无法在 C (visual studio) 中运行程序 - I'm a student and I can't run program in C (visual studio), when I include a library like stdio.h 我的C代码未在Visual Studio 2017上编译,并不断提示我找不到特定文件的错误? - My C code isn't compiling on Visual Studio 2017 and keeps giving me an error saying it can't find the specific file? 为什么 Visual Studio Code 无法编译我的 C 代码? - Why Visual Studio Code can't compile my C code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM