Compile C/C++ with gVim 8.1 in Windows 10 (MinGW) - c++

Since everything is 32-bit, I used :set makeprg=mingw32-make, and then tried compiling via :!make, which gave the following error message:
'make' is not recognized as an internal or external command, operable program or batch file.
shell returned 1
I tried the basic "Goodbye World" stuff:
#include <iostream>
int main() {
std::cout << "Goodbye World";
return 0;
}
What am I doing wrong? Other than being a noob.

:!make doesn't run 'makeprg'. It runs make. Since you are specifically trying to run mingw32-make.exe, presumably you don't have an executable named make.exe. (See :help :!)
:make runs 'makeprg'. (See :help :make, :help 'makeprg')
my goal is to see the "Goodbye World" either in console or terminal or any observable way. Would this require additional tinkering to the _vimrc file?
You would rather want to do it by tinkering with your makefile. Any arguments to :make will be passed to 'makeprg'. If you defined targets clean (to remove the Make artefacts) or run (to execute the product of your Make), you will be able to write :mak clean or :mak run in Vim to run those targets. Obviously, you can create your own commands and/or mappings to make those :make commands faster.

Note (and I completely overlooked this myself, silly me) that when you run a simple code, such as the one above, that does not need extensive file managements and Makefiles, all you need to go is :!gcc % for C and :!g++ % for C++. I believe (and I may be wrong, but this is a simple observation) that in Vim, :! indicates the following commands are to be executed in the system Command Prompt, and gcc/g++ is the command to invoke the GNU Compiler for C and C++ respectively. Finally, % is used to indicate the current filename, with all extensions intact. This is extremely useful in cases of keymapping, as I have done via the following:
nnoremap <F5> :!g++ %<CR>|"Press Func.5 to Compile via GNU Comp.

Related

i don't know what is wrong with vs code in running cpp

i just started to code in cpp and nothing is working idk if i didn't install the gcc properly or what but i already set the path of the bin file idk why it refuses to run
the code:
#include<iostream>
int main()
{
std::cout<<"hello";
}
and the problem is that when I try to use the "code runner extension" it is not working so I just press f5 and then when I get the error messages which says at first "could not find the task file'c/c++:g++.exe build active file' " and I get three options 1:debug anyway
2:configure task
3:cancel
when I choose debug anyway I get this error here
Since you're on windows consider installing Visual Studio or CLion. They're more beginner friendly - VSCode can get tricky to run c++ on windows. Either way, looks like you're trying to run your project without building it first. There should be a build option right next to the run option. Try building it, then running. The build is what compiles and creates the project.exe file, which is what the compiler is saying isn't there.
The referenced IDE's always auto-build on run, so there's that
If you're familiar with using the command line, these commands will do what you want, assuming the .cpp file is in your current directory.
g++ <FILENAME>
./a.out
There are wonderful flags you can add to the first command (the compiling command) like -Wall, -Wextra, -o, and many more. But, since it seems like you're just starting out, there's no need to worry about those!
If you're not familiar with the command line, I would encourage you to become familiar! It is an extremely powerful tool for a programmer. Here is a YouTube video that talks about it.

What is lldb's equivalent one of gdb's start command?

I heavily used gdb before, and now give lldb a shot. I like gdb's start command very much, but I can't find the equivalent one from lldb's manual. Now I can only use "b main" followed by run compound instead. So just curious whether there is an equivalent one in lldb? Or I can only use the compound of "b main" and run commands as a work-around.
You are correct, lldb doesn't have a dedicated start command. The stated motivation for that command is that gdb supports lots of runtimes that don't use a "main" symbol. That makes determining where user code begins non-trivial, and it's useful to have a command that figures that out for you. We haven't had a need for that in lldb yet.
If you always use start to run programs in gdb, then you can just set a breakpoint on main in your ~/.lldbinit file. That will get copied to any new targets that get made in your lldb session, and run will behave exactly like start (for runtimes that use a main symbol).
If it's something you would use a lot but not always, you could make your own version fairly easily using the python extension point in the command interpreter:
https://lldb.llvm.org/use/python-reference.html#create-a-new-lldb-command-using-a-python-function
Also, feel free to file an Enhancement Request with http://bugs.llvm.org.

C++ System(); function not working as expected / Windows console commands in C++

Hello friendly people of stack overflow!
I am currently working on a project using an Arduino Uno. Because i create all my files and sketches using a c++ program, i want to eliminate the Arduino IDE from my workflow. For that i can very easily use avrdude (which the IDE uses anyway) and some windows console commands.
These are the commands that i am using:
"C:\Program Files (x86)\Arduino\hardware\tools\avr/bin/avrdude" "-CC:\Program Files (x86)\Arduino\hardware\tools\avr/etc/avrdude.conf" -v -patmega328p -carduino -PCOM4 -b115200 -D -Uflash:w:C:\Users\Jzargo\AppData\Local\Temp\arduino_build_766345/EPaper_TestDither.ino.hex:i
"EPaper_TestDither.ino" is the arduino Sketch i want to compile and upload. When using the console and manually inserting the above commands, everything works as expected.
And here comes the part I am struggeling with:
Because i also dont want the user to manually open the console and type in some gibberish code, i want to integrate this command into my c++ program using the system(); function:
system("\"C:\\Program Files(x86)\\Arduino\\hardware\\tools\\avr/bin/avrdude\" \" - CC:\\Program Files(x86)\\Arduino\\\hardware\\tools\\avr/etc/avrdude.conf\" -v -patmega328p -carduino -PCOM4 -b115200 -D -Uflash:w:C:\\Users\\Jzargo\\AppData\\Local\\Temp\\arduino_build_766345/EPaper_TestDither.ino.hex:i");
When executing this function, the command cannot be executed because "Der Befehl "C:\Program" ist entweder falsch geschrieben oder konnte nicht gefunden werden.", which roughly translates to "The Command "C:\Program" is not written correctly or cant be found".
I do not understand why the console accepts the command when manually inserting it, but not when using the system(); function.
I hope you can help me figure this out.
Edit: By using
subst H: "C:\Program Files(x86)\Arduino\hardware\tools\avr\bin" and
system("\"H:/avrdude \"-CC:/Program Files (x86)/Arduino/hardware/tools/avr/etc/avrdude.conf\"\" -v -patmega328p -carduino -PCOM4 -b115200 -D -Uflash:w:C:/Users/Jzargo/AppData/Local/Temp/arduino_build_833906/EPaper_TestDither.ino.hex:i");
I was able to upload my sketch. Note the changed Placement of \".
But for some reason, this does not work when using C:\Program Files(x86)\Arduino\hardware\tools\avr\bin instead of H:.
Kindest regards
J'zargo
The command looks messed up with respect to the parameters, although I don't see how exactly that triggers your specific error.
The beginning is OK. The path is properly quoted (double quotes, protected by backslashes from the C compiler). But why do you have slashes and backslashes mixed? In some online examples I saw that people use forward slashes in Windows paths (C:/whatever...) ; that seems to work and is easier than using double backslashes all the time (but it should not trigger your — or any — error).
So system("\"C:\\Program Files(x86)\\Arduino\\hardware\\tools\\avr/bin/avrdude\" ... should call the right executable. Why don't you try that on its own (without parameters) to see whether the error persists?
I suspect that \" - CC:\\Program Files(x86)\\ ... is not correct though. avrdude expects a parameter -C<path>, not - C<path> (note the badly placed spaces before and after the dash).
As an aside, it may not hurt to quote parameters that contain funny characters like colons which may have special meanings.
The general advice for this kind of trouble:
Work in and with paths that do not contain spaces, brackets, or other non-identifier characters. If you don't want to change the avrdude installation path you can use the DOS subst command to create a drive whose root is C:\Program Files(x86) or even C:\Program Files(x86)\Arduino\hardware\tools\avr\bin, e.g. subst H: "C:\Program Files(x86)\Arduino\hardware\tools\avr\bin". The command would then be H:/avrdude :-).
If confronted with a bug you don't understand, simplify the problem radically until a toy version works; then add complexity bit by bit until you encounter the error; that should make it easier to recognize what triggered it.
Edit: I'm not sure this example is valid because I used the msys2 development environment and ran the example in a bash shell; I'm not even sure cmd is called as the system shell by the syste call!
In order to check the system call semantics I wrote the following minimal example (which uses mixed slashes/backslashes as a test). The current directory has a sub directory called "some dir" containing a minimal program showargs which simply writes its command line parameters to stdout:
$ ls -l "some dir" && echo && cat cmdline.c && echo && gcc -o cmdline cmdline.c && ./cmdline.exe
total 56
-rwxr-xr-x 1 Peter None 56097 Apr 16 17:23 showargs.exe
#include <stdlib.h>
int main(int argc, char **argv)
{
system("\".\\some dir/showargs\" 1 2 3");
}
->.\some dir/showargs<-
->1<-
->2<-
->3<-

Mlton compiler not working (not giving any output)

Installed the MLton compiler on Ubuntu (sudo apt-get install mlton) and had no problems (seemingly) with installation.
When I try to use it (e.g. "mlton test.sml") it sits for a second and then returns nothing. If I try to print something in the file I'm trying to compile, nothing. However, weird part is if I give it bad ML code ("x = 2", without val), it spits out regular errors like "Undefined variable," etc.
I've looked on here, and elsewhere online, and nothing seems to concern what I'm experiencing.
Perhaps I'm just using it wrong?
Thanks in advance.
mlton is a non-interactive compiler; it compiles the program, and that's it. You can run the program later if you want.
So, for example, if test.sml is a valid Standard ML program, then this:
mlton test.sml # compile the program
will compile it and emit a Linux executable file named test. You then run that executable file like this:
./test # run the program
If you want to compile and run the program with a single command, you can use your shell's && feature to run two programs (but only running the second if the first one had succeeded):
mlton test.sml && ./test

What is a 'shebang' line?

Currently I'm trying to start programming on my new Mac. I installed TextWrangler, and chose C++ as my language of choice; since I have some prior knowledge of it, from when I used Windows.
So, I wrote the ever so common "Hello World" program. Although, when I tried to run it, I got an error:
"This file doesn’t appear to contain a valid ‘shebang’ line (application error code: 13304)"
I tried searching the error code to find out how to fix this, but I couldn't find anything.. I have no idea what a 'shebang' line is... Can someone help me out?
You need to compile it with a compiler first. I assume you tried to run the source file like ./source but C++ doesn't work this way.
With some compilers however, you can provide a shebang-line as the first line of the source file (the #! is known as shebang or crunchbang, hence the name), like so:
#!/path/to/compiler
So that the shell knows what application is used to run that sort of file, and when you attempt to run the source file by itself, the compiler will compile and run it for you. That's a compiler-dependent feature though, so I recommend just plain compiling with G++ or whatever Macs use to get an executable, then run that.
While I wouldn't recommend it for regular C++ development, I'm using a simple shell script wrapper for small C++ utilities. Here is a Hello World example:
#if 0 // -- build and run wrapper script for C++ ------------------------------
TMP=$(mktemp -d)
c++ -o ${TMP}/a.out ${0} && ${TMP}/a.out ${#:1} ; RV=${?}
rm -rf ${TMP}
exit ${RV}
#endif // ----------------------------------------------------------------------
#include <iostream>
int main(int argc, char *argv[])
{
std::cout << "Hello world" << std::endl;
return 0;
}
It does appear that you are trying to run the source file directly, however you will need to compile using a C++ compiler, such as that included in the gcc (GNU Compiler Collection) which contains the C++ compiler g++ for the Mac. It is not included with the Mac, you have to download it first:
from http://www.tech-recipes.com/rx/726/mac-os-x-install-gcc-compiler/ : "To install the gcc compiler, download the xcode package from http://connect.apple.com/. You’ll need to register for an Apple Developer Connection account. Once you’ve registered, login and click Download Software and then Developer Tools. Find the Download link next to Xcode Tools (version) – CD Image and click it!"
Once it's installed, if you are going for a quick Hello World, then, from a terminal window in the directory of your source file, you can execute the command g++ HelloWorld.cpp -o HelloWorld. Then you should be able to run it as ./HelloWorld.
Also, if you're coming from a Visual Studio world, you might want to give Mono and MonoDevelop a try. Mono is a free implementation of C# (and other languages), and MonoDevelop is an IDE which is very similar to Visual Studio. MonoDevelop supports C# and other .NET languages, including Visual Basic .NET, as well as C/C++ development. I have not used it extensively, but it does seem to be very similar to VS, so you won't have to learn new everything all in a day. I also have used KDevelop, which I liked a lot while I was using it, although that's been a while now. It has a lot of support for GNU-style development in C/C++, and was very powerful as I recall.
Good luck with your endeavors!
Links:
Mono: http://mono-project.com/Main_Page
MonoDevelop: http://monodevelop.com/
KDevelop: http://kdevelop.org/
shebang is http://en.wikipedia.org/wiki/Shebang_%28Unix%29.
not sure why your program is not running. you will need to compile and link to make an executable.
What I find confusing (/interesting) is C++ program giving "Shebang line" error. Shebang line is a way for the Unix like operating system to specify which program should be used to interpret the rest of the file. The shebang line usually points to the path of the interpreter. C++ is a compiled language and does not have interpreter for it.
To get the real technical details of how shebang lines work, do a man execve and get that man page online here - man execve.
If you're on a mac then doing something like this on the commandline:
g++ -o program program.cpp
Will compile and link your program into an executable called program. Then you can run it like:
./program
The reason you got the 'shebang' error is probably because you tried to run the cpp file like:
./program.cpp
And the shell tries to find an interpreter to run the code in the file. Because this is C++ there is no relevant interpreter but if your file contains Python or Bash then having a line like this
#!/usr/bin/python
at the 1st line in your source file will tell the shell to use the python interpreter
The lines that start with a pattern like this: #!/.../.../.. is called a shebang line. In other words, a shebang is the character sequence consisting of the characters number sign and exclamation mark (#!).In Unix-like operating systems, when a text file with a shebang is used as if it is an executable, the program loader mechanism parses the rest of the file's initial line as an interpreter directive. The loader executes the specified interpreter program, passing to it as an argument the path that was initially used when attempting to run the script, so that the program may use the file as input data.