You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# Walkthrough: Compile a C program on the command line
10
10
11
-
The Visual Studio build tools include a C compiler that you can use to create everything from basic console programs to full Windows Desktop applications, mobile apps, and more. Microsoft C/C++ (MSVC) is a C and C++ compiler that, in its latest versions, conforms to some of the latest C language standards, including C11 and C17.
11
+
The Visual Studio build tools include a C compiler that you can use to create everything from basic console programs to full Windows desktop applications and mobile apps. Microsoft C/C++ (MSVC) is a C and C++ compiler that, in its latest versions, conforms to some of the latest C language standards, including C11 and C17.
12
12
13
-
This guide shows how to create a basic,*Hello, World*-style C program by using a text editor, and then compile it on the command line. If you'd rather work in C++ on the command line, see [Walkthrough: Compiling a Native C++ Program on the Command Line](walkthrough-compiling-a-native-cpp-program-on-the-command-line.md). If you'd like to try the Visual Studio IDE instead of using the command line, see [Walkthrough: Working with Projects and Solutions (C++)](../ide/walkthrough-working-with-projects-and-solutions-cpp.md) or [Using the Visual Studio IDE for C++ Desktop Development](../ide/using-the-visual-studio-ide-for-cpp-desktop-development.md).
13
+
This guide explains how to create a basic *Hello, World*-style C program by using a text editor, and then compile it on the command line. If you'd rather work in C++ on the command line, see [Walkthrough: Compiling a Native C++ Program on the Command Line](walkthrough-compiling-a-native-cpp-program-on-the-command-line.md). If you'd like to try the Visual Studio IDE instead of using the command line, see [Walkthrough: Working with Projects and Solutions (C++)](../ide/walkthrough-working-with-projects-and-solutions-cpp.md) or [Using the Visual Studio IDE for C++ Desktop Development](../ide/using-the-visual-studio-ide-for-cpp-desktop-development.md).
14
14
15
15
## Prerequisites
16
16
17
-
- Either Visual Studio or the Build Tools for Visual Studio, and the optional Desktop development with C++ workload
17
+
- Either **Visual Studio** or the **build tools for Visual Studio**, and the optional **Desktop development with C++** workload
18
18
- Visual Studio is a powerful integrated development environment that supports a full-featured editor, resource managers, debuggers, and compilers for many languages and platforms. For information on these features and how to download and install Visual Studio, including the free Visual Studio Community edition, see [Install Visual Studio](/visualstudio/install/install-visual-studio).
19
-
- The Build Tools for Visual Studio installs only the command-line toolset, the compilers, tools, and libraries you need to build C and C++ programs. It's perfect for build labs or classroom exercises and installs relatively quickly. To install only the command-line toolset, download Build Tools for Visual Studio from the [Visual Studio downloads](https://visualstudio.microsoft.com/downloads/#build-tools-for-visual-studio-2022) page and run the installer. In the Visual Studio installer, select the **Desktop development with C++** workload (in older versions of Visual Studio, select the **C++ build tools** workload), and choose **Install**.
19
+
- The build tools for Visual Studio install only the command-line toolset, the compilers, tools, and libraries you need to build C and C++ programs. It's perfect for build labs or classroom exercises and installs relatively quickly. To install only the command-line toolset, download build tools for Visual Studio from the [Visual Studio downloads](https://visualstudio.microsoft.com/downloads/#build-tools-for-visual-studio-2022) page and run the installer. In the Visual Studio installer, select the **Desktop development with C++** workload (in older versions of Visual Studio, select the **C++ build tools** workload), and choose **Install**.
20
20
21
-
- MSVC compiler
21
+
-**MSVC compiler**
22
22
- MSVC has complex requirements for the command-line environment to find the tools, headers, and libraries it uses. **You can't use MSVC in a plain command prompt window** without some preparation. You need a *developer command prompt* window, which is a regular command prompt window that has all the required environment variables set. Fortunately, Visual Studio installs shortcuts for you to launch developer command prompts that have the environment set up for command line builds. Unfortunately, the names of the developer command prompt shortcuts and where they're located are different in almost every version of Visual Studio and on different versions of Windows. Your first walkthrough task is to find the right shortcut to use.
23
23
24
24
> [!NOTE]
@@ -75,7 +75,7 @@ There might be differences in the current directory or version numbers, dependin
75
75
> [!NOTE]
76
76
> If you get an error such as **'cl' is not recognized as an internal or external command, operable program or batch file**, error C1034, or error LNK1104 when you run the `cl` command, then either you're not using a developer command prompt, or something is wrong with your installation of Visual Studio. You must fix this issue before you can continue.
77
77
78
-
If you can't find the developer command prompt shortcut, or if you get an error message when you enter `cl`, then your Visual Studio installation might have a problem. If you're using Visual Studio 2017 or later, try reinstalling the **Desktop development with C++** workload in the Visual Studio installer. For details, see [Install C++ support in Visual Studio](vscpp-step-0-installation.md). Or, reinstall the Build Tools from the [Visual Studio downloads](https://visualstudio.microsoft.com/downloads/) page. Don't go on to the next section until the `cl` command works. For more information about installing and troubleshooting Visual Studio, see [Install Visual Studio](/visualstudio/install/install-visual-studio).
78
+
If you can't find the developer command prompt shortcut, or if you get an error message when you enter `cl`, then your Visual Studio installation might have a problem. If you're using Visual Studio 2017 or later, try reinstalling the **Desktop development with C++** workload in the Visual Studio installer. For details, see [Install C++ support in Visual Studio](vscpp-step-0-installation.md). Or, reinstall the build tools from the [Visual Studio downloads](https://visualstudio.microsoft.com/downloads/) page. Don't go on to the next section until the `cl` command works. For more information about installing and troubleshooting Visual Studio, see [Install Visual Studio](/visualstudio/install/install-visual-studio).
79
79
80
80
> [!NOTE]
81
81
> Depending on the version of Windows on the computer and the system security configuration, you might need to right-click to open the shortcut menu for the developer command prompt shortcut and then choose **Run as Administrator** to successfully build and run the program that you create by following this walkthrough.
@@ -153,7 +153,7 @@ If you can't find the developer command prompt shortcut, or if you get an error
153
153
154
154
## Advanced steps
155
155
156
-
This *Hello, World* example is about as basic as a C program can get. Realworld programs have header files and more source files, link in libraries, and do useful work.
156
+
This *Hello, World* example is about as basic as a C program can get. Real-world programs have header files and more source files, link in libraries, and do useful work.
157
157
158
158
You can use the steps in this walkthrough to build your own C code instead of typing the sample code shown. You can also build many C code sample programs that you find elsewhere. To compile a program that has more source code files, enter them all on the command line:
159
159
@@ -173,7 +173,7 @@ You can use NMAKE and makefiles, or MSBuild and project files to configure and b
173
173
174
174
The C and C++ languages are similar, but not the same. The MSVC compiler uses a basic rule to determine which language to use when it compiles your code. By default, the MSVC compiler treats all files that end in *`.c`* as C source code, and all files that end in *`.cpp`* as C++ source code. To force the compiler to treat all files as C no matter the file name extension, use the [/TC](reference/tc-tp-tc-tp-specify-source-file-type.md) compiler option.
175
175
176
-
By default, MSVC is compatible with the ANSI C89 and ISO C99 standards, but not strictly conforming. In most cases, portable C code compiles and runs as expected. The compiler provides optional support for the changes in ISO C11/C17. To compile with C11/C17 support, use the compiler flag `/std:c11` or `/std:c17`. C11/C17 support requires Windows SDK 10.0.20201.0 or later. Windows SDK 10.0.22000.0 or later is recommended. You can download the latest SDK from the [Windows SDK](https://developer.microsoft.com/windows/downloads/windows-sdk/) page. For more information, and instructions on how to install and use this SDK for C development, see [Install C11 and C17 support in Visual Studio](../overview/install-c17-support.md).
176
+
By default, MSVC is compatible with the ANSI C89 and ISO C99 standards, but not strictly conforming. In most cases, portable C code compiles and runs as expected. The compiler provides optional support for the changes in ISO C11 and C17. To compile with C11 and C17 support, use the compiler flag `/std:c11` or `/std:c17`. C11 and C17 support requires Windows SDK 10.0.20201.0 or later. Windows SDK 10.0.22000.0 or later is recommended. You can download the latest SDK from the [Windows SDK](https://developer.microsoft.com/windows/downloads/windows-sdk/) page. For more information, and instructions on how to install and use this SDK for C development, see [Install C11 and C17 support in Visual Studio](../overview/install-c17-support.md).
177
177
178
178
Certain library functions and POSIX function names are deprecated by MSVC. The functions are supported, but the preferred names have changed. For more information, see [Security Features in the CRT](../c-runtime-library/security-features-in-the-crt.md) and [Compiler Warning (level 3) C4996](../error-messages/compiler-warnings/compiler-warning-level-3-c4996.md).
Copy file name to clipboardExpand all lines: docs/build/walkthrough-creating-and-using-a-dynamic-link-library-cpp.md
+13-13Lines changed: 13 additions & 13 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -193,9 +193,9 @@ When the `MATHLIBRARY_EXPORTS` macro is defined, the `MATHLIBRARY_API` macro set
193
193
194
194
1. In **Solution Explorer**, right-click on the **Source Files** node and choose **Add** > **New Item**. Create a new .cpp file called *MathLibrary.cpp*, in the same way that you added a new header file in the previous step.
195
195
196
-
1. In the editor window, select the tab for **MathLibrary.cpp** if it's already open. If not, in **Solution Explorer**, double-click **MathLibrary.cpp** in the **Source Files** folder of the **MathLibrary** project to open it.
196
+
1. In the editor window, select the **MathLibrary.cpp** tab if it's already open. If not, in **Solution Explorer**, double-click **MathLibrary.cpp** in the **Source Files** folder of the **MathLibrary** project to open it.
197
197
198
-
1. In the editor, replace the contents of the MathLibrary.cpp file with the following code:
198
+
1. In the editor, replace the contents of the *MathLibrary.cpp* file with the following code:
199
199
200
200
```cpp
201
201
// MathLibrary.cpp : Defines the exported functions for the DLL.
@@ -262,7 +262,7 @@ When the `MATHLIBRARY_EXPORTS` macro is defined, the `MATHLIBRARY_API` macro set
262
262
263
263
1. In the editor window, select the tab for **MathLibrary.cpp** if it's already open. If not, in **Solution Explorer**, double-click **MathLibrary.cpp** in the **Source Files** folder of the **MathLibrary** project to open it.
264
264
265
-
1. In the editor, replace the contents of the MathLibrary.cpp file with the following code:
265
+
1. In the editor, replace the contents of the *MathLibrary.cpp* file with the following code:
266
266
267
267
```cpp
268
268
// MathLibrary.cpp : Defines the exported functions for the DLL.
@@ -394,11 +394,11 @@ To avoid out-of-sync code, we recommend you set the include path in your client
394
394
395
395
1. In the **Configure your new project** page, enter *MathClient* in the **Project name** box to specify a name for the project. Leave the default **Location** and **Solution name** values. Set **Solution** to **Create new solution**. Uncheck **Place solution and project in the same directory** if it's checked.
396
396
397
-

397
+
:::image type="content" source="media/mathclient-project-name-2019.png" alt-text="Screenshot of the Create a new project dialog box with the Console App option highlighted.":::
398
398
399
399
1. Choose the **Create** button to create the client project.
400
400
401
-
A minimal console application project is created for you. The name for the main source file is the same as the project name that you entered earlier. In this example, it's named **MathClient.cpp**. You can build it, but it doesn't use your DLL yet.
401
+
A minimal console application project is created for you. The name for the main source file is the same as the project name that you entered earlier. In this example, it's named *MathClient.cpp*. You can build it, but it doesn't use your DLL yet.
402
402
403
403
::: moniker-end
404
404
@@ -410,11 +410,11 @@ A minimal console application project is created for you. The name for the main
410
410
411
411
1. In the left pane of the **New Project** dialog, select **Windows Desktop** under **Installed** > **Visual C++**. In the center pane, select **Windows Console Application**. Specify the name for the project, *MathClient*, in the **Name** edit box. Leave the default **Location** and **Solution name** values. Set **Solution** to **Create new solution**. Check **Create directory for solution** if it's unchecked.
412
412
413
-

413
+
:::image type="content" source="media/mathclient-new-project-name-159.png" alt-text="Screenshot of the New Project dialog box with Windows Console Application highlighted, and Math Client typed in the Name text box.":::
414
414
415
415
1. Choose **OK** to create the client app project.
416
416
417
-
A minimal console application project is created for you. The name for the main source file is the same as the project name that you entered earlier. In this example, it's named **MathClient.cpp**. You can build it, but it doesn't use your DLL yet.
417
+
A minimal console application project is created for you. The name for the main source file is the same as the project name that you entered earlier. In this example, it's named *MathClient.cpp*. You can build it, but it doesn't use your DLL yet.
418
418
419
419
::: moniker-end
420
420
@@ -426,15 +426,15 @@ A minimal console application project is created for you. The name for the main
426
426
427
427
1. In the left pane of the **New Project** dialog, select **Win32** under **Installed** > **Templates** > **Visual C++**. In the center pane, select **Win32 Console Application**. Specify the name for the project, *MathClient*, in the **Name** edit box. Leave the default **Location** and **Solution name** values. Set **Solution** to **Create new solution**. Check **Create directory for solution** if it's unchecked.
428
428
429
-

429
+
:::image type="content" source="media/mathclient-project-name.png" alt-text="Screenshot of the New Project dialog box with Win32 Console Application Visual C plus plus highlighted, and Math Client typed in the Name text box.":::
430
430
431
431
1. Choose the **OK** button to dismiss the **New Project** dialog and start the **Win32 Application Wizard**. On the **Overview** page of the **Win32 Application Wizard** dialog box, choose the **Next** button.
432
432
433
433
1. On the **Application Settings** page, under **Application type**, select **Console application** if it isn't already selected.
434
434
435
435
1. Choose the **Finish** button to create the project.
436
436
437
-
When the wizard finishes, a minimal console application project is created for you. The name for the main source file is the same as the project name that you entered earlier. In this example, it's named **MathClient.cpp**. You can build it, but it doesn't use your DLL yet.
437
+
When the wizard finishes, a minimal console application project is created for you. The name for the main source file is the same as the project name that you entered earlier. In this example, it's named *MathClient.cpp*. You can build it, but it doesn't use your DLL yet.
438
438
439
439
::: moniker-end
440
440
@@ -450,11 +450,11 @@ Next, to call the MathLibrary functions in your source code, your project must i
450
450
451
451
1. In the property pane, select the dropdown control next to the **Additional Include Directories** edit box, and then choose **Edit**.
452
452
453
-

453
+
:::image type="content" source="media/mathclient-additional-include-directories-property.png" alt-text="Screenshot of the Property Pages dialog showing the Edit command in the Additional Include Directories property dropdown.":::
454
454
455
455
1. Double-click in the top pane of the **Additional Include Directories** dialog box to enable an edit control. Or, choose the folder icon to create a new entry.
456
456
457
-
1. In the edit control, specify the path to the location of the **MathLibrary.h** header file. You can choose the ellipsis (**...**) control to browse to the correct folder.
457
+
1. In the edit control, specify the path to the location of the *MathLibrary.h* header file. You can choose the ellipsis (**...**) control to browse to the correct folder.
458
458
459
459
You can also enter a relative path from your client source files to the folder that contains the DLL header files. If you followed the directions to put your client project in a separate solution from the DLL, the relative path should look like this:
460
460
@@ -466,11 +466,11 @@ Next, to call the MathLibrary functions in your source code, your project must i
466
466
467
467
When the DLL and client projects are in other folders, adjust the relative path to match. Or, use the ellipsis control to browse for the folder.
468
468
469
-

469
+
:::image type="content" source="media/mathclient-additional-include-directories.png" alt-text="Screenshot of the Additional Include Directories dialog showing the relative path to the MathLibrary directory.":::
470
470
471
471
1. After you've entered the path to the header file in the **Additional Include Directories** dialog box, choose the **OK** button. In the **Property Pages** dialog box, choose the **OK** button to save your changes.
472
472
473
-
You can now include the **MathLibrary.h** file and use the functions it declares in your client application. Replace the contents of **MathClient.cpp** by using this code:
473
+
You can now include the *MathLibrary.h* file and use the functions it declares in your client application. Replace the contents of *MathClient.cpp* by using this code:
474
474
475
475
```cpp
476
476
// MathClient.cpp : Client app for MathLibrary DLL.
0 commit comments