Cross Compiling with Cmake. What should I expect from cross compiling - c++

I also want the code I developed for Linux to work on Windows. I wrote simple Hello world code for this. I was able to create the .exe file with minGW using Cmake. When I run the .exe file directly on Windows, I got the error "libstdc++-6.dll not found" even though it exists on my computer. When I deleted the contents of the /build folder in Windows and built it with Cmake, the newly created .exe file worked. When I compile directly with minGW without using Cmake, the .exe worked without problems.
Is it a mistake to transfer the .exe file with its source codes to the Windows environment and expect it to work directly? I want to understand the logic of this concept properly.

If you use libraries that are not part of Windows itself, you will generally need to distribute the DLLs for those libraries in the same directory as the EXE in order for your EXE to work on another computer.
Alternatively, many cross-compilers support a -static argument that you can pass at link time in order to statically link these libraries into your executable. I favor that approach, but the licensing requirements for it are stricter.
(Make sure you follow the license terms for any software you redistribute that isn't your own, open source or otherwise.)

Related

Missing libgcc_s_seh-1.dll starting the .exe on Windows

Intro
I have a CMake-based C++ project. Until now I build and ran the project via CLion. Everything worked fine until I tried to run the .exe-file directly (not via CLion).
Problem
When I navigate to the cmake build directory in order to start my program via the executable file, it fails with the following message in the popup: Cannot continue the code execution because libgcc_s so-1.dll was not found. Reinstalling the program may resolve the issue.
I have the following questions
If I interpret the error message correctly, then this dll is missing on my computer. So I ask myself, why does my program still work when I start it via the development environment (CLion), although the error message expressly states that the source code requires this dll?
Is it the fault of my application/source code that the error appears or rather the current state of my computer? If the former, how can I prevent this error from appearing for other users?
What is the best way to fix this error? It's obvious that I need to download this dll, but where is the best place to put it (which directory and environment variable to use on Window)?
Which source is trustworthy to download this dll? I don't want to download any malware under this dll-name.
Optional: What kind of library is that? What functionalities does it offer?
Additional information
I use CMake as my build tool, CLion as the IDE and MinGW as the compiler.
What I have did so far?
I made sure it still works through the IDE.
I found this dll does not exist in the MinGW installation folder.
I searched the web for more information. Unfortunately, there are only pages unknown to me that only offer the download of this dll. That doesn't satisfy me.
I found the cause of my problem: I had two MingGW installations on my machine. Once the installation that comes with CLion and a separate one. The latter did not have the required dll. However, CLion used its own installation, which in turn owns the DLL. So the solution was to remove the separate installation and include the path to the CLion installation's bin/ directory in the PATH environment variable.
This file is part of MinGW-w64 when using SEH as exception model (as opposed to Dwarf or SJLJ). You need to distribute the .dll files your .exe file(s) depend on in the same folder as the .exe file(s).
If you don't have that file, then you probably have been using libraries compiled with different versions of GCC/MinGW(-w64). I recommend building everything with the same compiler to ensure stable binaries.
Tools like Dependency Walker can help you figure out which .dll files your .exe file depends on.
Or use the command line tool copypedeps -r from https://github.com/brechtsanders/pedeps to copy the .exe files along with it's dependencies.

How to deploy Qt app compiled using MSCV so that it won't neet vc_redist installed?

Is there a way to deploy a Qt desktop application that is compiled using MSVC in such a way that it will be "portable" (just run exe from a folder, not install anything, not even install VC_redist)?
Of course, it is possible to use it if Microsoft Visual C++ Redistributable is installed on target computer, but is it possible to make it run without installing it (eg. by putting some dll from vcredist to application's folder)?
Just as you commented, you can use windeployqt to add Qt-related DLLs and resources. As for other required DLLs, you could use Dependencies to find them and MANUALLY copy them into your application folder, including MSVC DLLs.
PS: I know manually copy those DLLs is low efficient and fallible. This is why I ask Is there any way to search and copy all the DLL dependencies?, but I haven't found a tool smart enough to do this chore automatically. You might try those tools mentioned in the comments, like NDepend, though.
If you build/link your application statically, you will only have 1 .exe without DLL. The second way is to build your app with shared libraries and at runtime your app will search those DLL in PATH and in the current directory so you just need to put all your needed DLL in this folder. How do you manage your Qt library ?

Building a Qt executable in Visual Studio 2017

I made a Qt Application in Visual Studio and it works when I run it in the program. However then I build the solution and try to run the executable errors pop up that certain .dll files are not found(QtWidgets.dll, QtCore.dll,QtCored.dll etc.). How can I fix this?
To make your application ready for deployment, you can use windeployqt.
It is a commandline program that comes with Qt and collects all the required dependencies of your executable. Go to your QTDIR/bin/ folder and run this command
windeployqt <path-to-app-binary.exe>
It will scan your binary and copy everything that is needed next to it. For further details, have a look at the documentation.
Well, you have two options:
If you want to distribute your application, you have to copy the required DLLs to the folder where your executable is. You can do this either by copying them manually or you write a script for this. The DLLs are in the binary folder of your Qt installation, e.g.
Qt\5.12.2\mingw73_64\bin
Add the above mentioned folder to your system PATH variable, then the DLLs should be found by your application.

Compile C++ with netbeans

I'm currently making a small C++ program for my friends. I'm using Netbeans in school for Java and wanted to use Netbeans for C++ too (I want to use the same IDE for everything).
Now when I run my program in netbeans, it works perfectly in the IDE. But when I try to compile my program I can't seem to find the .exe file. I can only find the debug .exe file, which I can't run because I miss 3 .dll files (cygstdc++-6.dll, cygwin1.dll and cyggcc_s-seh-1.dll)
Am I doing something wrong? I can't seem to find the problem at this moment
thx for the help
If you want to have release .exe - you need to compile in Release mode instead of Debug
Regarding the issue with missing files:
My assumption is that Netbeans has a built-in compiler (which from your explanation appears to be one within cygwin environment). Having it built-in simply means that the IDE knows a path to all the sources (.dll) and header files
When you try to run the .exe out of the IDE - your executable cannot find these libraries (cygstdc++-6.dll, cygwin1.dll and cyggcc_s-seh-1.dll) because they're not set in the environment variables and are not visible from the executable's folder. (I expect, that the path to these .dlls is not hardcoded)

Packaging libraries with .exe

I've made an application using both the OpenCV and WxWidgets library. The code runs fine on my development machine.
However, when I transfer it to another windows machine I get the error
*"The program can't start because cv110.dll is missing from your computer.
Try reinstalling the program to fix this problem".*
Now, I'm guessing installing the same versions of OpenCV and WxWidgets as used in development would resolve this problem but this is a bit much to ask of a casual 3rd party user of my code.
In my IDE (Windows Visual C++ 2008) I've linked to the relevant .lib files by going to Properties->Linker->Input
How would I go about packaging the .exe so that it works on non-development machines?
i.e. if I include the .dll files, how would I need to change the linker configuration to reflect this ? Or, can the .dll files referenced by the .lib files be incorporated into the .exe ?
Sorry, I know this issue has come up before but I can't seem to find a resolution specific to my case. Any help would be appreciated!
OpenCV is built using dynamic libraries
The correct thing to do is create an installer containing your exe and the correct dlls you need. See https://stackoverflow.com/questions/1285591/installer-recommendation