How do I run cpp program directly from Vim? [duplicate] - c++

This question already has an answer here:
How to compile a cpp file directly from vim editor in Windows?
(1 answer)
Closed 2 years ago.
I've recently installed Vim on my Windows 10 and also the g++ compiler through mingw.
I want to directly compile and run my cpp program from Vim.
I've tried the following command.
:!g++ hello.cpp -o hello
below is the image for the reference
https://imgur.com/iYe0aVv

Probable answer to your problem: give the full paths: % for the source and %< for the executable.
Also, please, copy the message, avoid screenshots.
And finally, you should have a look at this question from this morning... IOW, prefer using quickfix feature. How to compile a cpp file directly from vim editor in Windows?
Regarding the execution on the current (monofile) program, it's
:!./%<
" or
:term ./%<
(you may have to add .exe after %<. IIRC, this is not necessary)

Related

How can I compile, assemble and link a C++ file using Clang?

I have been coding for over 5 years and would now like to take a step away from IDE's and try a project without one. I have the things I need to get started (I think), a HelloWorld.cpp file, the Windows Command Prompt open and Clang installed.
Now that I have these things my question is this - What do I need to type into the Command Prompt to make Clang take my C++ code in the HelloWorld.cpp file and compile it into a separate file containing the assembly code, and then make Clang take my assembly code and assemble it into a separate file containing the object code, and then finally make Clang take my object code and link it into a separate file containing the machine code?
Ultimately meaning at the end I will have 4 files, one with C++ code, one with assembly code, one with object code and finally one with machine code. The point of all of this being the ability to read and understand each stage of the process before running the file containing the machine code.
Being someone who has left the world of IDE's for the first time, I find the official Clang documentation very confusing and cannot find a straight answer to my question.
Same as with GCC, and I'll do you one better by first preprocessing the source file. In principle Clang also can emit LLVM bitcode or LLVM IR as two extra intermediate stages.
clang++ source.cpp -E
clang++ source.ii -S
clang++ source.s -c
clang++ source.o
This last one gives a.out as an executable file. You can define the output file for each command by appending
-o output.file
The extensions may not be 100% correct. Just check what comes out.

Meaning of ./main command in terminal [duplicate]

This question already has answers here:
Why do you need ./ (dot-slash) before executable or script name to run it in bash?
(9 answers)
Closed 7 years ago.
I am working with this c++ project for making command line text editor like Vim. The documentation tells that in order to run this text editor in terminal you need to write command ./main in the project folder.
The project folder has main.cpp file. Is this a command to execute that file (may be I am wrong) or this command is a standard terminal command.
Thank you.
Running ./main means run main from the current directory (.).
The current directory is normally missing from $PATH so you have to specify it explicitly.

Creating a bash script to compile a c++

I have a program I've written in c++ which outputs some simulation results to a .csv excel file.
According to some instructions I need to create a simple bash script that would run the .cpp file given the command "$ run_program" ($ is not a part of the command).
I've looked on Stackoverflow and other sites however I have not found a concrete answer to help me. I would also greatly appreciate it if those who answer can take some time to explain what the parameters mean.
Thank you.
How I should make a bash script to run a C++ program?
This is one of the links I've looked at, however I could not make heads or tails out of this.
i dont know the command you are using to compile your c++ program but this might help you.
Create a file with ".sh" extension and open it with your favorite text editor.
Paste this code (change compiling line with line you are using to compile your progam)
#!/bin/bash
#Run this in terminal
#+ Command to compile c++ program. here i used common one
g++ filename.cpp -o anyname
exit 0
Now you need to run this script, To do this open a terminal
chmod u+x scriptname.sh
Then run the script by ./scriptname.sh
Hopefully this will compile your program.
It sounds like a Makefile is what you are looking for here. Definitely worth getting a handle on if you deal with programming.

set Vim as a basic C++ Editor [duplicate]

