Sublime Text C++ Build System - c++

I use the following compiler settings to compile my C++ code:
g++ -Wall -Wextra -O2 -fwrapv -Wfloat-equal -Wconversion -std=c++17 A.cpp
But I am not sure how to setup the build system for the purpose, nor I want to try myself! I doubt that I may mess up the configuration (because I did once).
So I need help to configure my build system for the above compiler settings. How can I do that?

This build system works in my machine, basically its the default C++ build system with the extra options,
{
"shell_cmd": "g++ -Wall -Wextra -O2 -fwrapv -Wfloat-equal -Wconversion -std=c++17 \"${file}\" -o \"${file_path}/${file_base_name}\"",
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
"working_dir": "${file_path}",
"selector": "source.c++",
"variants":
[
{
"name": "Run",
"shell_cmd": "g++ -Wall -Wextra -O2 -fwrapv -Wfloat-equal -Wconversion -std=c++17 \"${file}\" -o \"${file_path}/${file_base_name}\" && \"${file_path}/${file_base_name}\""
}
]
}
You can place it in /Users/{user}/Library/Application Support/Sublime Text 3/Packages/User directory for MacOs, C:\Users\{user}\AppData\Roaming\Sublime Text 3\Packages\User in Windows or ~/.config/sublime-text-3/Packages/User in Linux.
You can call the file something like C++17.sublime-build to differentiate it from the default build system.

Related

Sublime Text 3 problem with compiling C++ programm

So I have following problem, I installed MinGW and it works I can manually compile my program using this g++ filename in cmd, and it crates exe file which I can run in cmd and see result. But when I try to use sublime text 3 and run it from there there is following error "[WinError 2] The specified file could not be found".
This is build system which I found and tried to use:
{
"shell_cmd": "g++ -Wall -Wextra -O2 -fwrapv -Wfloat-equal -Wconversion -std=c++17 \"${file}\" -o \"${file_path}/${file_base_name}\"",
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
"working_dir": "${file_path}",
"selector": "source.c++",
"variants":
[
{
"name": "Run",
"shell_cmd": "g++ -Wall -Wextra -O2 -fwrapv -Wfloat-equal -Wconversion -std=c++17 \"${file}\" -o \"${file_path}/${file_base_name}\" && \"${file_path}/${file_base_name}\""
}
]
}
Which I found here Sublime Text C++ Build System
Im not sure if I describe my problem correctly so if you have more question feel free to ask.
Thanks :)

Can't build even a simple program in Sublime Text 3

I have just switched from Python to C++ for implementing Data Structures and Algorithms. I found that Sublime Text 3 was quiet powerful. I installed it, added my Mingw-64 compiler to the path and also added a "build system". I hoped it would be suffice to build and run any basic C++ program. But when I run
#include <iostream>
using namespace std;
int main() {
int n;
cin>>n;
cout<<n<<endl;
return 0;
}
I know, there is some problem either in the build part or the compiler settings. I tried different compilers, from code blocks to independent Mingw-64 compiler without any success. In the past, I have used Code Blocks, it never required me to create an exe file or reference it. If I restart the program, it will show permission denied error which I know why it occurs.
Here is the error:
The system cannot find the file G:\Programming\C++\second.exe.
[Finished in 15.0s with exit code 1]
[shell_cmd: g++ "G:\Programming\C++\second.cpp" -o
"G:\Programming\C++/second" && "G:\Programming\C++/second"] [dir:
G:\Programming\C++] [path: C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\ProgramData\Oracle\Java\javapath;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program
Files (x86)\ATI Technologies\ATI.ACE\Core-Static;C:\Program
Files\Intel\WiFi\bin\;C:\Program Files\Common
Files\Intel\WirelessCommon\;C:\Program Files (x86)\Windows
Live\Shared;C:\Program Files\MATLAB\MATLAB Production
Server\R2015a\runtime\win64;C:\Program Files\MATLAB\MATLAB Production
Server\R2015a\bin;C:\Program Files\MATLAB\MATLAB Production
Server\R2015a\polyspace\bin;C:\Python27\;C:\Python27\Lib\site-packages\PyQt4;C:\Program
Files\Git\cmd;C:\Program Files
(x86)\mingw-w64\i686-7.1.0-posix-dwarf-rt_v5-rev0\mingw32\bin;C:\Users\80LM0141IH\Anaconda3;C:\Users\80LM0141IH\Anaconda3\Library\mingw-w64\bin;C:\Users\80LM0141IH\Anaconda3\Library\usr\bin;C:\Users\80LM0141IH\Anaconda3\Library\bin;C:\Users\80LM0141IH\Anaconda3\Scripts;C:\Users\80LM0141IH\AppData\Local\Programs\Python\Python36\Scripts\;C:\Users\80LM0141IH\AppData\Local\Programs\Python\Python36\;C:\Users\80LM0141IH\AppData\Local\Microsoft\WindowsApps;G:\Microsoft
VS
Code\bin;C:\Users\80LM0141IH\AppData\Local\GitHubDesktop\bin;C:\Users\80LM0141IH\AppData\Local\Microsoft\WindowsApps;";C:\Program
Files (x86)\Graphviz2.38\bin";C:\Program Files
(x86)\Graphviz2.34\bin;]
Before we start, you need to make sure that you have installed a C++ compiler and configured its path correctly. Make sure that you can invoke g++ command in the command line.
I see from your code that you need input from the standard input. Sublime Text's console can not accept input. So that maybe that is the problem. You need to run this program in a terminal
Try to replace your build system with following settings:
{
"shell_cmd": "g++ -std=c++11 -Wall \"${file}\" -o \"${file_path}/${file_base_name}\" && \"${file_path}/${file_base_name}\"",
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
"working_dir": "${file_path}",
"selector": "source.c++, source.cpp, source.cc, source.cxx",
"variants":
[
{
"name": "Run in Terminal",
"linux": {
"shell_cmd": "g++ -std=c++11 -Wall \"${file}\" -o \"${file_path}/${file_base_name}\" && xterm -e '${file_path}/${file_base_name} && echo && echo Press ENTER to continue && read line && exit'",
// "shell_cmd": "g++ -std=c++11 -Wall \"${file}\" -o \"${file_path}/${file_base_name}\" && gnome-terminal -e 'bash -c \"${file_path}/${file_base_name}&& echo && echo Press ENTER to continue && read line && exit\"'", // for gnome-terminal
// "shell_cmd": "g++ -std=c++11 -Wall \"${file}\" -o \"${file_path}/${file_base_name}\" && xterm -e '${file_path}/${file_base_name}; bash'", // for xterm
// "shell_cmd": "g++ -std=c++11 -Wall \"${file}\" -o \"${file_path}/${file_base_name}\" && xterm -hold -e ${file_path}/${file_base_name}", // for xterm
// "shell_cmd": "g++ -std=c++11 -Wall \"${file}\" -o \"${file_path}/${file_base_name}\" && konsole --hold -e ${file_path}/./${file_base_name}", // for konsole
},
"windows":{
"shell_cmd": "g++ -std=c++11 -Wall \"${file}\" -o \"${file_path}/${file_base_name}\" && start cmd /k $file_base_name "
// "shell_cmd": "g++ -std=c++11 -Wall \"${file}\" -o \"${file_path}/${file_base_name}\" && start \"$file_base_name\" call $file_base_name"
},
"osx":{
"shell_cmd": "g++ -std=c++11 -Wall \"${file}\" -o \"${file_path}/${file_base_name}\" && xterm -e '${file_path}/${file_base_name} && echo && echo Press ENTER to continue && read line && exit'",
},
"shell": true,
},
]
}
Press Ctrl + Shift + B and choose C++ - Run in Terminal. It will compile and run the program in your cmd.
I can run your code snippet correctly in my environment. Let me know if you encounter any problem.

GCC to compile compile 64 bit

