Borland C ++ engineering cainiao tutorial, borland Engineering

Source: Internet
Author: User

Borland C ++ engineering cainiao tutorial, borland Engineering

I can't believe that the compiler used for teaching in the school is still old in the 70 or 80 s. Is it better to train students who have never touched the code to deeply understand the running process of C language, I have to say that a heavyweight development environment like VisualStdio does Block A lot of underlying running processes. In this case, it is difficult for me to come up with an out-of-band tutorial!

When you write a small program in C language, you can directly create a new source file in the development environment, and compile and run the file. When developing a large project, you must create a project to organize various files, it generally includes the source file (. c) header file (. h), resource files (such as image, audio, and text files ).

In order to develop good programming habits and make the program file structure clear, we should first create a working directory (do not always put the program in C: \ BORLAND \ BIN) so where should this working directory be stored? Of course, it can be used anywhere. As long as your development environment Borland C knows where it is, the DISK_C folder is a virtual C disk according to the development tools you provide, therefore, the working directory can be stored in the DISK_C folder, no matter where it is, including sub-directories. To facilitate viewing, we can directly create a test folder under the root directory of DISK_C as the working directory.

Figure 1 development tools

Figure 2 create a working directory

With the working directory, We can compile the program. Our program is ultimately a Project that can be debugged ),

Open Borland C (BC)

Before the operation, we need to set several directories so that BC can know where our working directory is.

