Home Development Tools notepad Learn how to compile C/C++ with Notepad++ in one article

Learn how to compile C/C++ with Notepad++ in one article

Aug 14, 2019 pm 02:15 PM
c c++ notepad++ compile

Learn how to compile C/C++ with Notepad++ in one article

1. Previous story

The IDE (integrated development environment) I usually use is Dev-Cpp, because it is just In the learning stage, the code is still less than a thousand lines. Personally, I don’t think there is a need for a comprehensive version like VS, and there are some aspects of VC6.0 that are not pleasing to the eye (not just aesthetically).

But recently I found that when copying the C code file to Ubuntu 18.04 for compilation and running, the Chinese characters will always be garbled. Because of the encoding (character encoding) problem, Ubuntu only recognizes UTF-8, and Windows only recognizes ANSI. Although I can make VIM display ANSI in Ubtuntu, I can't make it display in the terminal. Dev-Cpp on Windows cannot set the encoding to UTF-8, so I want to use Notepad as Editor and add Compiler. Function.

Environment: win7. Notepad. Dev-Cpp.

2. Step

1. Configure the compiler

Download and install MinGW from the official website

change:

Learn how to compile C/C++ with Notepad++ in one article

Select "mark for installation" of "mingw32-gcc-g -bin" and select "Apply Changes" of "Installation" in the upper right corner. Can. There were no other problems when I didn't install the other items. Later, other problems arose and I had to ask for insurance before installing them. In total, just under 400MB was installed.

Related recommendations: "Notepad usage graphic tutorial"

2. Edit environment variables

Learn how to compile C/C++ with Notepad++ in one article

Control Panel\All Control Panel Items\System, Advanced System Settings→Environment Variables, find the PATH variable in the "Administrator's User Variables" column, create a new one if there is none, edit if there is one, the variable name is "PATH", the variable value Add a "C:\MinGW\bin;" on the original basis. This value is related to the location where each person installs MinGW and varies from person to person. The significance of ";" is that when the value of PATH has multiple items, use it to separate these items.

3. Two methods to check whether the first two steps are successful

Learn how to compile C/C++ with Notepad++ in one article

Enter the cmd console and enter gcc -v or g - vView the compiler version that has been added to the environment variables. It is normal to display the content in the yellow box, indicating that the first two steps were successful.

Create a simple C source code file to ensure that the code can run correctly. For example, Hello.cpp:

//Hello.cpp
#include<iostream>  
using namespace std;
int main()
{
    cout<<"Hello world, 世界你好!"<<endl;
    return 0;  
}
Copy after login

In the cmd console cd to the directory of the source code, then execute g Hello.cpp -o Hello.exe, and then execute the Hello.exe /k command. It can run normally. It shows that the first two steps are fine.

The former command is to use the g compiler to compile the source code and output the exe file to this directory, and the latter command is to run the exe file (/k parameter makes it stay in the program interface after running, as opposed to / c will close it. That is the comparison between keep and close). The first two commands can actually be combined into one using "&&", that is, g Hello.cpp -o && Hello.exe Hello.exe /k.

We are already more than half successful here, because as you can see, you can already compile and run the source code in the cmd console. Giving Notepad this ability only allows Notepad to "compile the currently opened source file in one step". Just use cmd to execute compilation and run commands."

4. Notepad adds run command

Open Notepad and press F5 to bring up "Run". Copy the command mentioned later and run it. It is recommended to save it with any name.

Learn how to compile C/C++ with Notepad++ in one article

You can also find "Run (R)" in the toolbar

Referenced a lot of information about Notepad run commands and cmd commands, modified My last command is cmd /k pushd "$(CURRENT_DIRECTORY)" && g -o "$(NAME_PART).exe" "$(FULL_CURRENT_PATH)" && "$(NAME_PART)".exe & PAUSE & EXIT (no branch, It is a whole command). To understand this command, you can divide it into six, namely:

(1) cmd /k: Open the cmd console, run the program and let it stay without automatically closure.

(2) pushd "$(CURRENT_DIRECTORY)": Change the working path to the path where the source file is located. For example, 'pushd E:\kkk' is equivalent to e: and then cd kkk in cmd. This command This is especially important when the source code calls files in the same directory and only writes relative paths. Because Notepad's default working path is its own installation path.

(3) g -o "$(NAME_PART).exe" "$(FULL_CURRENT_PATH)": Call the compiler g .exe in the environment variable to compile the source code into an exe file with the same name and output it to the same path.

(4) "$(NAME_PART)".exe: Run the executable file compiled from the source code.

