DEV Community

Rory-lhj
Rory-lhj

Posted on

HarmonyOSNext——Write the First Cangjie Program

This article details the entire process of creating the first Cangjie project, writing, and running the first Cangjie program on the Mac system. It covers project creation, code writing, program running and debugging, as well as rerunning after code modifications. Through this article, you will master the basic operations of Cangjie projects, further consolidate the configuration of the development environment, and take the first step in Cangjie programming.

Keywords

  • Cangjie program writing
  • First Cangjie project creation
  • Cangjie code debugging
  • VSCode usage guide
  • Mac Cangjie development
  • Cangjie project running

I. Create the First Cangjie Project

1.1 Create a Project

  1. Open VSCode and use the shortcut Shift + Command + P to open the command palette.
  2. Type cangjie and select the Create Cangjie Project option.
  3. Select the "Create CJNative Cangjie Project" option.
  4. In the pop-up menu, select the "Create Executable Output Cangjie Project" option.
  5. Select the project path, such as /Users/username/Projects/CangjieProject, enter the project name, and then press Enter.
  6. After confirming the creation, the main.cj file will be automatically generated.

1.2 Open the Project
Open the project folder using VSCode.

In the Explorer sidebar, navigate to the src/main.cj file and view the default code:

main(): Int64 {
    println("hello world")
    return 0
}
Enter fullscreen mode Exit fullscreen mode

1.3 Run the Project
1.3.1 Configure the Environment
Open the terminal in VSCode.

Enter the following command to configure the environment:
source /Users/cangjie/envsetup.sh

1.3.2 Execute the Program

Enter the following command in the terminal to run the project:
cjpm run
Check the terminal output to ensure it displays:
hello world

1.3.3 Modify and Rerun the Program

Open the src/main.cj file and modify the println statement, for example:

main(): Int64 {
    println("hello world")
    return 0
}
Enter fullscreen mode Exit fullscreen mode

After saving the file, return to the terminal and ensure the environment is configured (if not, re-execute the source command).

Run the program again:
cjpm run
Check the terminal output to ensure it displays:
hello world

II. Code Debugging

Debugging is an essential part of the programming process, enabling you to efficiently identify and fix errors in your code. Below are the steps to debug a Cangjie program in VSCode:
2.1 Set Breakpoints
Open the src/main.cj file.
Click on the left side of the code line where you want to pause execution to set a breakpoint. For example, set a breakpoint on the line println("hello world").
2.2 Start Debugging
In VSCode, click the Run and Debug icon in the left activity bar or use the shortcut Control + Shift + D.
Click the Start Debugging button (green arrow) or press F5.
The program will run and pause at the breakpoint, allowing you to inspect variables and the program state.
2.3 Inspect Variables and Call Stack
While paused at a breakpoint, use the Variables panel to view current variable values.
Use the Call Stack panel to trace the sequence of function calls.
Step through the code using Step Over (F10), Step Into (F11), and Step Out (Shift + F11).
2.4 End Debugging
Click the Stop button or use the shortcut Shift + F5 to terminate the debugging session.

Summary

This guide walked you through creating your first Cangjie project, verifying the correct configuration of your development environment, and successfully writing, running, and modifying your first Cangjie program. These steps lay a solid foundation for Cangjie development and introduce code debugging in VSCode to enhance programming efficiency and code quality.

Top comments (0)