Sublime Forum

C++ build For sublime text that can handle file name containing space

#1

{
“cmd”: [“g++.exe”,"-std=c++17", “${file}”, “-o”, “${file_base_name}.exe”, “&&” , “${file_base_name}.exe<inputf.in>outputf.in”],
“shell”:true,

“working_dir”:"$file_path",
“selector”:“source.cpp”
}

Currently I am using this build for my c++ code, but it can’t handle file that has a space in its name, what i should change in the above build

0 Likes

#2

As in an interactive shell, filenames need to be quoted to ensure whitespace are not interpreted as argument separators.

Note shell: true is deprecated. You may want to use shell_cmd instead to pass the whole command line as single string, which also makes \" quoting more obvious.

{
	"shell_cmd": ["g++.exe -std=c++17 \"${file}\" -o \"${file_base_name}.exe\" && \"${file_base_name}.exe<inputf.in>outputf.in\""],
	"working_dir": "$file_path",
	"selector": "source.cpp"
}
0 Likes

#3

Thank you for your time but the build is not working

0 Likes

#4

Notjing is building right now

0 Likes

#5

I can give only forma advice at this topic as I left C++ behind a decade ago.

My hunch from command line however is it maybe building, but the execution after && getting stuck cause of trying to read from stdin, which is not supported by ST’s default build system runner.

It’s designed to display compiler output, only.

You’d want to try to setup Terminus as build runner instead, if interactive execution is required.

0 Likes

#6

Input should work fine considering the input and output are defined by the inputf.in and outputf.in files in the build system.

@nathan45 What error are you getting?

0 Likes

#7

When you use cmd in a build, you specify an array of strings, with the first one being the thing to run and the remaining being arguments as single strings.

So as defined, your build looks like it will build a thing just fine if the input file has a space in it, because "${file}" is the full argument, and so is "${file_base_name}.exe".

So, I would guess that your original problem is not that it can’t compile the executable, it’s that the last argument in your build is:

“${file_base_name}.exe<inputf.in>outputf.in”

Note that here this is considered to be a single argument, so if your input file has a space in it (say it is my file here.cpp, then the shell is going to see it as:

my file here.exe<inputf.in>outputf.in

What is that doing? It’s trying to execute a program named my with the arguments file and here.exe. I’m going to guess that’s not what you want or expect.

As mentioned by @deathaxe, you should not be using cmd with "shell": true . That indicates that what you’re trying to do is run a shell command, and that is what shell_cmd is for.

When you use shell_cmd you enter exactly what you would enter in a terminal, and that includes the fact that in the shell files with spaces are a gigantic pain and you need to wrap them in double quotes.

So, I would try something like this:

{
	"shell_cmd": "g++.exe -std=c++17 \"${file}\" -o \"${file_base_name}.exe\" && \"${file_base_name}.exe\" <inputf.in>outputf.in",
	"working_dir": "$file_path",
	"selector": "source.cpp"
}

This is similar to what @deathaxe shared above, except that shell_cmd should be a string and (crucially), the quoting on the file base name was not in the correct spot.

0 Likes

#8

I applied the build you provided, It is building the file “Helloworld.cpp” but it is not building or showing any output for the file “Hello world.cpp”. The build is not handling the file whose have a space in the file name

0 Likes