Get the native compiler of a Linux machine - linux

I am willing to do the following:
gcc -dumpmachine
Because I want to get the architecture of the machine in a GNU like so I can give it directly to the option --host of a configure script. The case is that I can execute "gcc" hoping that will be installed on the machine? Imagine that they use icc or another compiler, what would you do, there is a way to execute the native compiler? Is also possible that the gcc is set to an alternative which is not the native of the machine so I execute that command and I get a wrong architecture. By the moment I am executing gcc but I know it can fail sometimes.
Thank you.

Related

How to safely reconfigure gcc on my Linux system?

I am trying to install a program on CentOS 6.10 and at the end of the installation, it gave an error saying that Glibc-2.14 is necessary. I upgraded the current Glibc and this time the error below occurred:
* These critical programs are missing or too old: gcc
* Check the INSTALL file for required versions.
I upgraded the gcc and tried to configure again. However, the same error persists. Hence I read the INSTALL file as it suggests, and I see this section:
You may also need to reconfigure GCC to work with the new library. The
easiest way to do that is to figure out the compiler switches to make
it work again (`-Wl,--dynamic-linker=/lib/ld-linux.so.2' should work
on GNU/Linux systems) and use them to recompile gcc.
So should I go to where gcc is built and do:
$ ./configure -Wl,--dynamic-linker=/lib/ld-linux.so.2 ?? Do I understand the instructions correctly? If so, then how will I be able to configure only gcc and not the other executable files as they are all in the same folder? (e.g gcc-5, git, idle, python, python-build.. etc) The directory is something like: home/linuxbrew/.linuxbrew/bin/gcc
I'm asking this because GNU compiler and GCC are fundamental in Linux system, and I'm not sure if those are the correct steps.
You are probably using a really old compiler (the one that comes with CentOS by default).
You need to install Red Hat Developer Toolset which provides up-to-date versions, see https://www.softwarecollections.org/en/scls/rhscl/devtoolset-7/ for more details:
Developer Toolset is designed for developers working on CentOS or Red Hat Enterprise Linux platform. It provides current versions of the GNU Compiler Collection, GNU Debugger, and other development, debugging, and performance monitoring tools.

how to build a linux-based program for window?

I want to build a inux-based program fully standalone for windows.
And I try to use mingw64 on ubuntu with the command
./configure --host=x86_64-w64-mingw32 --disable-shared --enable-static LDFLAGS="-L /usr/local/lib/"
make
But it failed with
src/libcork/posix/subprocess.c:14:24: fatal error: sys/select.h: No such file or directory
#include <sys/select.h>
^
compilation terminated.
Here is the source code in github:
https://github.com/shadowsocks/simple-obfs
So how can I build the program fully standalone for windows?
May I try the cygwin or msys2?
update:
I find another source code which has been ported.
But when I compiled it with
./configure --host=x86_64-w64-mingw32 CFLAGS="-static" LDFLAGS="-static"
I get an executable file which still needs the libssp-0.dll.
So how can I make the libssp be static linked?
Here is the new source code: https://github.com/Windendless/simple-obfs
I don't think that the functionality in select.h is available on the MinGW version of gcc, because it is not available in the Windows platform. If you're building something that was intended to be built on Windows, then you might find that there are options you can pass to configure that enable a different, Windows-friendly way of achieving the same thing. If you're building something that was only really intended to be used on Linux, then you might be out of luck. In that case, your choices really amount to:
Modify the code so it doesn't require select.h. Some of that functionality might already exist in the Windows Sockets API, with a somewhat different implementation. However, it might equally well not, in which case you'll have to do some work.
Use a Windows compatibility layer that is more feature-rich than MinGW. Cygwin might well work, as might the Windows Subsystem for Linux in Windows 10. However, none of these approaches amounts to building a "standalone" application, because they require some supporting infrastructure.

Cross-compilation of cgo (for darwin) fails

I am fairly new to go and even Linux in general.
I have built an app in a Linux environment which makes use of a gtk lib based on cgo (https://github.com/mattn/go-gtk/). The application builds fine in its native environment (linux 64bit) but when I try to compile for darwin 64bit I get the following result:
# net
could not determine kind of name for C.AI_MASK
# net
could not determine kind of name for C.AI_MASK
The command line I use to build:
env GOOS=$1 GOARCH=$2 CGO_ENABLED=1 go build $3
Where $1 is darwin and $2 amd64 (and $3 the path to my app).
As the error seems to come from the lib I import, I am not sure what to do to fix it. I have also read that cross compiling cgo does not really work as it relies on native macos stuff so it would need to be built on a mac. Is this true or is there something I can do to make it work in my environment?
I am also slightly confused as it seems most people discussing this subject are talking of go pre 1.5 which was entirely different when it comes to cross-compiling if I understand correctly.
Thanks
I am now able to compile successfully my code on linux for darwin thanks to the comments by JimB.
What I needed was a osx toolchain such as github.com/tpoechtrager/osxcross.
Then I compiled my code by doing env OSXCROSS_NO_INCLUDE_PATH_WARNINGS=1 MACOSX_DEPLOYMENT_TARGET=10.6 CC=o64-clang CXX=o64-clang++ GOOS=darwin GOARCH=amd64 CGO_ENABLED=1 go build -v mywork/myprogram.
Some of my programs compile successfully, some throw errors at linking time but I guess that's another issue so I'll mark this question as solved as far as the cross-compilation goes.

Cross-platform build under Windows targeting Linux using CMake

I am developing a software in C++ on windows 32-bit (using MSVC++), but since I want to be able to use my software on every platform, I have decided to use CMake as my build generator.
Therefore, I am still just a beginner in CMake. From the CMake tutorials, I understand that in order to cross compile codes, first a toolchain simulating the target platform should be installed on the host platform. Then using the appropriate target-platform C and C++ compilers provided by this toolchain, CMake would be able to generate makefiles etc.
Now, I want to build my code for Linux platform(GNU/Linux) on a Win32 platform. I tried doing the above procedure using CMake combined with Cygwin and using gcc and g++ as compilers. It built fine, created makefiles, and when I issued "make" in Cygwin terminal, the generated makefiles were "made". Now I have got an executable which I was hoping would run on Linux platform. But on Linux I get the error: bash cannot execute binary file.
Using command file executablename, I realized the executable which is made by the above procedure is of type PE32 which is only for Windows.
Now my question is: Is my understanding of cross-platform build procedure using cmake correct?Or should I just use another Linux toolchain under windows to get a Linux ELF executable? What toolchains come to your mind which would give me what I want?
Many thanks
Setareh
You will want to look here: cmake-toolchains(7) if you do cross compiling. However, I would suggest that you install a Linux VM like virtual box on your windows machine and build naively on Linux. It will compile much faster and you will not have to worry about cross compiling. You can mount the windows disk from the linux VM so you can share the same source tree. The linux VM will compile much faster than gcc running under windows.
Your understanding of CMake is correct... it will determine how to create the build system you request (or is default for the platform you are currently on) based on rules in your CMakeLists.txt file. However, this won't necessarily help you compile for linux on a windows machine if you don't have something installed that can target linux.
To compile targeting linux, you will need to use a linux compiler. The link posted by #stjin tells you how to install one on cygwin. Then, to set up your CMake build, do this in the terminal:
CC=gcc-linux CXX=g++-linux cmake . [options]
This will tell CMake to locate the special linux targeted compilers. Hopefuly, after compiling with these compilers you will be able to run on linux.

running binary on different machine causes segfault

I'm not well versed in how linking happens in c++
I have a binary that i compiled on one machine and i'd like to copy it and run it on another machine.
I would expect this to work, because the libraries are the same on both machines (i think!) and the version of linux is the same (same kernel, etc.) However, when i copy it over... it appears to segfault in one of the libraries i am dynamically linking when i run it.
It runs like butter on the machine that i compiled it on. But on the machine that i scp'd it over to, when i run the binary, it instantly segfaults on a std::string::compare in a call stack with some functions in one of the libraries i am dynamically linking.
i tried installing the libraries again on both machines and doing ldconfig, but same results.
any ideas on how to debug these kind of weird segfaults caused by dynamic linking issues?
Well, it might help narrow down the problem if you run the program in a debugger. When compiling, add the -g -ggdb arguments to the g++ command, then when running the program, use the command gdb ./executable (you may need to install gdb first.) At the gdb prompt, type run and your program will run until it segfaults. Then you can try to figure out what went wrong.
There are plenty of tutorials for using gdb (the GNU debugger) online.
Sounds like a binary compatibility issue. This SO link might shed some light:
Creating a generic binary in linux for all x86 machines

Resources