This question already has answers here:
Configuring Vim for C++
(3 answers)
Closed 9 years ago.
I want to set Vim to work with C++, I just want to perform these tasks:
write code (you don't say?)
check and highlight C++ syntaxis
autocompletion (if is possible)
compile, run, debugging and return to the editor
tree-view project files on the side
statusbar
I know that much of this tasks can be done with plugins, so I need your help to make a list of required plugins and how to set them up together.
why basic? well, I'm taking the programming course level 1 in my university, and we will make simple command-line programs, simple such a mathematical evaluations (functions, array even or odd numbers, draw triangles with asterisks and so.)
I don't think you need any plugins... the features you want are already there.
-write code (you don't say?)
this is a given
-check and highlight C++ syntax
:syntax enable
-autocompletion (if is possible)
in insert mode, try
ctrl-n
ctrl-p
-compile, run, debugging and return to the editor
vim is an editor, not a complier. You can, however, drop into a shell to run these commands or use :!commandname. Try one of the following
ctrl-z
g++ -o myprogram myprogram.cpp
fg
or
:!g++ -o myprogram myprogram.cpp
or just keep another terminal open.
-tree-view project files on the side
:!tree -C | less -R
-statusbar
already at the bottom. Try gvim for more toolbars et cetra.
Have fun!
BTW - this message was brought to you via vim and pentadactyl
Some plugins that might help you and I tried in the past when I was trying to get started with vim long ago:
IDE: http://www.vim.org/scripts/script.php?script_id=213
Tree view: http://www.vim.org/scripts/script.php?script_id=1658
Debugging: http://www.vim.org/scripts/script.php?script_id=3039
Completion: http://ctags.sourceforge.net/ and http://www.vim.org/scripts/script.php?script_id=1520
Statusbar: http://www.vim.org/scripts/script.php?script_id=3881 and its successor http://usevim.com/2013/01/23/vim-powerline/
You can search for further plugins at http://www.vim.org/scripts/index.php
That being said, I use vim just fine without any plugin for daily C++ development. It is also handy because I can use the same workflow when ssh'ing onto a server or someone else's machine without the consideration of major differences.
Also C++ syntax highlight works by default as such plugins for languages are usually included into the distributed vim, already.

Get the compiler options from the program [duplicate]

This question already has answers here:
Detect GCC compile-time flags of a binary
(4 answers)
Closed 9 years ago.
Is there any macro in c++ (using gcc) to get the compilation options used to build the executable ?
I'm sure I saw something like that in some about dialogs.
any help will be appreciated
PS: while the question in Detect GCC compile-time flags of a binary interests in finding the options activated to compile a program, I'm interesting in finding the exact command line options used to compile my program from within this program source.
Apart from creating the compile string from the
Common Predefined Macros
, which seems hectic. I think there is an easy way to do it. The gcc -V on debian gives back flags used for configuration.
However, my shot would be to get full command in ./configure equivalent step and dump it to some file like config_line.h as a define.
Something like:
./configure:
#!/bin/sh
echo "#define conf_flags \"configured with: "$*"\"" >> config_line.h
#do some configuration steps here
#maybe even compilation itself
Then:
luk32:~/projects/tests$ ./test.sh --with=test
luk32:~/projects/tests$ cat ./config_line.h
#define conf_flags "configured with: --with=test"
You get full config line defined in the external file under a define statement. I think its fairly straight forward and easy to use. And no need for much compiler magic.
It is also worth of noting you can most probably (if not always) create such file(s) right before the actual compilation so they are actually up-to-date and valid during compilation step. Answer in get-the-compiler-options-from-a-compiled-executable would imply the executable already exists, which might be a bummer in some cases.
Note: I gave bash example, but I'm pretty sure you can output similar header file under any half-descent build system, be it make, qmake, cmake, etc. the bash begin the simplest case.
I think most of them have access to the command line they are invoked with, as well as they provide easy way to get actual compile command. For example to provide two literals, one with commands used for make like -j 13 and another g++ ... used for actual compilation step performed by make.
Note2: I know this is not an answer the OP asked, but I guess it serves his purpose in the 1st place.
Because I'm using qmake build system I came across this solution :
I added this line to the end of my pro file :
QMAKE_CXXFLAGS += -DFLAGS=\"$$QMAKE_CXXFLAGS $$QMAKE_CXXFLAGS_RELEASE\"
then retrieved what I want from the FLAGS macro