1 work environment
(1) PC system: Ubuntu12.04LTS.
(2) editor version: Sublime Text 3
2 achieves its purpose
the background is that I recently started using Sublime Text 3 to edit code, found it very easy to use, and was attracted by its powerful plug-in features. However, using the build that comes with Sublime after editing C/C++ code isn’t easy, so I decided to customize a single-file C/C++ compilation command myself.
3 custom C compilation
(1) in the sublime toolbar, select “tools” -> “Compiling system” -& GT; “New build system” opens with a file name called “Untitled. Sublime -build”. Edit it, add the following code, save it as “myC.sublime-build” and make the path the default.
{
//"shell_cmd": "make"
"working_dir": "$file_path",
"cmd": "gcc -Wall \"$file_name\" -o \"$file_base_name\"",
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:?(.*)$",
"selector": "source.c",
"variants":
[
{
"name": "Run",
"shell_cmd": "gcc -Wall \"$file\" -o \"$file_base_name\" && \"${file_path}/${file_base_name}\""
}
]
}
After saving, you’ll find something sublime in the “tools” -& GT; “Compiling system” -& GT; See myC’s build system under “New build system”.
(2) edit simple C code for testing
// File name:test_c_build.c
#include <stdio.h>
int main(int argc, char const *argv[])
{
printf("hello world!\n");
return 0;
}
Pressing the “Ctrl” + “Shift” + “b” brings up the compilation command selection window
select “myc-run” to compile and the results appear on the console below sublime, as follows:
4 custom C++ to compile
C++ the process is the same as that of C, only with a slightly different file content when creating a new compilation system:
{
// "shell_cmd": "make"
"encoding": "utf-8",
"working_dir": "$file_path",
"shell_cmd": "g++ -Wall -std=c++0x \"$file_name\" -o \"$file_base_name\"",
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:?(.*)$",
"selector": "source.cpp",
"variants":
[
{
"name": "Run",
"shell_cmd": "g++ -Wall -std=c++0x \"$file\" -o \"$file_base_name\" && \"${file_path}/${file_base_name}\""
}
]
}
5 The difference between myC and MyC-Run is that myC only compiles, not executes; Myc-run, on the other hand, is directly executed after compilation.
6 solves the problem that sublime has no input in its own console
because sublime has no input in its own console, so if the program USES functions like cin, the program cannot be executed. In Windows system, you need to call CMD. exe and hand the console over to CMD, so you need to add the command to call CMD when compiling the configuration file.
modify the above myC++.sublime-build as follows,
{
// "shell_cmd": "make"
"encoding": "utf-8",
"working_dir": "$file_path",
"shell_cmd": "g++ -Wall -std=c++0x \"$file_name\" -o \"$file_base_name\"",
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:?(.*)$",
"selector": "source.cpp",
"variants":
[
{
"name": "Run",
"shell_cmd": "g++ -Wall -std=c++0x \"$file\" -o \"$file_base_name\" && \"${file_path}/${file_base_name}\""
},
{
"name": "RunInCmd",
"shell_cmd": "g++ -Wall -std=c++0x \"$file\" -o \"$file_base_name\" && start cmd /c \"\"${file_path}/${file_base_name}\" & pause \""
}
]
}
It’s just adding this sentence,
&& start cmd /c \"\"${file_path}/${file_base_name}\" & pause \"
After saving,
write the sample program myC++_test_example.cpp. It is as follows:
#include <iostream>
#include <string.h>
using namespace std;
int main ()
{
string str;
cout << "please enter a string" << endl;
cin >> str;
cout << "input string: " << str << endl;
return 0;
}
execution result:
7 currently only supports compiling single files, the rest will be discussed later.
Read More:
- Ubuntu 20.04 Desktop Install sublime-text Error: “Certificate verification failed”
- C#: How to get the value or text value of the select drop-down list
- [Solved] Linux C++ warning: ISO C++ forbids converting a string constant to ‘char*‘ [-Wwrite-strings]
- MAC: Clion configure C compiler Error: The C compiler identification is unknown
- [Solved] Ubuntu Eclipse C/C++ Error: launch failed.binary not found
- [Solved] Execute ./configure Error: [error]configure: error: You need a C++ compiler for C++ support.
- [Solved] C++: fatal error: Killed signal terminated program cc1plus
- error: C compiler cannot create executables [How to Solve]
- Read and write BMP image with Pure C language
- [Solved] configure: error: no acceptable C compiler found in $PATH
- Chinese garbled problem when running. C file in Linux
- C# implementation of TXT document to table example code
- C# WinForm gets the storage path of the selected file
- Ubuntu18.04 Compile A40i SDK Error: misc/create_inode.c:395:18: error: conflicting types for ‘copy_file_range‘
- [Solved] c++: fatal error: Killed signal terminated program cc1plus
- Vector series in actual C + +_ To_ fit()
- C Language Compilation Error: variably modified ‘* *’ at file scope
- [Solved] kali Execute history -c Error: fc: event not found: – C
- C# WPF Framework: How to Build Caliburn.Micro Quickly
- [Solved] Linux Compile Error: error: ‘for’ loop initial declarations are only allowed in C99 mode