Learn to read and write on a file in C, Learn all about file handling. Start with basics and ask your doubts. It can perform the function of both ofstream and ifstream which means it can create files, write on files, and read from files. Opening a file. Reading and writing on a file. Jan 31, 2009 If you’re reading this tutorial, it’s likely because you googled how to read and write text files with C. And it’s probably a pretty lucky guess you’re a beginner, so I’ll try to explain as much as I can here to make it clear.
-->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:
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.
Note
When 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++)
How to Install Dev-C++ and the GLUT Libraries
for Compiling OpenGL Programs with ANSI C
(version of July 16, 2009)
These notes explain how to compile programs written in ANSI C with OpenGL and GLUT using the Dev-C++ compiler.
Bloodshed Dev-C++ is a free C++ compiler and development environment for Windows operating systems. Like most C++ compilers, it also can be used to compile ANSI C. By installing the GLUT header and library files, it can be used to write programs that use OpenGL. This is needed to run programs for Edward Angel's textbook, Interactive Computer Graphics 5th edition and possibly other computer graphics texts.
These notes do not explain how to compile OpenGL with C++ . The 6th edition of Angel's book uses C++ which will not work with these notes.
These instructions have been tested on a small variety of Windows 2000 and Windows XP systems. These systems come with the files needed for OpenGL, but not the files needed for GLUT.
Dev-C++ does not work well with Microsoft's Vista. The problem, and a possible fix, is discussed here: http://aresio.blogspot.com/2007/06/vista-and-dev-cpp.html but I have not tested this information.
I. Download Dev-C++ from http://www.bloodshed.net/dev/devcpp.html and install it.
Details:
Get Dev-C++ 5.0 beta 9.2 (4.9.9.2) (9.0 MB) with Mingw/GCC 3.4.2 Although this is a 'beta' version, it works perfectly fine. Click on SourceForge to go to a list of download sites and pick one. The file will be something like devcpp4.9.9.2_setup.exe. Save this file in a place like C:Temp.
When the download is complete, click on the 'open' button to start the installation process. (Or go to C:Temp andDouble click on devcpp4.9.9.2_setup.exe). You will see a few screens that ask you to pick a language (English) and to agree to the license terms. Choose a 'typical' installation.
Accept the suggested destination for the installation:
Many subdirectories and files are extracted to the destintion:
Answer 'yes' when it asks if you wish to install Dev-cpp for all users. Note: if the installation fails, re-install and try 'no' for this.
A screen says the installation is complete:
Keep the check mark in the box. Click on 'Finish'. A first-time configuration screen appears:
Pick 'English' and 'New Look'. In the next several screens, hit 'Yes' for its suggestions.
Eventually you are done. Click 'OK'.
II. DEV-C++ starts up. Try out the installation with a simple C program.
Details:
The program starts up automatically.
Click File/New/Project. Pick a name for the project (such as 'myProject'). Click 'C Project'. Click on 'Empty Project'. Click 'OK'.
In 'Create New Project', click 'save' (later on you will probably want to create separate subdirectories for your various projects.).
Click 'File/New/Source File' and in 'Add source file to current project' click 'Yes'. You now get a screen where you can edit the source file.
Type in a simple C program, as below. Now click 'File/Save As' and save the file as 'hello.c' (or other name.) Important: be sure that the file extension is .c. With any other extension (such as the suggested .cpp) you will have problems compiling.
Now click 'Execute/Compile and Run'
The program will (hopefully) compile, run, and write its output to a DOS window. If you have the system('pause')
statement in your program, the output will stay in the window until you hit a key. Another way to run the program (after it has been compiled) is to start a DOS window outside of the Dev-Cpp system, then navigate to the subdirectory that holds your project, and type hello.exe.
At this point, the compiler and development environment has been installed. You should find Dev-C++ listed under 'Programs' on the 'Start' menu and will now be able to write, compile, and run C (and C++) programs. You will have include files, libraries, and dll's for OpenGL (and all other standard packages) but not GLUT. GLUT manages the windows and other user interface components needed for OpenGL programming, and needs to be separately installed.
If you do not need GLUT , you can quit now.
III. Download and install GLUT
To run OpenGL with GLUT (which is what the programs in Angel's book use), you need to get three files and place each file in its proper directory. All the files you need (and more) are contained in one zip file.
Details:
Download GLUT files from http://chortle.ccsu.edu/Bloodshed/glutming.zip Download the file glutming.zip
Save the zip file in some convenient location (perhaps C:temp).
Double click on glutming.zip (or otherwise unzip it). You will see the files that are in the zip archive. (Your un-zipping program will probably be diferent than the one shown here, but should work about the same.)
Click on 'Extract' to extract all the subdirectories and files. Pick some convenient directory to extract them to (perhaps C:tempglutming). You only need three files, but extract all of them anyway.
Only three of the files in the various subdirectories are needed. Each of the three files should be put in a subdirectory with other files of its type. Use Explorer to move the files to where they are needed.
Note: If you only see some of these files listed in Explorer, click on 'View/Options/View' and then select the radio button 'Show all Files'.
glut.h -- copy this file to C:Dev-CppincludeGL
How To Create H Files On Dev C Windows 10
Copy from your 'unzipped' subdirectories (wherever they are):
To here:
libglut32.a -- copy this file from your unzipped directories to C:Dev-Cpplib
There may be a newer version of this file there, already. Replace that version with the one you unzipped (if you keep the newer version your programs will not link correctly.)
Copy from your 'unzipped' subdirectories:
To here:
glut32.dll -- move this file to C:WINNTSystem32, or similar location.
The location for this file depends on your operating system. The directory where it goes is the directory that holds the dynamic load libraries (*.dll). An easy way to find where it should go is to look for glu32.dll (use 'Search' from the start menu).
The directory to use should also have the files glu32.dll and opengl32.dll. These should have come with your operating system.
IV. Test Dev-cpp with GLUT
The essential step in compiling and running a C program that contains OpenGL and GLUT functions is to tell the linker where the libraries are. This is done by clicking Project/Project Options/Parameters/Add Library or Options and then navigating to the libraries you need to include: libopengl32.a, libglu32.a, and libglut32.a. The libraries should be added in that order.
Details:
a. Create a subdirectory for a project. Do this first, before you start Dev-Cpp. Create a new subdirectory with 'Explorer' by clicking 'File/New/Folder'.
For example, create a folder C:GLproject.
b. Start Dev-cpp:
c. Start a new project by clicking File/New/Project. In the panel that pops up, name the project something like 'rectangle', click on 'empty project' and 'C': Click OK.
Note: For compiling with OpenGL you must create a project. You need to have a project (not just a single C file) in order to link in the OpenGL libraries.
d. In the next panel, navigate to your folder C:GLproject, and click 'Save'.
How To Create H Files On Dev C Youtube
e. In Dev-C++, click 'File/New/Source File' and then in the next panel 'Add to Project' click 'yes'. Click 'File/Save As' and then give the file a name. Navigate to your project subdirectory to save the file in it. Name the file something like 'rectangle.c'
How To Create .h File In Dev C++
Be sure that the file names ends with '.c' anything else will cause big problems.
f. Click and drag your mouse over the following program so that it is highlighted, then click 'Edit/Copy' from the browser's menu bar.
g. Now click in the editing window of Dev-cpp and then click 'Edit/Paste' in its menu bar. The program will appear in the editing window.
h. Click 'File/Save'. The file in your project directory should now contain an OpenGL program.
i. Tell Dev-cpp what libraries need to be linked. Click 'Project/Project Options'.
j. Now click 'Parameters'. Click the 'Add Library or Object' button and navigate to the libraries that should be added, found under C:Dev-cpplib
- ../lib/libopengl32.a
- ../lib/libglu32.a
- ../lib/libglut32.a
How To Create H Files On Dev C Download
Add them in that order (only). Notice that the slashes will appear in Unix style '/' rather than DOS-style '.
When you are done adding the three libaries, you should see:
The exact pattern of '../../..' you see depends on how deep in the directory structure your source file lies.
Click 'OK'.
k. Click 'Execute/Compile and Run'. The program should compile, link, and run:
If things don't work (very common) click on the 'Compile Log' tab for some confusing error messages. If you see something like the following, it means that you made a mistake in adding the libraries to the project:
Try to fix the list of libraries, or perhaps start over from scratch.
You now are finished, or have given up.