running an executable in its own working directory - c++

I added a button to my gvim toolbar which runs a .sh file. The .sh file runs scons to build my c++ application in the /build subdirectory and runs it. The problem is that when the application is running, its current working directory is the folder that contains the .sh file (not the applications /build subdirectory)! So how do I run a built c++ applications executable (linux) from a .sh file, so that its working directory would be the folder which contains executable?

Just
cd $(dirname "$0")
./exec_test
Note, you need ./exec_test, not exec_test unless the directory is actually already in PATH

Here's an example of something similar (I don't use scons.)
I add my toolbar icon with:
:amenu ToolBar.mytool :!/home/me/code/misc/foo.sh "%"
For me, when I click this, vim runs the script in the same working directory as vim.
foo.sh contains:
#!/bin/bash
set -e
# You should see the name of your file.
# It might just be "my_file.c"
echo "$1"
# This will tell you where your script is current cd'd to.
pwd
# `cd` to where the file passed on the command line is:
cd "$(dirname "$1")"
# Look for "CMakeLists.txt"
# You only need this loop if your build file / program might be up a few directories.
# My stuff tends to be:
# / - project root
# CMakeLists.txt
# src/
# foo.c
# bar.c
while true; do
# We found it.
if [[ -e "CMakeLists.txt" ]]; then
break
fi
# We didn't find it. If we're at the root, just abort.
if [[ "`pwd -P`" = "/" ]]; then
echo "Couldn't find CMakeLists.txt." >&2
exit 1
fi
cd ..
done
# I do builds in a separate directory.
cd build && make
You'd replace CMakeLists.txt with SConstruct, and the last cd build && make with scons, or something appropriate to scons.

Related

How to deploy Qt applications for Linux

I followed all the steps successfully as mention in the Qt documentation:
Qt for Linux/X11 - Building from Source
Qt for Linux/X11 -
Deployment
But I still couldn't make static Qt application, the executable generated by the above documented steps still needs Qt shared objects on other system.
Any ideas?
You need to deploy the application, for this purpose I use the utility
cqtdeployer
This utility itself collects all the necessary dependencies of your application and you do not have to spend your time on it, or you can automate this process.
You can install from github releases (Windows)
or
from snapstore (Linux)
sudo snap install cqtdeployer
You can use as follows:
Windows:
%cqtdeployer% -bin myApp -qmake path/to/Qt/5.x.x/build/bin/qmake.exe -qmlDir path/to/my/qml/files/dir
Linux:
cqtdeployer -bin myApp -qmake path/to/Qt/5.x.x/build/bin/qmake -qmlDir path/to/my/qml/files/dir
path/to/Qt/5.x.x/build/bin/qmake - This is the way qmake is used to build your program.
path/to/my/qml/files/dir - this is the path directly to your qml file (which you wrote)
And Run application with sh script (Linux) or exe (Windows)
If you'll use the version from snap then make sure that you have all the permissions.
If you need use windows version just install application from installer
Updated
If you want create a simple installer for you application just add qif option for command of cqtdeployer.
Example :
cqtdeployer -bin myApp -qmake path/to/Qt/5.x.x/build/bin/qmake -qmlDir path/to/my/qml/files/dir qif
Details on all the intricacies of cqtdeployer can be found on the official wiki project.
The best way to deploy your application is not necessarily to statically link it for the following reasons:
LGPL licencing means that your application must now be made public and may not sold (I think) - i.e. since its statically linked and the qt libs are within your executable your executable is now part of the open source.
Its a massive pain in the arse... I have gone around this loop and know the pain well.
Installing qt-everywhere is also not so great, I just don't see how you can garantee that the libraries will be the same version as the ones that your program needs.
So what I started to do was create my own script to deploy qt for me. The basic "jist" of this is that you use ldd to find out which qt libraries you need and copy them into a sub folder (./lib) within the same folder as your executable to make an install bundle.
Note: on Windows there is a deployqt application which does somthing similar (can't remember exactly what it is called).
Below I have copied a version of my deploy script. Note that it is quite old now, but I don't see why it should not work (its not written particularly well), but if not it will give you a start place. Also look out for the plugin's. In this script I have added code to copy the audio plugin since I was using that. If you are using other plugins then you will need to copy those (they are usually in sub dir's of the qt libs like .../audio)... I had a todo to try to figure out what plugins are needed from the .pro file but I never got around to that (I would have to pass in the .pro file to this script as well)...
To run, just run this script and pass in the directory that your executable lives in.
#!/bin/bash
# Rememeber start dir
START_DIR=$PWD
# Determine which dir to deploy in and cd to that dir
if [ -d "$1" ]; then
DEPLOY_DIR=$1
else
DEPLOY_DIR=$PWD
fi
echo "Deploy dir: $DEPLOY_DIR"
cd $DEPLOY_DIR
# Run ldd on all files in the directory and create a list of required qt libs
flag=false
for entry in `ldd $DEPLOY_DIR/* | grep -i qt`; do
if $flag; then
# Only add to the array if it is not already in it
if ! [[ $libsArray =~ $entry ]]; then
echo "adding $entry"
libsArray="$libsArray $entry"
fi
flag=false
fi
# If we see a "=>" then the next line will be a library
if [ $entry == "=>" ]; then
flag=true
fi
done
echo
echo
# Create the required folder structure. Note here we are need the qt audio plugin so we are going to manually copy that as well.
mkdir -p lib
mkdir -p lib/audio
# Now copy these files to the deploy directory
for entry in $libsArray; do
echo "cp -v -f $entry $DEPLOY_DIR/lib"
cp -v -f $entry $DEPLOY_DIR/lib
done
# Now get the audio lib - this is a plugin that we are using so we need these libs as well.
# Add other plugins here as well.
# TODO: maybe we can read this in from the *.pro file.
cp -v -f `qmake -query QT_INSTALL_BINS`/../plugins/audio/* $DEPLOY_DIR/lib/audio
# Go back to start dir
cd $START_DIR
Once you have all the files you need you should be able to copy the whole lot to another PC and run it. Note: you may have to set the export LD_LIBRARY_PATH=<path-to-libs> so that the libs can be found... or install the libs into somewhere like /usr/lib/your-appplication/.
But installing libs is another question/subject!

prezto prompt shows ~RVM_PROJECT_PATH instead of current working directory

I installed prezto following this guide: http://codurance.com/2015/03/16/installing-zprezto-a-quick-guide/
If I start in a regular folder that's not in git, I see the current working directory as expected in the prompt.
~/Documents/projects/
As soon as I change to directory that's managed in git, the prompt changes to:
~RVM_PROJECT_PATH [feature/awesome]
where feature/awesome is my branch name.
No matter which prompt I configure to use, as soon as I navigate to a git repo, RVM_PROJECT_PATH shows up. How do I fix this?
Put this in your ~/.zshrc
unsetopt auto_name_dirs
Thanks to mcornella in this response: https://github.com/rvm/rvm/issues/3091#issuecomment-60083194
Tested in zsh 5.2 (x86_64-unknown-linux-gnu)
I was having this issue too and I was having an old version of prezto. Updating prezto (cd to ~/.zprezto && git pull && git submodule update --init --recursive) solved the issue thanks to this update in the ~/.zprezto/modules/ruby/init.zsh:
# Load RVM into the shell session.
if [[ -s "$HOME/.rvm/scripts/rvm" ]]; then
# Unset AUTO_NAME_DIRS since auto adding variable-stored paths to ~ list
# conflicts with RVM.
unsetopt AUTO_NAME_DIRS
# Source RVM.
source "$HOME/.rvm/scripts/rvm"
...
# Prepend local gems bin directories to PATH.
else
path=($HOME/.gem/ruby/*/bin(N) $path)
fi

Bash script to change file extension using regex

I have a lot of files i've copied over from my iphone file system, to start with they were mp3 files, but app on iphone changed their names to some random staff which looks like:
1c03e04cc1bbfcb0c1237f57f1d0ae2e.mp3?extra=f7NhT68pNkmEbGA_I1WbVShXQ2E2gJAGBKSEyh3hf0hsbLB1cqnXDuepYA5ubcFm_B3KSsrXDuKVtWVAUh_MAPeFiEHXVdg
I only need to remove part of file name after mp3. Please give me a script - there are more than 600 files, and manually it is impossible.
you can use rename command:
rename "s/mp3\?.*/mp3/" *.mp3*
#!/bin/bash
shopt -s nullglob
for F in *.mp3\?*; do
echo mv -v -- "$F" "${F%%.mp3\?*}.mp3"
done
Save it to a script like script.sh then run as bash /path/to/script.sh in the directory where the files exist.
Remove echo when you find it correct already.

Perl Script in batch

I got a shell script an need to "convert" it to a batch file!
SET LOADERMID=app\run
##### SHELL PART #####
cd /d %BASEDIR%
LOADERMID=${LOADERMID//\//\\\/}
# Copy & minify index.html to dist
cat "$SRCDIR/index.html" | tr '\n' ' ' | \
perl -pe "
s/<\!--.*?-->//g; # Strip comments
s/isDebug: *1/deps:['$LOADERMID']/; # Remove isDebug, add deps
s/<script src=\"$LOADERMID.*?\/script>//; # Remove script app/run
s/\s+/ /g; # Collapse white-space" > "$DISTDIR/index.html"
echo "Build complete"
######## SHELL PART END ##########
I got the most of it working expect the following part that copies a file to another directory and does some regex/replacing stuff.
Now I don´t know how to do it in a batch file.
Is it possible to call perl in batch?
Is it possible to do that perl code in a batch file?
Thanks for help!
If you find yourself needing to run bash scripts in a windows environment, you could also try MinGW and MSYS. MSYS, in particular, gives you a nice unixy command line environment that is useful for doing real work.

Vim [compile and] run shortcut

Basically what I want is a keyboard shortcut in vim that lets me [compile and] run the currently being edited C, C++ or Python program. In psuedocode:
when a shortcut key is pressed:
if current_extension == 'c' then
shell: gcc this_filename.c -o this_filename_without_extension
if retcode == 0 then shell: ./this_filename_without_extension
else if current_extension == 'cpp' then
shell: g++ this_filename.cpp -o this_filename_without_extension
if retcode == 0 then shell: ./this_filename_without_extension
else if current_extension == 'py' then
shell: python this_filename.py
end if
end key
I realise I might be asking a bit much but would love it if this was possible!
Something like this would work. Just create filetype autocmd that map <F4> or whatever you want to save and compile and run the program. It uses exec to build the string and uses shellescape to escape the file name.
autocmd filetype python nnoremap <F4> :w <bar> exec '!python '.shellescape('%')<CR>
autocmd filetype c nnoremap <F4> :w <bar> exec '!gcc '.shellescape('%').' -o '.shellescape('%:r').' && ./'.shellescape('%:r')<CR>
autocmd filetype cpp nnoremap <F4> :w <bar> exec '!g++ '.shellescape('%').' -o '.shellescape('%:r').' && ./'.shellescape('%:r')<CR>
% is the current buffer filename. %:r is the buffer filename without extension
http://singlecompile.topbug.net seems to do more than what you want. For a simpler solution you could also just add the following to your vimrc
au BufEnter *.cpp set makeprg=g++\ -g\ %\ -o\ %<
au BufEnter *.c set makeprg=gcc\ -g\ %\ -o\ %<
au BufEnter *.py set makeprg=python\ %
au BufEnter *.[rR] set makeprg=Rscript\ %
map <F5> :call CompileGcc()<CR>
func! CompileGcc()
exec "w"
silent make
endfunc
HTH
It's 2018 now, vim 8 has released for 2 years and shipped with all the mean stream Linux distributions and Mac OS X. But a lot of vim tutorials are still teaching people something ten years ago.
You can compile your C++/Java programs in vim as convenience as Sublime Text or NotePad++ with some dedicated plugins for Vim 8 or NeoVim.
For example, the AsyncRun plugin will allow you run shell commands in background and read output from quickfix window in realtime.
See the screen capture.
Just like compiling programs in IDEs, the compilation errors will be matched by errorformat and be highlighted and become selectable. You can navigate errors in the quickfix window or continue editing while compiling.
Quick setup
Copy & paste the lines below to your vimrc:
Plug 'skywind3000/asyncrun.vim'
" open quickfix window automatically when AsyncRun is executed
" set the quickfix window 6 lines height.
let g:asyncrun_open = 6
" ring the bell to notify you job finished
let g:asyncrun_bell = 1
" F10 to toggle quickfix window
nnoremap <F10> :call asyncrun#quickfix_toggle(6)<cr>
When you input “:AsyncRun echo hello ” in the command line:
see the capture here
You will see the realtime command output in the open quickfix window.
Compile and run a single file
Compiling a single file with AsyncRun is much simpler than Sublime Text’s build system. We can setup F9 for this:
noremap <silent> <F9> :AsyncRun gcc -Wall -O2 "$(VIM_FILEPATH)" -o "$(VIM_FILEDIR)/$(VIM_FILENOEXT)" <cr>
The macros in $(..) form will be expanded as the real file name or directory, and then we will have F5 to run the executable:
noremap <silent> <F5> :AsyncRun -raw -cwd=$(VIM_FILEDIR) "$(VIM_FILEDIR)/$(VIM_FILENOEXT)" <cr>
The double quotation mark is used to handle path names containing spaces. The option -cwd=$(VIM_FILEDIR) means running the file in the file's directory. The absolute path name $(VIM_FILEDIR)/$(VIM_FILENOEXT) is used because linux needs a ./ prefix to running executables in current directory, but windows doesn't . Using the absolute path name of the binary file can handle this crossing platform issue.
Another option -raw means the output will not be matched by vim's errorformat, and will be displayed in quickfix as what it is. Now you can compile your file with F9, check the compilation errors in quickfix window and press F5 to run the binary.
Build C/C++ Projects
No matter what build tool you are using, make or cmake, project building means acting to a group of files. It requires locating the project root directory. AsyncRun uses a simple method called root markers to identify the project root. The Project Root is identified as the nearest ancestor directory of the current file which contains one of these directories or files:
let g:asyncrun_rootmarks = ['.svn', '.git', '.root', '_darcs']
If none of the parent directories contains these root markers, the directory of the current file is used as the project root. This enables us to use either <root> or $(VIM_ROOT) to represent the project root. and F7 can be setup to build the current project:
noremap <silent> <F7> :AsyncRun -cwd=<root> make <cr>
What if your current project is not in any git or subversion repository ? How to find out where is my project root ? The solution is very simple, just put an empty .root file in your project root, it will be located easily.
Let’s move on, setup F8 to run the current project:
noremap <silent> <F8> :AsyncRun -cwd=<root> -raw make run <cr>
The project will run in its root directory. Of course, you need define the run rule in your own makefile. then remap F6 to test:
noremap <silent> <F6> :AsyncRun -cwd=<root> -raw make test <cr>
If you are using cmake, F4 can be map to update your Makefile:
nnoremap <silent> <F4> :AsyncRun -cwd=<root> cmake . <cr>
Due to the implementation of c runtime, if the process is running is a non-tty environment, all the data in stdout will be buffered until process exits. So, there must be a fflush(stdout) after your printf statement if you want to see the real-time output. or you can close the stdout buffer at the beginning by
setbuf(stdout, NULL);
At the mean time, if you are writing C++ code, a std::endl can be appended to the end of std::cout. It can force flush the stdout buffer. If you are developing on windows, AsyncRun can open a new cmd window for the child process:
nnoremap <silent> <F5> :AsyncRun -cwd=$(VIM_FILEDIR) -mode=4 "$(VIM_FILEDIR)/$(VIM_FILENOEXT)" <cr>
nnoremap <silent> <F8> :AsyncRun -cwd=<root> -mode=4 make run <cr>
Using the option -mode=4 on windows will open a new prompt window to run the command, just like running command line programs in Visual Studio. Finally, we have these key mappings below:
F4: update Makefile with cmake.
F5: run the single file
F6: run project test
F7: build project
F8: run project
F9: compile the single file
F10: toggle quickfix window
It is more like build system in NotePad++ and GEdit. If you are using cmake heavily, you can write a simple shell script located in ~/.vim/script/build.sh to combine F4 and F7 together: it will update Makefile if CMakeList.txt has been changed, then exectute make.
Advanced usage
You can also define shell scripts in your dotfiles repository and execute the script with F3:
nnoremap <F3> :AsyncRun -cwd=<root> sh /path/to/your/dotfiles/script/build_advanced.sh <cr>
The following shell environment variables are defined by AsyncRun:
$VIM_FILEPATH - File name of current buffer with full path
$VIM_FILENAME - File name of current buffer without path
$VIM_FILEDIR - Full path of current buffer without the file name
$VIM_FILEEXT - File extension of current buffer
$VIM_FILENOEXT - File name of current buffer without path and extension
$VIM_CWD - Current directory
$VIM_RELDIR - File path relativize to current directory
$VIM_RELNAME - File name relativize to current directory
$VIM_ROOT - Project root directory
$VIM_CWORD - Current word under cursor
$VIM_CFILE - Current filename under cursor
$VIM_GUI - Is running under gui ?
$VIM_VERSION - Value of v:version
$VIM_COLUMNS - How many columns in vim's screen
$VIM_LINES - How many lines in vim's screen
$VIM_SVRNAME - Value of v:servername for +clientserver usage
All the above environment variables can be used in your build_advanced.sh. Using the external shell script file can do more complex work then a single command.
Grep symbols
Sometimes, If you don't have a well setup environment in you remote linux box, grep is the most cheap way to search symbol definition and references among sources. Now we will have F2 to search keyword under cursor:
if has('win32') || has('win64')
noremap <F2> :AsyncRun! -cwd=<root> grep -n -s -R <C-R><C-W> --include='*.h' --include='*.c*' '<root>' <cr>
else
noremap <F2> :AsyncRun! -cwd=<root> findstr /n /s /C:"<C-R><C-W>" "\%CD\%\*.h" "\%CD\%\*.c*" <cr>
endif
The above script will run grep or findstr in your project root directory, and find symbols in only .c, .cpp and .h files. Now we move around the cursor and press F2, the symbol references in current project will be displayed in the quickfix window immediately.
This simple keymap is enough for most time. And you can improve this script to support more file types or other grep tools in your vimrc .
That’s the practical way to build/run C/C++ projects in Vim 8 or NeoVim. Just like Sublime Text’s build system and NotePad++’s NppExec.
No more outdated vim tutorials again, try something new.
this is my map it really work you can update it to any language:
" <!----------------------------" gcc compile C files----------------------------------------!>
autocmd filetype c nnoremap <Leader>c :w <CR>:!gcc % -o %:r && ./%:r<CR>
" <!----------------------------" java compile files----------------------------------------!>
autocmd filetype java nnoremap <Leader>c :w <CR>:!javac % :r&& java %:r<CR>
" <!----------------------------" php compile files----------------------------------------!>
autocmd filetype php nnoremap <Leader>c :w <CR>:!clear && php %<CR>
now if you are ine one of those file in vim just do in normal mode:
<leader>c
I know I came here 7 years later. I tried the code of other answers and the result didn't satisfied me, so I tried this:
autocmd filetype cpp nnoremap <F9> :w<bar>term ++shell g++ %:p -o %:p:r && %:p:r<CR>
When F9 is pressed, it (like all the answers above) compiles and executes the current file. The output is displayed in a :terminal section splitted screen.
You can change the g++ command to run another language code.
It is saved in a gist, with another mapping to display the output, also in a splitted section, but in a new file, so you can save the output.
Hopefully it could help someone.
I wanted to find a shortcut too. But I didn't want to use autocmd for some reason. I used bash script. I was already using a bash script to compile and run my C/C++ codes. So, I thought, why don't I use a bash script and use it in fltplugin file for C and C++. I made two separate bash scripts. One for C and one for C++. Here is the script for C (For C++, it is also similar just change the compiler to clang++/g++,
std=c2x to std=c++20 and $filename.c to $filename.cpp),
filename=$1
compiler=clang-10
if [ $2 ]; then
if [ $2 == "-v" ]; then
FLAGS="-g -Wall -Wextra -pedantic -std=c2x"
fi
else
FLAGS="-Wall -Wextra -pedantic -std=c2x"
fi
$compiler $FLAGS $filename.c -o $filename -lm
return_value=$?
if [ $return_value -eq 0]; then
if [ $2 ]; then
if [ $2 == "-v" ]; then
valgrind ./$filename
rm $filename
fi
else
./$filename
echo
echo "[process exited $return_value]"
rm $filename
fi
fi
Saved it as run. I made it executable for all users,
$ chmod 777 run
I moved it to my user bin directory.
$ mv run ~/bin
If you dont have a user bin directory, make on. Go to your home directory, and make a directory named bin.
$ cd ~
$ mkdir bin
Then move the run (or whatever name you gave to your script) file to the bin directory.
$ mv run ~/bin
Let's move on.
Then I made a c.vim file in ~/.vim/after/ftplugin directory. And add two new key remaps to the c.vim file.
nnoremap <F9> :vertical bo term run %:r<CR>
# This will run my code with valgrind
nnoremap <F2> :vertical bo term run %:r -v<CR>
Here are some screen shots,
Clicking F9 shows me the output screen in a vim buffer,
Clicking F2 shows me the output using valgrind in a vim buffer
You can do same for C++, just make a cpp.vim file in ~/.vim/after/ftplugin directory. And make another run file (like run_cpp), save it in your user bin(~/bin) file. Make some custom key bindings, and you are good to go.