(5) PAUSE: Pause, prompting "Press any key to continue", and cooperate with the next command to achieve the effect of "Press any key to close". If neither is available, the program will close in seconds after running.

(6) EXIT: Close the cmd console. If not, press any key to return to the cmd command console, waiting for the next command to be entered.

·$(CURRENT_DIRECTORY) represents the path of the directory where the file is located.

·$(NAME_PART) indicates the file name without the suffix part of the file.

·$(FULL_CURRENT_PATH) represents the current complete file path.

·Double quotation marks (half-width): used to prevent directories or file names from containing spaces.

· "&&" and "&": The former means that the previous command is executed normally before the next command is executed. If it is abnormal, the next command will not be executed; the latter is worry-free. For example, the last two subcommands after dividing into six means that regardless of whether the source code is compiled and run successfully, the words "Press any key to continue" will appear, and the cmd console will be closed after pressing any key.

3. Follow-up

After struggling for nearly half a day, I found that it was the same as Ubuntu. It only allowed the editor to display UTF-8, but it could not display it on the console. That is, Notepad can display UTF-8/ANSI, but after running, the cmd console still only recognizes ANSI.

The above is the detailed content of Learn how to compile C/C++ with Notepad++ in one article. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

C++ object layout is aligned with memory to optimize memory usage efficiency C++ object layout is aligned with memory to optimize memory usage efficiency Jun 05, 2024 pm 01:02 PM

C++ object layout and memory alignment optimize memory usage efficiency: Object layout: data members are stored in the order of declaration, optimizing space utilization. Memory alignment: Data is aligned in memory to improve access speed. The alignas keyword specifies custom alignment, such as a 64-byte aligned CacheLine structure, to improve cache line access efficiency.

Similarities and Differences between Golang and C++ Similarities and Differences between Golang and C++ Jun 05, 2024 pm 06:12 PM

Golang and C++ are garbage collected and manual memory management programming languages ​​respectively, with different syntax and type systems. Golang implements concurrent programming through Goroutine, and C++ implements it through threads. Golang memory management is simple, and C++ has stronger performance. In practical cases, Golang code is simpler and C++ has obvious performance advantages.

How to implement the Strategy Design Pattern in C++? How to implement the Strategy Design Pattern in C++? Jun 06, 2024 pm 04:16 PM

The steps to implement the strategy pattern in C++ are as follows: define the strategy interface and declare the methods that need to be executed. Create specific strategy classes, implement the interface respectively and provide different algorithms. Use a context class to hold a reference to a concrete strategy class and perform operations through it.

What are the underlying implementation principles of C++ smart pointers? What are the underlying implementation principles of C++ smart pointers? Jun 05, 2024 pm 01:17 PM

C++ smart pointers implement automatic memory management through pointer counting, destructors, and virtual function tables. The pointer count keeps track of the number of references, and when the number of references drops to 0, the destructor releases the original pointer. Virtual function tables enable polymorphism, allowing specific behaviors to be implemented for different types of smart pointers.

How to implement nested exception handling in C++? How to implement nested exception handling in C++? Jun 05, 2024 pm 09:15 PM

Nested exception handling is implemented in C++ through nested try-catch blocks, allowing new exceptions to be raised within the exception handler. The nested try-catch steps are as follows: 1. The outer try-catch block handles all exceptions, including those thrown by the inner exception handler. 2. The inner try-catch block handles specific types of exceptions, and if an out-of-scope exception occurs, control is given to the external exception handler.

How to copy files using C++? How to copy files using C++? Jun 05, 2024 pm 02:44 PM

How to copy files in C++? Use std::ifstream and std::ofstream streams to read the source file, write to the destination file, and close the stream. 1. Create new streams of source and target files. 2. Check whether the stream is opened successfully. 3. Copy the file data block by block and close the stream to release resources.

How to iterate over a C++ STL container? How to iterate over a C++ STL container? Jun 05, 2024 pm 06:29 PM

To iterate over an STL container, you can use the container's begin() and end() functions to get the iterator range: Vector: Use a for loop to iterate over the iterator range. Linked list: Use the next() member function to traverse the elements of the linked list. Mapping: Get the key-value iterator and use a for loop to traverse it.

How to use C++ template inheritance? How to use C++ template inheritance? Jun 06, 2024 am 10:33 AM

C++ template inheritance allows template-derived classes to reuse the code and functionality of the base class template, which is suitable for creating classes with the same core logic but different specific behaviors. The template inheritance syntax is: templateclassDerived:publicBase{}. Example: templateclassBase{};templateclassDerived:publicBase{};. Practical case: Created the derived class Derived, inherited the counting function of the base class Base, and added the printCount method to print the current count.

See all articles