简体   繁体   中英

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 ,...). Why Linux (Ubuntu) can run but Visual Studio can't? 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,...). Why Linux (Ubuntu) can run but Visual Studio can't? 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. I heard of Windows Subsystem for Linux which is rumored to be some compatibility layer.

Windows is rumored to have its own Windows API .

The C standard has a small and rather poor API, eg fopen , fprintf & malloc (it does not know about directories or processes). Read Modern C and some C standard like n1570 or newer. You could code in C (using a cross-compiler ) some micro-controller like Arduino - they don't have any operating systems or files.

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. 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 .

A possible approach might be to use some framework library which has been ported to both Linux and Windows. You could try using 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. 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 . Notice that learning C++ takes months of efforts.

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.

Try downloading GNU GCC, and run it from the command line.

$ gcc -Wall file.c -o file

$ ./file

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM