Sublime Forum

Sublime text not compiling valid C++ code

#1

I decided to learn C++ and use Sublime text build 4126 on latest macos version to build my code. I use the Build system C++ single file without any other configuration.

#include <iostream>
#include <fstream>

using namespace std;

int main() {
	cout << "Please enter input file name: ";
	string iname;
	cin >> iname;
	ifstream ist{iname};
}

When building this, sublime text outputs: “error: expected ‘;’ at end of declaration” yet all lines have ;.
Upon debugging, I noticed that if I change:

ifstream ist{name}
to
ifstream ist(name) code compiles. I don’t understand why since ifstream ist{iname} is valid syntax.

0 Likes

#2

Sublime Text just starts an external compiler. Maybe the used compiler doesn’t support this syntax feature. If it requires special arguments to be enabled, those would need to be added to the command of the corresponding build system configuration.

1 Like

#3

Works fine with gcc.

0 Likes

#4

facing the same issue :frowning:

0 Likes

#6

If you are on a mac, you need to create a new build system. Tools ->Build System > New build system.

Then you need to use gcc with your parameters. If you search on Github: ‘mac sublime build system’ you are going to find an example.

Hope it works!

0 Likes

#7

Thanks for your reply!

0 Likes

#8

Thanks. I managed to setup my own build system with gcc.

0 Likes

#9

I suppose you were using clang (as part of Xcode?) because you’re using a Mac. The clang compiler defaults to the c++98 dialect. The syntax you’re using requires at least the c++11 dialect. To do so, all you need is an additional flag on the command-line: -std=c++11. Or rather use -std=c++2a for the most modern version of C++.

GCC on the other hand, defaults to the c++14 dialect. Hence your code works without adjustments to the default build system packages in sublime text.

0 Likes

#10

Thanks for the detailed reply.

0 Likes