I'm trying to compile C++ with G++ in 64 bit.
I'm using sublime text 3. This is my build system file :
{
"cmd": ["g++ ", "-m64", "-std=c++0x", "$file", "-o", "${file_path}/${file_base_name}"],
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
"working_dir": "${file_path}",
"selector": "source.c, source.c++, source.cxx, source.cpp",
"variants":
[
{
"name": "Run",
"cmd": ["bash", "-c", "g++ -m64 -std=c++0x '${file}' -o '${file_path}/${file_base_name}.exe' && xterm -e bash -c '\"${file_path}/${file_base_name}.exe\" ; read'"]
}
]
}
Compiled files are still created as 32 bit.
When I run g++ --print-multi-lib I get:
32;#m32
x32;#mx32
I'm sure this has something to do with it, but I'm not sure how to fix it, (Pretty new to this), I've tried installing multilib 64 but nothing has changed.
Any help appreciated. Thank you :)
Your sublime text 3 IDE is confusing and spoiling you and you misconfigured it.
I hope that you are using some POSIX operating system, e.g. Linux.
Learn first to compile on the command line (in some terminal, outside of your editor or IDE). You absolutely need to understand how to invoke the GCC compiler (with commands in a terminal). Here are a few hints.
Be sure to have a recent GCC compiler, at least GCC 6 if you want C++11 or better. Perhaps you need to upgrade your compiler.
compiling a simple single-source program
To compile a single-source C++ program in single.cc into tinyprog executable, use
g++ -std=c++11 -Wall -g single.cc -o tinyprog
FYI, -std=c++11 sets your C++ standard, -Wall ask for almost all warnings (and you might even add -Wextra to get more of them), -g is for debug information (in DWARF) and -o tinyprog sets the output executable. Do ls -l tinyprog, ldd tinyprog, file tinyprog to understand more about your executable.
compiling a simple multi-source program
To compile a small multi-source C++ program, e.g. made of first.cc and second.cc into a smallprog executable
compile each of these into object files
g++ -std=c++11 -Wall -g -c first.cc -o first.o
g++ -std=c++11 -Wall -g -c second.cc -o second.o
link these object files into your executable
g++ -std=c++11 -Wall -g first.o second.o -o smallprog
building more complex programs
If you are using extra libraries, you may need to add some extra -I includedir option at compile time, and some extra -L libdir and -l libname options at link time. You might want to use pkg-config to help you. You could, if curious, add -v after g++ to be shown the actual steps done by g++
I run g++ --print-multi-lib
This just queries how your g++ compiler has been configured. You could also try g++ -v alone.
Once you are able to compile by (one or several) commands, consider using some build automation tool. To build a single program using GNU make, you could take inspiration from this.
Once you are fluent with compiling thru commands (either directly or thru some build automation tool like make or ninja or thru your script), read the documentation of your editor or IDE to configure it suitably.

How to use clang-tidy with relative paths?

I'm using clang-tidy to perform some analysis on my C++ code. For this, I'm using the Clang JSON compilation database. It is working on the development machine but I cannot reuse the compilation database on another machine because all the paths in the compilation database are absolute and the other machine does not have the same current working directory. Here is an example in the JSON:
{
"command": "clang++ -g -fprofile-arcs -ftest-coverage -Iinclude -std=c++1y -Wextra -Wall -Qunused-arguments -Wuninitialized -Wsometimes-uninitialized -Wno-long-long -Winit-self -Wdocumentation -pedantic -Iinclude -ICatch/include -Werror -MD -MF debug/test/algorithm.cpp.d -o debug/test/algorithm.cpp.o -c test/algorithm.cpp",
"directory": "/home/wichtounet/dev/cpp_utils_test",
"file": "test/algorithm.cpp"
},
Ideally, the directory, should be the current working directory. I tried replace the absolute directory path with "." and let it empty and also remove it, but nothing worked.
Currently, the only solution I have is to use a script replace all the previous absolute paths by the paths of the current working directory, but this is highly unpractical...
Is it possible to use the same compilation database on different machine ?

g++ fatal error: no input files windows

I'm trying to set windows programming enviroment for c++. I use the Visual Studio to write the code but my University wants me to use g++ compiler. So far I managed to link g++ with the PATH using cmd but that's not all. I have to use an alias which in linux is:
p1++="g++ -ansi -Wall -O2 -DNDEBUG -Wextra -Werror -Wno-uninitialized -Wno-sign-compare -Wshadow
I tried the same using the command doskey:
doskey p1=g++ -ansi -Wall -O2 -DNDEBUG -Wextra -Werror -Wno-uninitialized -Wno-sign-compare -Wshadow
and it works but whenever I use for example:
p1++ hello.cpp
it says
g++: fatal error: no input files
compilation terminated.
but if i use:
g++ hello.cc
It does compile, so my question is what does all that code mean and how can I get rid of that error?
Thanks
test.cpp (assumed to exist at C:\test.cpp):
#include <iostream>
int main(void) { std::cout << "hello" << std::endl; return 0; }
Here's what it looks like in my Windows CMD prompt:
NOTE: No quotes in alias, and it is assumed that g++ is already in your PATH.
C:\>set p1++=g++ -ansi -Wall -O2 -DNDEBUG -Wextra -Werror -Wno-uninitialized -Wno-sign-compare -Wshadow
C:\>%p1++%
g++: fatal error: no input files
compilation terminated.
C:\>%p1++% test.cpp
C:\>a.exe
hello
C:\>doskey p1++=%p1++% $*
C:\>p1++ test.cpp
C:\>a.exe
hello
If you wanted to have the %p1++% environment variable persist across CMD prompts then you'd need to add an environment variable to your Windows User Profile.
On Windows 7:
Control Panel => System => Advanced System Settings => Environment Variables.
Create a new User Variable with name = p1++ and value = g++ -ansi -Wall -O2 -DNDEBUG -Wextra -Werror -Wno-uninitialized -Wno-sign-compare -Wshadow.