"sh: ./<file> not found" error when trying to execute a file - c++

I've come across a weirdest problem I ever met. I'm cross-compiling an app for ARM CPU with Linux on-board. I'm using buildroot, and all goes well until I'm trying to run the application on the target: I'm getting -sh: ./hw: not found. E.g.:
$ cat /tmp/test.cpp
#include <cstdio>
#include <vector>
int main(int argc, char** argv){
printf("Hello Kitty!\n");
return 0;
}
$ ./arm-linux-g++ -march=armv7-a /tmp/test.cpp -o /tftpboot/hw
load the executable to the target; then issuing on the target:
# ./hw
-sh: ./hw: Permission denied
# chmod +x ./hw
# ./hw
-sh: ./hw: not found
# ls -l ./hw
-rwxr-xr-x 1 root root 6103 Jan 1 03:40 ./hw
There's more to it: upon building with distro compiler, like arm-linux-gnueabi-g++ -march=armv7-a /tmp/test.cpp -o /tftpboot/hw, the app runs fine!
I compared executables through readelf -a -W /tftpboot/hw, but didn't notice much defference. I pasted both outputs here. The only thing I noticed, are lines Version5 EABI, soft-float ABI vs Version5 EABI. I tried removing the difference by passing either of -mfloat-abi=softfp and -mfloat-abi=soft, but compiler seems to ignore it. I suppose though, this doesn't really matter, as compiler doesn't even warn.
I also thought, perhaps sh outputs this error if an executable is incompatible in some way. But on my host PC I see another error in this case, e.g.:
$ sh /tftpboot/hw
/tftpboot/hw: 1: /tftpboot/hw: Syntax error: word unexpected (expecting ")")

sh prints this weird error because it is trying to run your program as a shell script!
Your error ./hw: not found is probably caused by the dynamic linker (AKA ELF interpreter) not being found. Try compiling it as a static program with -static or running it with your dynamic loader: # /lib/ld-linux.so.2 ./hw or something like that.
If the problem is that the dynamic loader is named differently in your tool-chain and in your runtime environment you can fix it:
In the runtime environment: with a symbolic link.
In the tool-chain: use -Wl,--dynamic-linker=/lib/ld-linux.so.2

Related

How do I compile my C++ code with anaconda boost?

I have to use Anaconda provided compilers and their Boost package to compile my C++ code. I have a problem such that I do not know how to link properly during the compilation command.
To begin with: I create a dedicated anaconda env. with the following packages:
- boost-cpp=1.77.0
- compilers=1.3.0
These packages may be found here:
https://anaconda.org/conda-forge/compilers
https://anaconda.org/conda-forge/boost-cpp
Upon env. activation: double check which compiler I use:
$ which g++
/usr/share/miniconda3/envs/MYNAME/bin/g++
And then I would like to compile, but I don't know how to include boost libraries.
When I run:
$ g++ workflow/src/CODE.cpp -o workflow/bin/CODE -lm
Of course I get:
CODE.h:10:10: fatal error: boost/property_tree/info_parser.hpp: No such file or directory
10 | #include <boost/property_tree/info_parser.hpp>
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
Nevermind, I have worked it out.
Although it might be useful for others so let's keep the solution in here:
g++ workflow/src/CODE.cpp -o workflow/bin/CODE -I$HOME/miniconda3/envs/MYNAME/include -lm

Compile C++ on Windows

