Sublime Text 3 Build Many Source Files - c++

Well all I've seen for build systems for C++ is the following:
"build_systems":
[
{
"name: "g++ test",
"cmd": ["g++", "${file}"],
"shell": true
}
{
"name": "echo test",
"cmd": ["echo", "${file}"],
"shell": true
}
]
Just as a test, this only works for the currently opened file, which might not even be a source file nor part of the sublime project. So this is good for a test program that only has one source but it isn't that useful otherwise.
Question 1:
Is there any ${project_files} variable with regex to accept only .cpp files ?
Question 2:
I would want to pass these files to qmake, such that it generates a .pro project file. Such that if I add a file to a sublime project it and build the file will be added to the project file. How would I do this without shell script or otherwise such that it's cross platform ?

Related

How to compile and run .cpp files after writing them in sublime text?

I really prefer writing code in sublime text or anything else. So, naturally that's what I want to use. However, when I try to open the file in Netbeans, I get an error. So, I want to know how I can save a .cpp file from sublime text and then go about running it through the command prompt. I know I have to set up a path or something, but I'm not exactly sure how to do it. Thanks for any help at all. Also, I am new to C++ and programming in general(have dabbled in Python a bit).
EDIT: Really sorry, I meant how do I actually execute/run the file afterwards. Like if the program were to just print out "Hello World".
The following build system should suit your needs, assuming that you're using the GNU Compiler Collection and g++ for compiling your .cpp files:
{
"cmd": ["g++", "${file}", "-o", "${file_base_name}"],
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
"working_dir": "${file_path}",
"selector": "source.c, source.c++",
"variants":
[
{
"name": "Run",
"cmd": ["${file_base_name}"]
}
]
}
Please note that the following instructions are for Sublime Text 2 only...
To use it, select Preferences -> Browse Packages... to open the Packages folder in Windows Explorer. It should be located in C:\Users\YourUserName\AppData\Roaming\Sublime Text 2. Or not, depending on your install. In either case, browse to the C++ directory and open the file C++.sublime-build in Sublime and set the syntax to JSON if you want it to look prettier. Replace its entire contents with the code above, then save the file. The old code is kind of convoluted, and also runs some commands needlessly.
Now, set the build system by going to Tools -> Build System and selecting Automatic. Assuming that g++ is in your PATH, you can build your executable using the CtrlB keyboard shortcut, also available via Tools -> Build. If your binary has already been compiled, you can run it by pressing CtrlShiftB.
One final note: if your program asks for any kind of input, or has a graphical user interface, this Run command won't work. Instead, replace it with the following:
"name": "Run",
"cmd": ["start", "cmd", "/k", "${file_path}/${file_base_name}"],
"shell": true
This will open a new instance of the command line and run your program from there, instead of inside Sublime. The /k switch means that the window will be kept open after your program has run, so you can examine output, errors, etc. If instead you want the window to close immediately, simply change the /k to /c.
Good luck!

Sublime Text 2 file include pattern

I know that a file exclude pattern can be set in Sublime Text's project/default settings, but is it also possible to only include files based on a pattern?
For example, when working with my C++ projects, I tend to have files in the same folder that are not necessarily source files.
Can I explicitly tell Sublime Text to show me only *.cpp, *.h, and *.txt files while ignoring everything else?
List the extensions you want included in file_include_patterns. The following works for me with ST3 project files, don't have ST2 handy to test.
{
"folders":
[
{
"name": "My Project",
"path": ".",
"file_include_patterns": [
"*.cpp",
"*.h"
]
}
]
}

build and compile files outside mingw main folder

I want to be able to save my files in E:\Documents\C++ and then be able to run and compile them. My MinGW location is E:\MinGW\bin. Everything is saved on a USB flash drive. I am using SublimeText to run and compile these files. Everything works fine if i save the C++ files in the E:\MinGW\bin folder. I just want to be able to change where to save the files and build and run them. Also I am using this as my sublime-build file
{
"cmd": ["g++", "${file}", "-o", "${file_path}/${file_base_name}"],
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
"working_dir": "${file_path}",
"selector": "source.c, source.c++",
"variants":
[
{
"name": "Run",
"cmd": ["${file_path}/${file_base_name}.exe"]
}
]
}
Your path should contain E:\MinGW\bin (or you should give full path for g++)
You want to change your home directory and your path for mingw.
This page shows how to set your home directory, and where to set your PATH,
http://www.mingw.org/wiki/HOWTO_Set_the_HOME_variable_for_cmd_exe
Here is a page that tells you how to build a cpp file,
http://www.mingw.org/wiki/MinGW_for_First_Time_Users_HOWTO
If you want to use the same home location for MINGW and windows,
http://mingw.5.n7.nabble.com/making-home-directory-on-msys-agree-with-Windows-td22176.html
Or, you can set your home directory to your flash drive.