First modify the workspace, choose File> Change dir ...., Change the Directory Name to the working Directory we created earlier. Put your working Directory in the root Directory of Disk_C (Virtual Drive C, change it to C: \ test and click OK to save it.

Another problem is that the working directory will change back to C: \ Borland \ BIN next time we start borland c. To avoid re-configuring the working directory every time we start it, we need to modify the default working directory. The configuration file is the bc31.conf file in the dosbox folder. Open it with any text editor (such as NotePad) and find the last few lines, add the # sign in front of cd \ BORLANDC \ BIN and add cd \ test, which is your working directory, save the file, and restart Borland C, the working directory is always cd \ test.

Next, choose Options> Directories. There are four edit boxes. The first one is the package directory that comes with BC (the header file that comes with the compiling environment (for example, stdio. h). The second item is the library directory of BC, which contains some built-in static link library files. It doesn't matter what the previous two do not understand, as long as it is set to the following, (usually the default is ). The following two items are specific settings. The third item is Output Directory, which stores the files generated by project compilation and running (such. obj file), in principle, can be set to any directory in Disk_C, but since we have created a working directory, in order to facilitate viewing and management, of course, is set in the working directory, if there are many output files, we can create a new folder in the test folder in the working directory as the output directory. Because the program set in Class C is not too large, we can directly set the output file directory in the test folder. The fourth item is the source code Directory, which, as its name implies, tells the place where BC source code is stored. Similarly, we can create a folder in the test folder to store the source files. However, given that the program set up in class C is not large enough, we put it directly in the test folder.

OK, save.

 

Finally, we can start to build a project by creating an empty project in the development environment and then adding the source file and header file.

Choose Project> Open project.

Enter the project name. Here is test. PRJ, and the blue area below is shown in the test working directory. OK.

The project has been established. For example, this project is an empty project without any source files. Next we need to compile the source files. There are three ideas.

Method 1: Use the BC editor to create a file and add it to the project.

Use File-> New to create a New File, enter the source code, save it, and change the File name to the name you want ., The following test. cpp implements a simple addition.

This file can be compiled and run independently, but it is not added to the test Project. The Project: TEST at the bottom shows that it does not contain any files, to Add this file to the Project, first move the cursor to the lower area, a green line will appear, and then choose Project> Add item to find the file to be added, here is test. cpp (we can write multiple source files at a time and Add them at a time), Add them to the project, and Done completes this operation. After adding the file, we will find that the newly added file is successfully added in the file list of the Project column under the display area. If you want to remove an added file, you can operate it in Project> Delete item.

We can Compile and run it again. For the difference between Compile and Build all in the menu, the first one is to Compile the current file, and the other one is to Compile the entire project.

Method 2: directly create a file to the Project

The first method is to create a file that needs to be added to the project, so how to create a file that is included in the project at the beginning.

Step 1: locate the Project column below the display area (green bar appears in the Project column), and do not locate the Project column. Some sub-menus of the Project menu item cannot be selected.

Choose Project> Add item from the menu, enter the file Name in the Name edit box, and click Add. Done completes the operation and can Add multiple files at a time. For example, I have added the test1.cpp file.

 

You can double-click the file name in the file list under the display area to edit the file. For example, we write the main function in TEST. in CPP, call the method-added operation function in the TEST1.CPP file. In the TEST1.CPP file, we implement two functions (addition and subtraction) and put the function declaration in the header file TEST1.H. The header File can be created using File-> New, but the File name must be changed to TEST1.H (note the suffix) when saving the File. After testing, the header File only needs to be placed in the working directory, you do not need to add it to the project. Otherwise, an error will be reported and the program will automatically search for the current directory (working directory or the same directory as the source code ). Save File-> Save All at any time. During the operation, I found that the file name is case-insensitive.

 

Note that test1.h is a custom header file. It must be enclosed in double quotation marks (") to indicate that it is in the current directory.

 

Build all and Run again to get the result. Or Run

 

So far, we have created a small program. We can open our working directory and find all the files, engineering files, and output files we have created are in it.

Method 3: Use a third-party editor to write a program file, create a project in BC, and add a file. (Recommended)

The previous two methods are to directly use the editor provided by BC to edit the program. We all know that this editor is very good. It is very troublesome to edit the text, search, replace, and locate it. We can use a third-party editor, the simplest such as Notepad. We recommend that you use the Notepad ++ editor for beginners. This editor has a Chinese version, which is Chinese by default during download and installation, you can easily download and use it.

Using a third-party Editor, we can write the program file and save or copy it to the working directory. For example, I can add two program files test2.cpp (multiplication and division algorithm) and test2.h (header file)

 

Save to working directory

Open BC Software

By default, the Project is opened before the last exit. (if it is not opened, it will be opened in the Project menu.) But we cannot see it here. In this case, we will go to the menu Window> Project. the project bar is displayed at the bottom of the display area. (This operation is often used)

In this case, the test2.cpp file does not exist in the project. You can add the file to the project in the previous two methods. Menu Project-> Add item, we will find that BC automatically lists test2.cpp, we can Add it.

Modify test. cpp

Compile and run the result.

 

During all the previous operations, we put the source files and header files directly under the root directory of the working directory. In the actual development of large projects, the source files are usually stored in a subfolders in the working directory, and the header files are stored in another subfolders in the working directory. For example, we can put all the source files in the source folder and put the header files in the include folder,

We create another project from start to end.

 

1. Project-> Open Project

 

 

2. Project-> Add item. Open the source folder and Add the source files.

At this time, we will compile the project and find the following errors:

The system prompts that the header files test1.h and test2.h cannot be found. That's of course, because we didn't tell BC that we put the header file in the include folder, by default, bc will find these two header files under the root directory of the test folder in the working directory. Of course, they cannot be found. How can we tell the BC header file where it is located? There are two methods

First, modify the path of the pre-defined header file in the source code to test. cpp is used as an example. The first two header files do not need to be modified. The next two files need to find the corresponding header file and change it to the following format. What does this mean ,. /indicates the meaning of the previous directory, ". /include/test1.h "indicates test1.h in the include folder in the upper directory of the source code directory, so that the program can find it. Similarly, test1.h and test2.h are modified.

 

Second: The program can find stdio. h and other built-in header files, because BC adds the folders where these header files are located to the include directory, we can also add our own header file directory.

Options-> Directories: Use semicolons to separate the Directories in the folders where the header files are added. After the program is saved, the program can be compiled and run again.

 

All paths involved in the program must be clarified. Because the program is dead, it cannot be found if you don't tell it. This is the most common case when reading files, for example, to read an image, you must specify the path of the image relative to the source file to the corresponding code.Write it here temporarily



From Weizhi note (Wiz)



Attachment List

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.