Dec 09, 2018 Quick links What is C header file? How to create C header file? Program Output Write a C program to create and include your custom header file. How to create and include your own header file in C programming. C program to include another C program and custom header files. In this article I Continue reading C program to create and include custom header file →.
- Make Your Own Exhaust Header
- How To Create A Header File In Dev C++
- How To Make Your Own Header File In Dev C Download
- How To Create Header File In Dev C++
This step-by-step walkthrough shows how to create a static library (.lib file) for use with C++ apps. Using a static library is a great way to reuse code. Rather than reimplementing the same routines in every app that requires the functionality, you write them one time in a static library and then reference it from the apps. Code linked from a static library becomes part of your app—you don't have to install another file to use the code.
This walkthrough covers these tasks:
- Nov 06, 2012 Demonstrates how to create and use a user-defined header file in a C application. Skip navigation. Create Header Files in C profgustin. How to create your own header file in C.
- Apr 14, 2009 In the header files you have declarations so you can use function, classes, whatever which are not defined in your source file (they can be defined in another source file you will compile or in a library, for template stuff, you will have the definition in the header).
Prerequisites
An understanding of the fundamentals of the C++ language.
Create a static library project
The instructions for how to create the project vary depending on your version of Visual Studio. To see the documentation for your preferred version of Visual Studio, use the Version selector control. It's found at the top of the table of contents on this page.
To create a static library project in Visual Studio 2019
- On the menu bar, choose File > New > Project to open the Create a New Project dialog box.
- At the top of the dialog, set Language to C++, set Platform to Windows, and set Project type to Library.
- From the filtered list of project types, select Windows Desktop Wizard, then choose Next.
- In the Configure your new project page, enter MathLibrary in the Project name box to specify a name for the project. Enter StaticMath in the Solution name box. Choose the Create button to open the Windows Desktop Project dialog.
- In the Windows Desktop Project dialog, under Application type, select Static Library (.lib).
- Under Additional options, uncheck the Precompiled header check box if it's checked. Check the Empty project box.
- Choose OK to create the project.
To create a static library project in Visual Studio 2017
- On the menu bar, choose File > New > Project.
- In the New Project dialog box, select Installed > Visual C++ > Windows Desktop. In the center pane, select Windows Desktop Wizard.
- Specify a name for the project—for example, MathLibrary—in the Name box. Specify a name for the solution—for example, StaticMath—in the Solution Name box. Choose the OK button.
- In the Windows Desktop Project dialog, under Application type, select Static Library (.lib).
- Under Additional Options, uncheck the Precompiled header check box if it's checked. Check the Empty project box.
- Choose OK to create the project.
To create a static library project in Visual Studio 2015
- On the menu bar, choose File > New > Project.
- In the New Project dialog box, select Installed > Templates > Visual C++ > Win32. In the center pane, select Win32 Console Application.
- Specify a name for the project—for example, MathLibrary—in the Name box. Specify a name for the solution—for example, StaticMath—in the Solution Name box. Choose the OK button.
- In the Win32 Application Wizard, choose Next.
- In the Application Settings page, under Application type, select Static library. Under Additional options, uncheck the Precompiled header checkbox. Choose Finish to create the project.
Add a class to the static library
To add a class to the static library
- To create a header file for a new class, right-click to open the shortcut menu for the MathLibrary project in Solution Explorer, and then choose Add > New Item.
- In the Add New Item dialog box, select Visual C++ > Code. In the center pane, select Header File (.h). Specify a name for the header file—for example, MathLibrary.h—and then choose the Add button. A nearly blank header file is displayed.
- Add a declaration for a class named
Arithmetic
to do common mathematical operations such as addition, subtraction, multiplication, and division. The code should resemble: - To create a source file for the new class, open the shortcut menu for the MathLibrary project in Solution Explorer, and then choose Add > New Item.
- In the Add New Item dialog box, in the center pane, select C++ File (.cpp). Specify a name for the source file—for example, MathLibrary.cpp—and then choose the Add button. A blank source file is displayed.
- Use this source file to implement the functionality for class
Arithmetic
. The code should resemble: - To build the static library, select Build > Build Solution on the menu bar. The build creates a static library, MathLibrary.lib, that can be used by other programs.NoteWhen you build on the Visual Studio command line, you must build the program in two steps. First, run
cl /c /EHsc MathLibrary.cpp
to compile the code and create an object file that's named MathLibrary.obj. (Thecl
command invokes the compiler, Cl.exe, and the/c
option specifies compile without linking. For more information, see /c (Compile Without Linking).) Second, runlib MathLibrary.obj
to link the code and create the static library MathLibrary.lib. (Thelib
command invokes the Library Manager, Lib.exe. For more information, see LIB Reference.)
Create a C++ console app that references the static library
To create a C++ console app that references the static library in Visual Studio 2019
- In Solution Explorer, right-click on the top node, Solution 'StaticMath', to open the shortcut menu. Choose Add > New Project to open the Add a New Project dialog box.
- At the top of the dialog, set the Project type filter to Console.
- From the filtered list of project types, choose Console App then choose Next. In the next page, enter MathClient in the Name box to specify a name for the project.
- Choose the Create button to create the client project.
- After you create a console app, an empty program is created for you. The name for the source file is the same as the name that you chose earlier. In the example, it's named
MathClient.cpp
.
To create a C++ console app that references the static library in Visual Studio 2017
- In Solution Explorer, right-click on the top node, Solution 'StaticMath', to open the shortcut menu. Choose Add > New Project to open the Add a New Project dialog box.
- In the Add New Project dialog box, select Installed > Visual C++ > Windows Desktop. In the center pane, select Windows Desktop Wizard.
- Specify a name for the project—for example, MathClient—in the Name box. Choose the OK button.
- In the Windows Desktop Project dialog, under Application type, select Console Application (.exe).
- Under Additional Options, uncheck the Precompiled header check box if it's checked.
- Choose OK to create the project.
- After you create a console app, an empty program is created for you. The name for the source file is the same as the name that you chose earlier. In the example, it's named
MathClient.cpp
.
To create a C++ console app that references the static library in Visual Studio 2015
- In Solution Explorer, right-click on the top node, Solution 'StaticMath', to open the shortcut menu. Choose Add > New Project to open the Add a New Project dialog box.
- In the Add New Project dialog box, select Installed > Visual C++ > Win32. In the center pane, select Win32 Console Application.
- Specify a name for the project—for example, MathClient—in the Name box. Choose the OK button.
- In the Win32 Application Wizard dialog, choose Next.
- On the Application Settings page, under Application type, make sure Console application is selected. Under Additional options, uncheck Precompiled header, then check the Empty Project checkbox. Choose Finish to create the project.
- To add a source file to the empty project, right-click to open the shortcut menu for the MathClient project in Solution Explorer, and then choose Add > New Item.
- In the Add New Item dialog box, select Visual C++ > Code. In the center pane, select C++ File (.cpp). Specify a name for the source file—for example, MathClient.cpp—and then choose the Add button. A blank source file is displayed.
Use the functionality from the static library in the app
To use the functionality from the static library in the app
- Before you can use the math routines in the static library, you must reference it. Open the shortcut menu for the MathClient project in Solution Explorer, and then choose Add > Reference.
- The Add Reference dialog box lists the libraries that you can reference. The Projects tab lists the projects in the current solution and any libraries they reference. Open the Projects tab, select the MathLibrary check box, and then choose the OK button.
- To reference the
MathLibrary.h
header file, you must modify the included directories path. In Solution Explorer, right-click on MathClient to open the shortcut menu. Choose Properties to open the MathClient Property Pages dialog box. - In the MathClient Property Pages dialog box, set the Configuration drop-down to All Configurations. Set the Platform drop-down to All Platforms.
- Select the Configuration Properties > C/C++ > General property page. In the Additional Include Directories property, specify the path of the MathLibrary directory, or browse for it.To browse for the directory path:
- Open the Additional Include Directories property value drop-down list, and then choose Edit.
- In the Additional Include Directories dialog box, double-click in the top of the text box. Then choose the ellipsis button (...) at the end of the line.
- In the Select Directory dialog box, navigate up a level, and then select the MathLibrary directory. Then choose the Select Folder button to save your selection.
- 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 to the project.
- You can now use the
Arithmetic
class in this app by including the#include 'MathLibrary.h'
header in your code. Replace the contents ofMathClient.cpp
with this code: - To build the executable, choose Build > Build Solution on the menu bar.
Run the app
To run the app
- Make sure that MathClient is selected as the default project. To select it, right-click to open the shortcut menu for MathClient in Solution Explorer, and then choose Set as StartUp Project.
- To run the project, on the menu bar, choose Debug > Start Without Debugging. The output should resemble:
See also
Walkthrough: Creating and Using a Dynamic Link Library (C++)
Desktop Applications (Visual C++)
-->Desktop Applications (Visual C++)
This walkthrough shows how to create a traditional Windows desktop application in Visual Studio. The example application you'll create uses the Windows API to display 'Hello, Windows desktop!' in a window. You can use the code that you develop in this walkthrough as a pattern to create other Windows desktop applications.
The Windows API (also known as the Win32 API, Windows Desktop API, and Windows Classic API) is a C-language-based framework for creating Windows applications. It has been in existence since the 1980s and has been used to create Windows applications for decades. More advanced and easier-to-program frameworks have been built on top of the Windows API. For example, MFC, ATL, the .NET frameworks. Even the most modern Windows Runtime code for UWP and Store apps written in C++/WinRT uses the Windows API underneath. For more information about the Windows API, see Windows API Index. There are many ways to create Windows applications, but the process above was the first.
Important
For the sake of brevity, some code statements are omitted in the text. The Build the code section at the end of this document shows the complete code.
Prerequisites
- A computer that runs Microsoft Windows 7 or later versions. We recommend Windows 10 for the best development experience.
- A copy of Visual Studio. For information on how to download and install Visual Studio, see Install Visual Studio. When you run the installer, make sure that the Desktop development with C++ workload is checked. Don't worry if you didn't install this workload when you installed Visual Studio. You can run the installer again and install it now.
- An understanding of the basics of using the Visual Studio IDE. If you've used Windows desktop apps before, you can probably keep up. For an introduction, see Visual Studio IDE feature tour.
- An understanding of enough of the fundamentals of the C++ language to follow along. Don't worry, we don't do anything too complicated.
Create a Windows desktop project
Follow these steps to create your first Windows desktop project. As you go, you'll enter the code for a working Windows desktop application. To see the documentation for your preferred version of Visual Studio, use the Version selector control. It's found at the top of the table of contents on this page.
To create a Windows desktop project in Visual Studio 2019
- From the main menu, choose File > New > Project to open the Create a New Project dialog box.
- At the top of the dialog, set Language to C++, set Platform to Windows, and set Project type to Desktop.
- From the filtered list of project types, choose Windows Desktop Wizard then choose Next. In the next page, enter a name for the project, for example, DesktopApp.
- Choose the Create button to create the project.
- The Windows Desktop Project dialog now appears. Under Application type, select Desktop application (.exe). Under Additional options, select Empty project. Choose OK to create the project.
- In Solution Explorer, right-click the DesktopApp project, choose Add, and then choose New Item.
- In the Add New Item dialog box, select C++ File (.cpp). In the Name box, type a name for the file, for example, HelloWindowsDesktop.cpp. Choose Add.
Your project is now created and your source file is opened in the editor. To continue, skip ahead to Create the code.
To create a Windows desktop project in Visual Studio 2017
- On the File menu, choose New and then choose Project.
- In the New Project dialog box, in the left pane, expand Installed > Visual C++, then select Windows Desktop. In the middle pane, select Windows Desktop Wizard.In the Name box, type a name for the project, for example, DesktopApp. Choose OK.
- In the Windows Desktop Project dialog, under Application type, select Windows application (.exe). Under Additional options, select Empty project. Make sure Precompiled Header isn't selected. Choose OK to create the project.
- In Solution Explorer, right-click the DesktopApp project, choose Add, and then choose New Item.
- In the Add New Item dialog box, select C++ File (.cpp). In the Name box, type a name for the file, for example, HelloWindowsDesktop.cpp. Choose Add.
Your project is now created and your source file is opened in the editor. To continue, skip ahead to Create the code.
Make Your Own Exhaust Header
To create a Windows desktop project in Visual Studio 2015
- On the File menu, choose New and then choose Project.
- In the New Project dialog box, in the left pane, expand Installed > Templates > Visual C++, and then select Win32. In the middle pane, select Win32 Project.In the Name box, type a name for the project, for example, DesktopApp. Choose OK.
- On the Overview page of the Win32 Application Wizard, choose Next.
- On the Application Settings page, under Application type, select Windows application. Under Additional options, uncheck Precompiled header, then select Empty project. Choose Finish to create the project.
- In Solution Explorer, right-click the DesktopApp project, choose Add, and then choose New Item.
- In the Add New Item dialog box, select C++ File (.cpp). In the Name box, type a name for the file, for example, HelloWindowsDesktop.cpp. Choose Add.
Your project is now created and your source file is opened in the editor.
Create the code
Next, you'll learn how to create the code for a Windows desktop application in Visual Studio.
To start a Windows desktop application
- Just as every C application and C++ application must have a
main
function as its starting point, every Windows desktop application must have aWinMain
function.WinMain
has the following syntax.For information about the parameters and return value of this function, see WinMain entry point.NoteWhat are all those extra words, such asCALLBACK
, orHINSTANCE
, or_In_
? The traditional Windows API uses typedefs and preprocessor macros extensively to abstract away some of the details of types and platform-specific code, such as calling conventions, __declspec declarations, and compiler pragmas. In Visual Studio, you can use the IntelliSense Quick Info feature to see what these typedefs and macros define. Hover your mouse over the word of interest, or select it and press Ctrl+K, Ctrl+I for a small pop-up window that contains the definition. For more information, see Using IntelliSense. Parameters and return types often use SAL Annotations to help you catch programming errors. For more information, see Using SAL Annotations to Reduce C/C++ Code Defects. - Windows desktop programs require <windows.h>. <tchar.h> defines the
TCHAR
macro, which resolves ultimately to wchar_t if the UNICODE symbol is defined in your project, otherwise it resolves to char. If you always build with UNICODE enabled, you don't need TCHAR and can just use wchar_t directly. - Along with the
WinMain
function, every Windows desktop application must also have a window-procedure function. This function is typically namedWndProc
, but you can name it whatever you like.WndProc
has the following syntax.In this function, you write code to handle messages that the application receives from Windows when events occur. For example, if a user chooses an OK button in your application, Windows will send a message to you and you can write code inside yourWndProc
function that does whatever work is appropriate. It's called handling an event. You only handle the events that are relevant for your application.For more information, see Window Procedures.
To add functionality to the WinMain function
- In the
WinMain
function, you populate a structure of type WNDCLASSEX. The structure contains information about the window: the application icon, the background color of the window, the name to display in the title bar, among other things. Importantly, it contains a function pointer to your window procedure. The following example shows a typicalWNDCLASSEX
structure.For information about the fields of the structure above, see WNDCLASSEX. - Register the
WNDCLASSEX
with Windows so that it knows about your window and how to send messages to it. Use the RegisterClassEx function and pass the window class structure as an argument. The_T
macro is used because we use theTCHAR
type. - Now you can create a window. Use the CreateWindow function.This function returns an
HWND
, which is a handle to a window. A handle is somewhat like a pointer that Windows uses to keep track of open windows. For more information, see Windows Data Types. - At this point, the window has been created, but we still need to tell Windows to make it visible. That's what this code does:The displayed window doesn't have much content because you haven't yet implemented the
WndProc
function. In other words, the application isn't yet handling the messages that Windows is now sending to it. - To handle the messages, we first add a message loop to listen for the messages that Windows sends. When the application receives a message, this loop dispatches it to your
WndProc
function to be handled. The message loop resembles the following code.For more information about the structures and functions in the message loop, see MSG, GetMessage, TranslateMessage, and DispatchMessage.At this point, theWinMain
function should resemble the following code.
To add functionality to the WndProc function
How To Create A Header File In Dev C++
- To enable the
WndProc
function to handle the messages that the application receives, implement a switch statement.One important message to handle is the WM_PAINT message. The application receives theWM_PAINT
message when part of its displayed window must be updated. The event can occur when a user moves a window in front of your window, then moves it away again. Your application doesn't know when these events occur. Only Windows knows, so it notifies your app with aWM_PAINT
message. When the window is first displayed, all of it must be updated.To handle aWM_PAINT
message, first call BeginPaint, then handle all the logic to lay out the text, buttons, and other controls in the window, and then call EndPaint. For the application, the logic between the beginning call and the ending call is to display the string 'Hello, Windows desktop!' in the window. In the following code, notice that the TextOut function is used to display the string.HDC
in the code is a handle to a device context, which is a data structure that Windows uses to enable your application to communicate with the graphics subsystem. TheBeginPaint
andEndPaint
functions make your application behave like a good citizen and doesn't use the device context for longer than it needs to. The functions help make the graphics subsystem is available for use by other applications. - An application typically handles many other messages. For example, WM_CREATE when a window is first created, and WM_DESTROY when the window is closed. The following code shows a basic but complete
WndProc
function.
Build the code
How To Make Your Own Header File In Dev C Download
As promised, here's the complete code for the working application.
To build this example
- Delete any code you've entered in HelloWindowsDesktop.cpp in the editor. Copy this example code and then paste it into HelloWindowsDesktop.cpp:
- On the Build menu, choose Build Solution. The results of the compilation should appear in the Output window in Visual Studio.
- To run the application, press F5. A window that contains the text 'Hello, Windows desktop!' should appear in the upper-left corner of the display.
Congratulations! You've completed this walkthrough and built a traditional Windows desktop application.