[英]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.h
, sys/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,例如
fopen
, fprintf
和malloc
(它不知道目录或进程)。 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
.使用GDB和Valgrind (甚至可能使用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 bash 、 GNU make 、 ninja 、 RefPerSys或FLTK等。通过贡献其中一个,您会学到很多东西。
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.