I'm trying to compile C++ on Windows.
The command needed to compile on Linux is:
g++ -O3 -Wall -shared -std=c++11 -fPIC `python -m pybind11 --includes` EO_functions_bipartite.cpp -o extremal_bi.so
I installed MinGW but when I try to compile I get the following error:
g++.exe: error: python: No such file or directory
g++.exe: error: pybind11: No such file or directory
g++.exe: error: unrecognized command line option '-m'
g++.exe: error: unrecognized command line option '--includes EO_functions_bipartite.cpp'
g++.exe: fatal error: no input files
compilation terminated.
Assuming you have python in your path.
The backtick escape thing that embeds the python -m pybind11 --includes command within the g++ doesn't work on cmd.exe in Windows.
Run the python -m pybind11 --includes command on its own line in the cmd shell. Take the output of that command and substitute in into the g++ command. It should be a bunch of -I include params.
So if the output of the python command is this:
-IC:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.7_3.7.2544.0_x64__qbz5n2kfra8p0\Include -IC:\Users\User\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\pybind11\include
Expand your g++ command to be this:
g++ -O3 -Wall -shared -std=c++11 -fPIC "-IC:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.7_3.7.2544.0_x64__qbz5n2kfra8p0\Include" -IC:\Users\User\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\pybind11\include EO_functions_bipartite.cpp -o extremal_bi.so
Notice the quotes I put around the first include directory because it has a space in its path.
The easiest way to start on native Windows if you have a Linux background is to install MSYS2 shell with MinGW-w64. This will provide an actual bash that allows you to run commands almost exactly the same way as on Linux, including support for backticks like in the case of your issue.
Though I would always recommend using $( ... ) instead of backticks, as this allows nesting.
Note that MinGW-w64 also exists on Windows to allow cross-building for Windows from Linux, but that may be a bit more difficult if you have never done any cross-building before.
Also -shared ... -o extremal_bi.so in your command should be replaced with -shared ... -o extremal_bi.dll -Wl,--out-implib,libextremal_bi.dll.a as .so files don't exist on Windows as Windows uses .dll files for shared libraries and the compiler uses .dll.a files as library objects for them.
Finally on Windows you need to tell the compiler or linker which symbols you will be exporting by either writing a libextremal_bi.def starting with the line EXPORTS followed all the symbols you want to be exported and include -def libextremal_bi.def in the link command, or using __declspec(dllexport)/__declspec(dllimport) when defining those symbols, which may be a bit complexer as it requires some conditional defines to determine if the code is being compiled for Windows and if it's during the actual build process of the shared library (__declspec(dllexport)) or code that uses it (__declspec(dllimport)). There is also another method to export all symbols, but that's a dirty method that may more easily cause symbol conflicts.

"make[2]: g++: Command not found" in Netbeans

I get the error message:
make[2]: g++: Command not found
which I know means it cannot find the C++ compiler. However, in Netbeans if I go to the configuration to choose my C++ compiler it shows the following being in /usr/bin:
g++4.6
g++4.7
g++4.8
but it doesn't have just g++.
Tried sudo aptitude install g++ but it didnt work.
If I do g++ --version I get:
The program 'g++' can be found in the following packages:
* g++
* pentium-builder
1) Verify you can run "g++" from the command line,
2) type whereis g++ to get the path,
3) Make sure you have the NetBeans C++ plugin installed
Go to Tools->Options->C++->Build Tools
4) Configure the path in NetBeans
Usually, /usr/bin/g++ is a symlink to some /usr/bin/g++-4.7 (or g++-4.8 etc...); just make it again (which is usually provided by the g++ virtual package on Debian or Ubuntu), e.g.
% sudo -s
# cd /usr/bin
# ln -sv g++-4.8 g++
Above % and # are shell prompts that you should not type.
Of course, don't forget the sudo apt-get install g++ etc...
BTW, you might put that link from $HOME/bin/g++ to /usr/bin/g++-4.8 and you don't need root permission for that.
(sometimes, these symlinks go indirectly thru /etc/alternatives etc...)
Your issue is a sysadmin issue; you should rather ask it (with a big lot more details) on askubuntu or superuser; it is off-topic on Stack Overflow.
BTW, NetBeans is not a compiler, but an editor (sometimes called IDE). You could use something better/simpler (e.g. emacs or vim) with e.g. make as a builder (to be run inside a terminal). Before using NetBeans ensure that g++ -v is working alone in some terminal

providing the path to a library for ifort

Disclaimer: This is not my field and I don't know the Jargon.
I'm trying to compile and run some code on a computation server. The machine have intel compiler installed on. When I try to compile the code using
ifort src.f -o mem
Everything works. If I try to optimize things:
ifort -fast src.f. -o mem
I first get messages:
ipo: remark #11001: performing single-file optimizations
ipo: remark #11006: generating object file /tmp/ipo_ifortYepD4m.o
Which seem logical. When I run the out file I get an error:
./mem: error while loading shared libraries: libgfortran.so.1: cannot open shared object file: No such file or directory
I searched for libgfortran:
avityo#admin:~/prog/mn270.161> locate libgfortran
/home/MATLAB/R2011b/sys/os/glnxa64/libgfortran.so.3
/home/MATLAB/R2011b/sys/os/glnxa64/libgfortran.so.3.0.0
/opt/matlab/r2012b/sys/os/glnxa64/libgfortran.so.3
/opt/matlab/r2012b/sys/os/glnxa64/libgfortran.so.3.0.0
/usr/lib64/gcc/x86_64-suse-linux/4.3/libgfortran.a
/usr/lib64/gcc/x86_64-suse-linux/4.3/libgfortran.so
/usr/lib64/gcc/x86_64-suse-linux/4.3/libgfortranbegin.a
/usr/lib64/libgfortran.so.3
/usr/lib64/libgfortran.so.3.0.0
Is there a way to tell ifort the available libgfort library?
I agree with Vladimir that it is a strange dependency between gfortran & ifort. However, it appears that ifort is looking for libgfortran.so.1 and you have libgfortran.so.3 listed there. You should be able link the former to the latter via ln -s [target] [shortcut]. That is,
ln -s /usr/lib64/libgfortran.so.3 /usr/lib64/libgfortran.so.1

Dealing with "C compiler cannot create executables" in Cygwin

Whatever I try to compile in Cygwin I get the following output:
checking for mingw32 environment... no
checking for EMX OS/2 environment... no
checking how to run the C preprocessor... gcc -E
checking for gcc... gcc
checking whether the C compiler (gcc ) works... no
configure: error: installation or configuration problem: C compiler cannot creat
e executables.
The last few lines of the logfile look like this:
configure:2810: checking for EMX OS/2 environment
configure:2822: gcc -c conftest.c 1>&5
configure: In function `main':
configure:2818: error: `__EMX__' undeclared (first use in this function)
configure:2818: error: (Each undeclared identifier is reported only once
configure:2818: error: for each function it appears in.)
configure: failed program was:
#line 2815 "configure"
#include "confdefs.h"
int main() {
return __EMX__;
; return 0; }
configure:2838: checking how to run the C preprocessor
configure:2859: gcc -E conftest.c >/dev/null 2>conftest.out
configure:2943: checking for gcc
configure:3056: checking whether the C compiler (gcc ) works
configure:3072: gcc -o conftest conftest.c -llib 1>&5
/usr/lib/gcc/i686-pc-cygwin/3.4.4/../../../../i686-pc-cygwin/bin/ld: cannot find
-llib
collect2: ld returned 1 exit status
configure: failed program was:
#line 3067 "configure"
#include "confdefs.h"
main(){return(0);}
This is a fresh Cygwin install with G++ and a bunch of other devtools added. Any idea what I need to do to get this thing working?
Update 0: Nick, your link to http://www.geektimes.com/linux/troubleshooting/c-cant-create-executables.html was tried already - unfortunately this instructions are for redhat and do not seem to apply to cygwin.
Your Configure is wrong.
Usually autoreconf -f helps. If not you need to check the failing rule and fix it.
The '-llib' seems a bit unusual to me, but I'm far from an expert. Just out of curiosity is autoconf installed? I had some problems similar to this until I installed autoconf. It seems like the configure script is incorrectly generating a '-llib' value but it's hard to say why based on just the snippets posted.
When I've had this problem, it has been a link error caused by Cygwin looking for "library.o" instead of "library.obj" or "library.so" instead of "library.dll".