简体   繁体   中英

How do I run a C++ Program in Sublime 3 (Ubuntu)?

So I have started to use Sublime Text 3 recently with my Ubuntu OS. I wanted to test it out so wrote a simple piece of c++ code. But when I try to build it does nothing, I have checked online and still nothing I even installed a build system ( https://github.com/shikharkunal99/Sublime-Build-System ) and still whenever I go to build it just opens open a black section at the bottom (see picture)

在此处输入图片说明

Install g++ to run c++ code

apt-get install g++

Then I will tell you a personal trick that I used. it is:

find | grep "part of your filename"

Replace "part of your filename" section with the name of the file or a part of the name of the file. Suppose, the file name is Here.c. I type "Here" in place of part of your filename. Then the final step, type

./a.out

Output is ready in front of you.

This post will help you in setting up Sublime Text 3 in a way that leads to a good workflow specifically for C++ programming environment (Ubuntu, GNU C++ Compiler) :

Note: Only the following step is essential for running c++ programs.

1. Create a Build System in Sublime Editor :

Sublime Text provides build systems to allow users to run external programs.

  1. Go to Tools -> Build System -> New Build System .

  2. Paste the following code in the file

{
    "cmd": ["g++ -Wall -Wextra -O2 -pthread -H -std=c++17 \"${file}\" -o runfile && ./runfile <input.in> output.out"], 
//above line works fine if input.in and output.out files are present in same directory in which .cpp file is present else add complete address of these files for using them as common input output files in your system.
    "shell":true,
    "working_dir":"$file_path",
    "selector":"source.c,source.c++,source.cpp",
    "variants": [
   { 
       "name": "Variant Run",
       "cmd" : ["gnome-terminal -- bash -c \"g++ $file_name ;echo ------------Output-------------; ./a.out;echo;echo;  echo Press ENTER to continue; read line;exit; exec bash\""
     ],
   }
 ]

}
  1. Save the file (By default the file is placed in "~/.config/sublime-text-3/Packages/User" dir) something like "C++17.sublime-build" to differentiate it from the other build system files.

  2. Create input.in and output.out text files in your working directory. This can be used for piping input from the input.in file, and output to the output.out file.

2. Setup window layout :

  1. Create three new c++ file, file.cpp. Select View > Layout > Columns : 3. This will create three columns in the workspace. Select View > Groups > Max Columns : 2 .

  2. Write a hello world program & save inputs if any in the input.in file, and test its working. Use Shift+Ctrl+B and Select C++17 to build and execute the file (If selected C++17 - Variant Run it will execute the program in a separate terminal window like a normal program would).
    The windows will look like this when you are done.
    Layout Preview

3. Precompile headers :

Generally useful in competitive programming, we can speed up compilation time by precompiling all the header files as mentioned here , ie by precompiling the bits/stdc++.h header file.

  1. For this, first, navigate to the stdc++.h file. This will be located at a directory similar to ~/usr/include/x86_64-linux-gnu/c++/9/bits Open terminal window here.
  2. Run the command sudo g++ -std=c++17 stdc++.h , to compile the header. Take care to use the same flags you used in your build system. Check to make sure that the stdc++.h.gch file was created in that directory.

4. Sublime Text features :

Snippets & Completion
Read up on the documentation of snippets and completions at the official guide.

5. Other Features :

Read https://scotch.io/bar-talk/best-of-sublime-text-3-features-plugins-and-settings

This program works perfectly fine for me using Build 3120, and I expect it will work fine with previous builds. First, you need to select Tools → Build System → C++ Single File ( Tools → Build System → Automatic should also work, but I prefer to be explicit). Then, either hit Ctrl Shift B or select Tools → Build With… and select C++ Single File - Run . This will compile your .cpp file to an executable in the same directory as the source file, then run it.

东西

Well I also got various issues with this thing finally I got an amazing thing in the package control pallet.Follow the instructions:

1.Open up the Package control Pallet

2.Search for C++ Builder

3.You will see C++ Builder-Mingyang Yang

4.click it and then wait for a couple of seconds

5.finally go to tools->build system->select C++ Builder-Mingyang Yang

6.finally tap the Shift+Ctrl+B and then select C++ Builder-Mingyang Yang Build and Run

7.finally here you go you can not only build this but also use the console for input

Note:This will execute only when there is gcc compiler included in the terminal otherwise at first install gcc by the command apt-get install gcc then you can use c++

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM