Can I link MSVCRT statically with mingw? - c++

I have C program I compile with mingw on Windows. It works fine but requires MSVCRT.DLL. I want to link that statically (like I can do in Visual Studio). Is this possible?
I tried -static flag to gcc and it didn't make any change.
What about C++ program using also standard C++ library?

I believe that MinGW doesn't use the static runtime library for copyright reasons.
You can maybe try to use newlib (http://sourceware.org/newlib/) to create an executable that doesn't link to msvcrt.dll

Having spent a while searching for this myself, I came across this post introducing the in-progress Mingw32 Alternate C Runtime Library. However, it's not ready for use, and the developer appears to have abandoned it. Home page link here.
Posting this answer here in the hope that it'll come in useful to people googling at some point in future.

This doesn't answer the question of "how to avoid linking with MSVCRT.DLL", but if you're here for a more general question of "How to link with MinGW statically"...:
Link with gcc -static -static-libgcc -static-libstdc++ and you won't need libgcc_s_dw2-1.dll and libstdc++-6.dll

Related

problems with running c++-programs

I wrote a c++-program including <iostream> to use std::cout and std::cin. I compiled it with the g++ compiler (GNU compiler collection) on Windows 10 using MinGW. When I run the program with the run-terminal of MinGW it works but when I try to run it with cmd.exe or open it in Windows Explorer I get this Error:
"The program cannot be run because libstdc++-6.dll is missing. Please reinstall the program to solve the problem."
Because I didn´t install my program, I tried to install libstdc++-6.dll. I downloaded the file in zip-format but I don´t know where to unpack. Has this file to be in system32? Do I have another problem? Can anyone help me to solve it? I already read a simliar question and its answers but -static-libgcc -static-libstdc++ didn´t work.
This is a repetition of this question libstdc++-6.dll not found according to #kerrek-sb do this
If you are using MingW to compile C++ code on Windows, you may like to add the options -static-libgcc and -static-libstdc++ to link the C and C++ standard libraries statically and thus remove the need to carry around any separate copies of those. Version management of libraries is a pain in Windows, so I've found this approach the quickest and cleanest solution to creating Windows binaries.
You might want to compile your code with g++ and the options -static-libgcc and -static-libstdc++ so to link the C and C++ standard libraries statically. As a result you don't have to install libraries in your Windows path and you can carry around the executable on other systems.

C++ - How to make a program compiled with MinGW standalone?

I am working on a project(for Windows) that is small and should be portable. I saw somewhere that MinGW gets rid of the requirement of some .dlls on the target system, so I thought I'd give it a try. To my suprise, when I tried running my program on a friend's computer, I got two errors saying that 2 different .dlls were missing. I thought MinGW used the system dlls, but it obviously didn't work. I saw an article on how to make a standalone exe with Visual Studio, but I'd prefer to use MinGW due to it's simplicity.
So my question: How to produce a standalone exe with MinGW?
Note: I am only using standard libraries, but it'd be nice to know how to include other libraries for future projects.
If you'd like an example of what I mean by a standalone exe, putty is a good example(it is an ssh client for Windows).
The two libraries you needed were most likely libgcc_s_sjlj-1.dll and libstdc++-6.dll or some variation of these, depending on how your mingw was configured when it was built. These are C and C++ standard library implementations.
If you want, you can pass extra flags to the compiler when you are linking the final executable, -static-libgcc and -static-libstdc++, which will cause it to link these libraries in statically instead of requiring you to distribute DLLs for them with your executable.
More info here: Mingw libgcc_s_sjlj-1.dll is missing
In general you can always try to statically link libraries into your executable. These special -static-libgcc flags are special flags provided only by mingw, not by other compilers, and not for other libraries, you use a different -static syntax for other libs. Static linking is fine but it gets more complicated and error prone as the dependencies get more complex. Static linking has some pitfalls associated to the order in which the libraries are linked. After a certain point, its usually simpler to make them all shared and don't do any static linking. It's up to you, it depends on how complex your project gets / if you start to have problems.

compiling c++ into "real" programs

I know how to use g++ and all that to compile c++ programs.
My question is, if I have some code which depends on various libraries, how can I compile it into a simple executable that I can send anyone. For this I would be happy with just keeping it on os x.
I would like to know how to compile a "real" program not just an executable I can run locally.
I have tried googling this but haven't found much.
Do I have to use installing software?
I know in windows you can make some simple .exe stuff that use common DLL files.
You a looking for "static linking". That will import all the needed code from the libraries into your executable. Note the executable will get larger. If you are using standard libraries, they should be present on standard OS installation.
You should try "-static" flag of g++.
Running "ldd your_executable_name" should display all libraries your executable uses (linked dynamically).
Since you are talking about Mac OS X, you probably want to make a bundle. Qt Software has a very useful deployment guide for getting started with this kind of activity.
You can use -static or -s option for static linking in gcc or g++

Linking VS2005 static library with gcc in Windows

Is it possible to link a static library built with VS2005 into an application that is to be built with gcc (in Cygwin)?
Unlike UNIX where there was no standard C++ ABI for years, Windows has had a standard C++ ABI from the beginning. So, yes, it's possible. But it can be difficult.
Theoretically it should work, but I would suggest to just try it.
I used to link a *.dll in G++ which compiled with a *.lib, It works well currently, but I'm not sure about static library, May be you can compiled it in dll for a try.
Good luck.

Tool Chain for WxWidgets explained

Where can I find an writeup that shows me how to set up a tool chain for WxWidgets (C++) on linux/ubunto and/or OS X.
I downloaded, compiled & installed WxWidgets both on linux and OS X, compiled and tried the samples, but seem to be stuck setting up a compile environment in my own home directory.
DialogBlocks from http://www.dialogblocks.com looked promising, but it insists on recompiling WxWidgets again and again .. must be something about it I don't understand.
Writing code from scratch seems to fail due to a lack of paths to libraries, tools or whatnot .. again a lack og understanding on my part, I am sure.
So, can anyone point me to a tool chain setup, that has more than the bare minimum of instructions and fills in some of the "why" instead of only the minimal "what".
Like all C/C++ programs, the compiler has to know in what directories to look for include files, and the linker has to know what libraries it should link to.
The WxWidgets package, if installed correctly, includes the program wx-config. This can be used while compiling and linking, like so:
g++ $(wx-config --cxxflags) -c my_prog.cpp
g++ my_prog.o $(wx-config --libs) -o my_prog
I've found these two pages to be of help when setting up wxWidgets for Eclipse and MinGW.