How to Compile C in Windows with Visual Studio
The C programming language was created in the 1970s and became widely used to program all types of computers, from simple embedded microcontrollers to multiprocessor supercomputers. It remains a beneficial computer language for programmers to know and has influenced the development of many other computer languages.
It makes sense to be able to write, compile, run, and debug C code on your computer. (If you are starting in programming see What is Computer Programming?). After writing the C code in a file editor and saving it to a .c
file it needs to be compiled into a program executable that the computer can run. To "compile" a program is to turn it the binary code that only the computer understands:
There are many free C compilers that you can choose to use. On a Windows computer, you can use Microsoft's own C compiler. It can be installed with the Microsoft Visual Studio Integrated Development Environment (IDE), Microsoft's main software development tooling. Let's see how to compile C in Windows with the Windows compiler installed by Visual Studio.
Install Windows C Compiler with Visual Studio
If you are learning to code, contributing to open source projects, or developing code for your personal use, then you can select and install the free Visual Studio Community version. You will still need to register the software, but once registered the Community version is free to use. You may be lucky and can get the Professional or Enterprise versions from your workplace.
To compile a C program in Windows, you need to select the Desktop development with C++ option when installing Visual Studio. The Microsoft compiler supports C and C++ programs (C++ is a follow-on from C). If the Visual Studio IDE is already installed use the Tools then Get Tools and Features menu to reopen the Visual Studio installer and select and install the Desktop development with C++ option.
How to Check that the Microsoft C Compiler is Installed
With Visual Studio installed, it is now possible to use the Developer Command Prompt. From this command prompt, it is possible to check that the Microsoft C Compiler is installed on your Windows machine. Simply start typing Developer Command in the Windows search bar (press the Windows key to access the search) and select the Developer Command Prompt app.
In the command prompt run the compiler by entering cl
and pressing return. If the C compiler is present you will see a message from it.
The command prompt will have set up a path to the C compiler. This means you can switch to a directory where you will be keeping your C project files and be able to access the compiler from there.
Compile an Example Hello World C Program
Here is a simple C program that prints the message Hello World!. Add this code to a file using Windows Notepad or another file editor, such as Notepad++ or the Visual Studio IDE. Save this program in a file with a .c
extension, e.g., hello.c.
#include <stdio.h>
int main() {
printf("Hello World!");
return 0;
}
Change to the file location in the Developer Command Prompt and run the compiler with the file name using, for example, cl hello.c
. This will compile the C code into a Windows program called hello.exe. Running hello.exe in the command prompt will print the message. See the first image at the start of the article to see it in action.
Compile More Than One C File in Windows
We can change the above example C code to call a function in another file. Here is a C file printing a message via a sayhello function. The code is saved into the same directory in a file called another.c:
#include <stdio.h>
void sayhello() {
printf("Hello from another file.");
}
Now change hello.c. It references the new sayhello function before using it. A line break has been added to the end of the previous message:
#include <stdio.h>
void sayhello();
int main() {
printf("Hello World!\n");
sayhello();
return 0;
}
If I had lots of functions in another file I would have listed them in a .h
file, a header file. Then use the #include directive to read the header file into the main program file. This is why the above C program has #include <stdio.h>
. It tells the compiler about the function printf
which is part of standard C.
You can now pass the new another.c file to the compiler along with the hello.c using cl hello.c another.c
. The two code files are added to the compiled program:
Compile and Link a Windows C DLL
We can get the C compiler to produce a Windows dynamic link library, a DLL, for the another.c file. Then call the sayhello function located in the DLL file from the hello.exe file. To use the Windows DLL file the symbols for sayhello are exported into a library, or .lib file. (Another type of library file, a static library, can contain compiled code, but in this case, only the symbols are needed as the code is in the DLL.) The library file is used to tell the compiler about the DLL contents.
Telling the C compiler to export the correct library symbols for Windows is done by declaring sayhello to be DLL exported:
#include <stdio.h>
__declspec(dllexport) void __cdecl sayhello(void) {
printf("Hello from another file.");
}
For the most efficiency when linking to the DLL use dllimport on sayhello in hello.c:
#include <stdio.h>
__declspec(dllimport) void sayhello();
int main() {
printf("Hello World!\n");
sayhello();
return 0;
}
Now compile another.c on its own but tell the C compiler to generate another.dll using the /LD compiler switch, i.e., cl /LD another.c
. The another.lib file is generated. Then compile hello.c and pass it another.lib so it can be linked to the DLL, using cl hello.c another.lib
:
Read more about dllexport and dllimport. You now have the basics set up to enable the development of C code on Windows.
See Also
- Hello World in C#, A Starting WinForms Example
- View the Tek Eye full Index for other articles.
Author:Daniel S. Fowler Published: