gcc - suffix suppression during cross-compilation Windows PE on Ubuntu - c++

System info:
Distributor ID: Ubuntu
Description: Ubuntu 20.04.4 LTS
Release: 20.04
Codename: focal
Using x86_64-w64-mingw32-gcc:
x86_64-w64-mingw32-gcc --version
x86_64-w64-mingw32-gcc (GCC) 9.3-win32 20200320
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
For a project we are using mingw to cross-compile Windows PE files on Ubuntu. During the cross-compilation we are making modifications to the files. During cross-compilation, the following code is used in the Python script:
x86_64-w64-mingw32-gcc {path} -o {outfile} -static
When the process starts we have a file, somefile (no extension is used), but after going through the cross-compilation the newly compiled file is called somefile.exe.
On an older Ubuntu system, I believe it was 17.##, my apologies but the box is offline and I don't remember the exact versioning, we could run the script and the filename would be preserved, no .exe appended to the filename.
The addition of .exe to the filename is causing some issues with my workflow, and I'm hoping there's a way around this outside of cloning the old Ubuntu system to newer hardware. This may work, but logistically it's not ideal.
We're able to make some modifications by bash during the process, but this is creating unwanted artifacts. And we'd like to keep our system processes streamlined.
An older post, gcc - how to force to not add .exe suffix, suggests adding a . to the end of -o {outfile}, removing the suffix leaving a .. While a great idea, this still has issues with our workflow in that the filename, a SHA256 hash, is used and no extension is looked for (the . still causes issues).
If anyone has any insight on how this may be remedied, perhaps modifying the main.c file that is referenced during compilation, it's greatly appreciated. I can provide more information if necessary.
Also, my apologies if the post is missing any mission-critical information, I'm new to posting here.

Related

Compile C++ Windows exe standalone files with MSYS2

I've recently started using Msys2 to install gcc compiler to make some exe for Windows. It works very well, but there's a problem when passing my exe to my brother. His laptop has not msys2 installed and when he tries to run my exe some errors occur. Seems like few dll files are necessary to use my exe (like msys-2.0.dll).
I've found out that those files are used by msys2 to "fake" the OS on the machine pretending it's a POSIX one. Is there a way to compile standalone exe for windows with msys2? I would like my brother to be able to use my exe without installing msys or else.
Here are all the details to understand better my situation:
g++ HelloWord.cpp -o Helloword is the line I use to compile
C:\msys64\mingw64\bin here's the path where g++ is stored
All the exact error messages I receive from windows after double clicking on the exe file that has been generated. Note that these messages do not appear on the CMD, but in a classic Windows error pop-up:
The program can't start because msys-2.0.dll is missing from your computer.
Try reinstalling the program to fix this problem.
The program can't start because libstdc++-6.dll is missing from your computer.
Try reinstalling the program to fix this problem.
The program can't start because libgcc_s_seh-1.dll is missing from your computer.
Try reinstalling the program to fix this problem.
Fixed:
I've resolved the issue just using the g++ parameter -static. Is it an overkill?
My version of MinGW is a bit old ...
C:\example>where g++
C:\misc\mingw810_64\bin\g++.exe
C:\example>g++ --version
g++ (x86_64-posix-seh-rev0, Built by MinGW-W64 project) 8.1.0
Copyright (C) 2018 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
But same idea:
C:\example>cat > compile_me.cpp
#include <iostream>
int main () { std::cout << "hi" << std::endl; }
^Z
C:\example>g++ compile_me.cpp -o compiled.exe
C:\example>compiled.exe
hi
C:\example>dumpbin /dependents compiled.exe
...
Image has the following dependencies:
KERNEL32.dll
msvcrt.dll
libstdc++-6.dll
...
In that case (dynamically linked stdlib) you'd deploy libstdc++6.dll with the executable, installing it to the same path as the exe (the other two are generally present in the windows system path).
If you want to drop that dependency, use -static:
C:\example>g++ compile_me.cpp -o compiled.exe -static
C:\example>compiled.exe
hi
C:\example>dumpbin /dependents compiled.exe
...
Image has the following dependencies:
KERNEL32.dll
msvcrt.dll
...
Deploying that .exe alone should be fine.
The file size will be larger but that's not a huge deal these days. Also your MinGW / MSYS install might come with strip:
C:\example>dir compiled.exe
Volume in drive C is Windows
Volume Serial Number is D2BA-C6F0
Directory of C:\example
09/24/2022 06:49 PM 2,389,120 compiled.exe
1 File(s) 2,389,120 bytes
0 Dir(s) 135,945,314,304 bytes free
C:\example>strip compiled.exe
C:\example>dir compiled.exe
Volume in drive C is Windows
Volume Serial Number is D2BA-C6F0
Directory of C:\example
09/24/2022 07:03 PM 838,656 compiled.exe
1 File(s) 838,656 bytes
0 Dir(s) 135,944,765,440 bytes free
C:\example>compiled.exe
hi
If there are other dynamic libraries that your particular executable ends up depending on, and the vendor has chosen not to provide statically linked alternatives, then you'll have to just deploy them with the exe. It's generally easy enough to just throw everything in a zip file or use your favorite scriptable installer.
(Note: dumpbin ships with Visual Studio; and can be found in some appropriate subdirectory in VC\Tools in the vs install path).
MSYS2 compiles native PE32 executables. It does not rely on any magic msys environment or static linking.
Get yourself a dependency walker and look to see what DLLs your app needs to run. Anything not in a Windows subdirectory should be where you focus your attention. Also make sure your app does not require any special Microsoft redistributable dependencies.
Ultimately, you should be creating an installer for your application to handle dependencies. I personally like Inno setup, but plenty of others exist that are well liked also.
The OP has solved their problem but for anyone else who finds this:
Make sure you launch MSYS2 by clicking on mingw64.exe or the equivalent shortcut.
Run which g++ to make sure you are using /mingw64/bin/g++. If it shows some other g++ then run pacman -S mingw-w64-x86_64-toolchain to install the toolchain.
Compile your code with g++ and use the -static option.
Run ntldd yourprogram.exe to check which DLLs your program is using and make sure they are part of Windows, not part of MSYS2.

How do I set path to respective bin of the mingw directory

I'm trying to use C++ in VSCode, and I found a tutorial where I install g++ and clang to make it work.
I installed g++ fine, and added it to my list of environment variables, but then the instructions for installing clang say to "set path to respective bin of the mingw directory" without showing me how.
What does this mean and how do I do that?
Also, #include <iostream> looks like it doesn't work either, it says the file is not found, so I'm wondering if it's because clang isn't installed or something else I need to fix. Thanks!
Ok, I've skimmed that tutorial video and YouTube comment section. Basically I think it's a poor tutorial, as it doesn't explain the basics, and that's why you're getting tripped up. My first recommendation is to save yourself some trouble and follow the VSCode Getting Started with C++ Tutorial instead.
Not only is the official tutorial easier to understand, it will guide you toward using the Microsoft C++ extension that almost everyone uses (and can help you with), rather than the comparatively obscure Clang-based C++ extension.
But that's not answer to your actual question. You asked:
... the instructions for installing clang say to "set path to respective bin of the mingw directory" without showing me how.
What does this mean and how do I do that?
I'm not sure! It's sort of nonsensical. But I think what is meant is:
Install mingw GCC and put its bin directory on the PATH.
Install LLVM+Clang and put its bin directory on the PATH.
Start VSCode from a shell where both are on the PATH.
Then proceed with the linked tutorial.
You say you already have mingw GCC on your path, but let's check that. At the command prompt (I assume you are using the default Windows cmd.exe shell), run:
> gcc --version
gcc (x86_64-posix-seh-rev0, Built by MinGW-W64 project) 5.4.0
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
If you don't see output like that, then something is wrong. Make sure the bin directory of mingw GCC, which contains gcc.exe, is on your PATH.
Next, Clang. Clang is part of LLVM. Wherever you installed LLVM, there should be a bin directory inside it containing clang.exe. Add that to your PATH. In my case, I installed LLVM into d:\opt\llvm-8.0.1, so I would run:
> set PATH=%PATH%;d:\opt\llvm-8.0.1\bin
Then check that it is working:
> clang --version
clang version 8.0.1 (tags/RELEASE_801/final)
Target: x86_64-pc-windows-msvc
Thread model: posix
InstalledDir: D:\opt\llvm-8.0.1\bin
Once both gcc --version and clang --version respond similarly to what I have shown, you're ready to start VSCode:
> code
and from there, the tutorial's instructions should work.
I faced a similar issue a while back. Go to the installation directory of mingw/bin. copy this path and add it to the environment variable PATH to your Windows system, save it. Relaunch vscode and then try pressing ctrl + ` and execute code using g++. This way gdb will also work.

Downgrade gcc/g++ in Ubuntu?

I am trying to downgrade gcc/g++ to 4.8.1. I tried two options, both with problems.
use sudo apt install gcc-4.8 g++-4.8. This option will install 4.8.5 etc. I wonder if there is any way to specify 4.8.1. sudo apt install gcc-4.8.1 g++-4.8.1 does not work as it will complain that "unable to locate package gcc-4.8.1".
Download "gcc-4.8.1" and follow the official steps ("configure, make, make install") but it fails at the make step with errors "CXXABI_1.3.8" not found.
I found another solution but I am not sure whether it is desirable (" install gcc-4.8.1 from source code on Ubuntu-16.04").
How do I downgrade gcc/g++ to 4.8.1 in Ubuntu?
Many thanks.
How do I downgrade gcc/g++ to 4.8.1 in Ubuntu?
I think you want look into the command "update-alternatives" (instead of 'downgrade').
To learn some more browse "https://askubuntu.com/questions/529687/how-to-use-update-alternatives-to-manage-multiple-installed-version-of-the-sam"
The update-alternatives works by changing what the command g++ points at. Currently on my system, g++ points to g++-6.
I have sometimes experienced an install that simply does not work. My recent g++ v6.2 install is broken, I don't know why.
But because my ubuntu is out of date, I plan to upgrade to the latest ubuntu. I might as well wait to install the latest compiler.
In the mean time I have 6.2.0, which does not 'work' (cause unknown). g++ points to this:
~$ g++ --version
g++ (GCC) 6.2.0
Copyright (C) 2016 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Since I seldom remove (un-install) the previous version in use, it turns out that with a simple search to identify what is available, I can access an older compiler using a suffix. For example
~$ g++-5 --version
g++-5 (Ubuntu 5.2.1-23ubuntu1~15.10) 5.2.1 20151028
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
So, I continue more or less the same as before the failed 6.2.0 install.
For you, I think this means that you can use your package manager to install your desired earlier compiler, but remember to search for it in case it already exists on your system. Then use update-alternatives to make it the default, or just learn the new command name to invoke, i.e. g++-5, and continue developing with both commands easily available.

Using GCC Through Git Bash

I have Git for Windows 2.10.2 and the latest version of MSYS2 installed on my Windows 7 machine. I had been using an older version of MinGW to compile c++ code. I would use notepad++ to write/edit the files and compile and run them through Git Bash using gcc console commands. I love being able to go to my file directory, right click, and select the "Git Bash Here" option to open the console and do whatever I needed.
After uninstalling MinGW, I installed MSYS2 and downloaded some of their packages using the package manager, Pacman, that comes with MSYS2. I also updated my PATH variable. I went to test everything out with Git Bash by typing the command:
gcc --version
and I've been getting this error
OWNER#Seth MINGW64 /c
$ gcc --version
2 [main] gcc (5284) C:\msys64\usr\bin\gcc.exe: *** fatal error - cygheap base mismatch detected - 0x1802FF408/0x1802FE408.
This problem is probably due to using incompatible versions of the cygwin DLL.
Search for cygwin1.dll using the Windows Start->Find/Search facility
and delete all but the most recent version. The most recent version *should*
reside in x:\cygwin\bin, where 'x' is the drive on which you have
installed the cygwin distribution. Rebooting is also suggested if you
are unable to find another cygwin DLL.
Segmentation fault
OWNER#Seth MINGW64 /c
$
I've never used Cygwin and therefore have never installed it on this computer. I've spent the last three days searching online for solutions to this. I've uninstalled and reinstalled Git and MSYS2. I put the git-bash.exe in my c:\msys64\ directory and ran the same command from there and got:
OWNER#Seth MINGW64 ~
$ gcc --version
gcc.exe (Rev2, Built by MSYS2 project) 6.2.0
Copyright (C) 2016 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
OWNER#Seth MINGW64 ~
$
but that doesn't seem to me to be a very elegant solution. It does work though, I can cd to the folder containing my code and compile and run them.
Another thing I tried was to go into the registry and change where the "Git Bash Here" option looks for git-bash.exe. I changed it from:
"C:\program files\Git\git-bash.exe" "--cd=%v."
to
"C:\msys64\git-bash.exe" "--cd=%v."
and now right clicking and selecting "Git Bash Here" and running the command:
gcc --version
results in:
OWNER#Seth MINGW64 ~
$ gcc --version
gcc.exe (Rev2, Built by MSYS2 project) 6.2.0
Copyright (C) 2016 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
OWNER#Seth MINGW64 ~
$
and once again I can cd to the folder with my code and compile and run them.
The problem now is that before when Git Bash would open, it would be looking in the directory where I had right clicked. But now I have to cd to where ever I'm needing to go.
Is there a better way to get MSYS2 and Git working together? Am I making this way more difficult than it should be? Or is there a better way and I'm just stumbling around in the dark?

Get "Access is denied" when trying to compile with g++ from command line. Cygwin

I have installed all packages in cygwin. I have also added C:\cygwin\bin to my PATH variable. But when I try to compile a c++ file in command line I get the error 'Access is denied'. The same commands work in the cygwin batch window. Does anyone know what's wrong?
Edit: I changed the permissions for gcc and g++. I no longer get the 'Access Denied' error, but get a new one: "This version of C:\cygwin\bin\g++.exe is not compatible with the version of Windows you're running. Check your computer's system information to see whether you need a x86 (32-bit) or x64 (64-bit) version of the program, and then contact the software publisher.".
Because c:\cygwin\bin\gcc.exe isn't an executable file, it's a cygwin symbolic link.
$ file /bin/gcc
/bin/gcc: symbolic link to `/etc/alternatives/gcc'
$ file /etc/alternatives/gcc
/etc/alternatives/gcc: symbolic link to `/usr/bin/gcc-4.exe'
The underlying file runs just fine.
C:\cygwin\home\Ben>gcc-4 --version
gcc-4 (GCC) 4.5.0
Copyright (C) 2010 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
I just deleted gcc and g++ and renamed the g++/cc-4.
Unless you're a masochist, always use a Cygwin batch window with Cygwin executables. Also if you're using the Cygwin compiled gcc, you'll need the Cygwin DLL to run the results. The Cygwin website explains why. If you need executables without the Cygwin dll, I'd explore MinGW.
I got the same issue and was because I had pending the reboot after Cygwin installation. After the reboot works for me.