Efficiently Using SublimeText with SublimeClang for CMake/C++ Projects

I have been trying to play with SublimeText2 for some time now. While it is very easy to work with Python in it almost out of the box, working with C++ is a bit more tricky. I can manage to set up a CMake build script by copying and modifying the existing Makefile script, but there are many things that just don't work as they do in a CMake supported IDE, like Eclipse CDT. SublimeText 2 does not seem to understand the concept of a separate build directory, it also cannot get me autocomplete through SublimeClang, if I include the libraries with reference to the directories added in CMake. SublimeClang keeps complaining that it cannot find the libraries, and when I try to #include, it cannot even offer me autocomplete on standard STL header file names, e.g., algorithm. If someone has a pipeline figured out, I would be obliged to hear about it.
I have asked this question in more general purpose usage-related forums before, where I did not get any response, which is why I thought of posting it here.
I use Sublime Text 2 with CMake and SublimeClang. I also use SublimeGDB. My build directory is under [project root]/build. Have a look at my project file and see if it helps you:
{
"folders":
[
{
"path": "."
}
],
"build_systems":
[
{
"name": "Build",
"cmd": [ "make", "-C", "build" ],
"file_regex": "/([^/:]+):(\\d+):(\\d+): "
}
],
"settings":
{
"sublimegdb_commandline": "gdb --interpreter=mi myapp",
"sublimegdb_workingdir": "build",
"sublimeclang_options" :
[
"-Wno-reorder"
],
"sublimeclang_options_script": "${project_path:scripts/compileflags.rb} ${project_path:build}"
}
}
The compileflags.rbscript is used to search for flags.make files in the CMake build tree which is where CMake keeps its compile flags. These flags are needed so that SublimeClang knows where to find your includes.
Here is that script, located under scripts/:
#!/usr/bin/env ruby
# Searches for a flags.make in a CMake build tree and prints the compile flags.
def search_dir(dir, &block)
Dir.foreach(dir) do |filename|
next if (filename == ".") || (filename == "..")
path ="#{dir}/#{filename}"
if File.directory?(path)
search_dir(path, &block)
else
search_file(path, &block)
end
end
end
def search_file(filename)
return if File.basename(filename) != "flags.make"
File.open(filename) do |io|
io.read.scan(/[a-zA-Z]+_(?:FLAGS|DEFINES)\s*=\s*(.*)$/) do |match|
yield(match.first.split(/\s+/))
end
end
end
root = ARGV.empty? ? Dir.pwd : ARGV[0]
params = to_enum(:search_dir, root).reduce { |a, b| a | b }
puts params

Sublime Text 2 and MinGW

Good day!
Can anyone share their experience how to attach MinGW-compiler to Sublime?
I found a config in the internet, but when I run compiled program popping bugs with missing files from "../MinGW/bin/".
Config:
{
"cmd": ["mingw32-g++.exe", "-o", "$file_base_name", "$file_name"],
"path": "c:\\Program Files\\MinGW\\bin\\"
}
Thanks!
UPD
I found answer for my question! I had to add one parameter in cmd. It's "-static".
So, it's my MinGW.sublime-build, which works fine:
{
"path": "c:\\Program Files\\MinGW\\bin\\",
"cmd": ["mingw32-g++.exe", "-static", "-o", "$file_base_name", "$file"]
}
Make sure to include the bin file in the "Path" variable on your system.
Open the start menu and type "variable" or "environment variable" (or google it) to find how to do it. You'll get in a Window with a lot of variables, find the Path (and not PATH) variable and add the path to the bin folder of MinGW.
And btw, as suggested, you should change file_base_name by file, and put file_base_name where you put file_base.
Here's the command I personally use:
"cmd": ["C:\\MinGW\\bin\\mingw32-g++.exe", "-Wall", "-time", "$file", "-o", "$file_base_name"]
You should be using $file instead of $file_name. $file_name expands to only the name whereas $file expands to the full path.
The changed config would be
{
"cmd": ["mingw32-g++.exe", "-o", "$file_base_name", "$file"],
"path": "c:\\Program Files\\MinGW\\bin\\"
}