Sublime Forum

编译c++出现中文乱码

#1

我已经将自建的编译系统放在图片上,请问如何修改c++的编译系统才能编译c++像编译C那样正常显示中文?

到此我已经找到解决办法了(2018.01.25)

{
“cmd”: [“g++”, “${file}”, “-fexec-charset=gbk”,"-o","${file_path}/${file_base_name}"],
“file_regex”: “^(…[^:]):([0-9]+):?([0-9]+)?:?(.)$”,
“working_dir”: “${file_path}”,
“encoding”:“cp936”,
“selector”: “source.c”,
“variants”:
[
{
“name”: “Run”,
“cmd”: [“cmd”,"/C",“g++”, “${file}”,"-fexec-charset=gbk", “-o”,"${file_path}/${file_base_name}", “&&”,“start”,“cmd”,"/c", “${file_path}/${file_base_name} & pause”]
}
]
}

0 Likes

Sublime Text versus Visual Studio Code in 2019
#3

You cannot use input on Sublime Text because it does not support full terminal features. See:

Also, you seem to be only compiling the program, instead of building it and running it, see this:


0 Likes

#4

thank you very much. I am a beginner and I don’t understand those documents. I only hope that you can give me the correct build system code. thank.

0 Likes

#5

I’m sorry, there is no way to do what you want with a Sublime Text build system. cin will not work.

0 Likes

#6

Try this:

{
    "working_dir": "${file_path}",
    "cmd":["bash", "-c", "g++ -std=c++1y '${file}' -o '${file_path}/${file_base_name}' && '${file_path}/${file_base_name}'"],

    "encoding": "utf-8",
    "selector": "source.c, source.c++",
    "file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
}

Sublime Text usually requires better understand of inner workings of things, i.e., study much more, and for a beginner, Visual Studio Community, should be easier to learn C++:

0 Likes

#9

Your original code will not work as @wbond explained. You cannot do input with Sublime Text build system. You need to either use another editor or learn/study more about Sublime Text. I would suggest you to give a try on VSCode. May be it will be easier for you to learn. Otherwise, stick with Visual Studio Community.

0 Likes

#10

An alternative to what @addons_zz is if you like Sublime Text is to use it as your text editor.

IDEs tend to try to put all tools into one application, so you don’t have a lot of options in what parts to use. Text editors tend to focus more energy on the editing of the code, and leave all of the other tasks to specialized tools.

If you use Sublime Text as your text editor, you can use something like PowerShell to compile and execute the code. Most software projects have tools/systems to build that are separate from an IDE. Using a full terminal program for invoking and interacting with your compiled program allows you to utilize all of the features that terminal, and use the one that best fits your requirements and preferences.

For what its worth, that is how I work when working on Sublime Text. I use Sublime Text to write the code, and I build and run the program in my favorite terminal on each platform.

2 Likes

#11

I also do it sometimes either when I did not get the time to integrate it with the Sublime Text build system, or because it cannot be integrated due usage of non-supported features by Sublime Text as cin. The advantage of integrating an external build system with Sublime Text are features like file_regex, which allow directly double-clicking the error messages and jumping directly to the source code line, and even showing the error messages directly above the source code line (inline error messages, phantoms).

Other advantages are the ones provided by packages like https://github.com/asfaltboy/SublimeTestPlier for Python. It does parse the current source code file and detect on which Unit Test I have around the cursor/caret. Then, allows you to run directly only that test. Additionally, in my fork of the package, I allowed it to remember and run the last test it had ran, then, I can directly run the last test when I am browsing the source code (i.e., fixing a test on TDD). These functionalities, are usually provided out of the box by IDE’s while you can integrate them into text editors by robust extensions. I just hope in the future, Sublime Text manages to fix and complete its API, then, features like these and even more robust, can be directly integrated by extensions.

Another point for IDE’s are the integrated debuggers, there are some years I do not use any of them and just code everything directly with Sublime Text as my sole editor. However, the other day, I almost had to recur to one of them because Python was exiting with failure status, while no errors/exceptions whatsoever where being printed to the screen. The problem was on Python system shutdown (object destruction). The stream sys.stderr already had been destroyed when the exception occurred, then, the only thing Python could do was to exit with an error status code. How am I supposed to fix something, if cannot print anything to the screen and I have no ideia from where it is coming? I got lucky and manage to fix it by reading the documentation and online forums, then, without recurring to either a graphical or command line step by step debuggers.

0 Likes