187

I have created a simple program in C++ with Code::Blocks.

If I run it from Code::Blocks, it works correctly; but if I run it by doubleclicking on the executable file, a window pops up with this message:

The program can't start because libgcc_s_dw2-1.dll is missing from your computer.
Try reinstalling the program to fix this problem.

So, what is the problem? What do I have to do to fix it?

xRobot
  • 25,579
  • 69
  • 184
  • 304

17 Answers17

213

I believe this is a MinGW/gcc compiler issue, rather than a Microsoft Visual Studio setup.

The libgcc_s_dw2-1.dll should be in the compiler's bin directory. You can add this directory to your PATH environment variable for runtime linking, or you can avoid the problem by adding:

-static -static-libgcc -static-libstdc++

to your compiler and linker flags.

If you plan to distribute the executable, the latter probably makes the most sense. If you only plan to run it on your own machine, the changing the PATH environment variable is an attractive option (keeps down the size of the executable).

Updated:

Based on feedback from Greg Treleaven (see comments below), I'm adding links to:

[Screenshot of Code::Blocks "Project build options"]

[GNU gcc link options]

The latter discussion includes -static-libgcc and -static-libstdc++ linker options.

hardmath
  • 8,753
  • 2
  • 37
  • 65
  • I have the same problem as @xRobot, and I couldn't find anywhere to add stuff to compiler flags, so I tried adding it to Other Options, then Linker Options, and the error message still occurred. Is anything wrong with what I'm doing? – Greg Treleaven Feb 06 '11 at 14:35
  • @Greg Treleaven: Just for clarity, you are building an executable with Code::Block, it works as expected inside the IDE but reports an error "can't start because libgcc_s_dw2-1.dll is missing" when you try to run it outside the IDE. You checked the compiler's bin directory, and yes, this DLL is in fact there. You are trying to fix the problem by doing a static linkage, which pulls in compiled code from a library so the DLL (runtime linkage) is not needed, but it isn't working. Two suggestions: Do a clean build (changing options may not trigger a rebuild), and try adding the PATH variable. – hardmath Feb 06 '11 at 21:33
  • @hardmath: Still not working after doing a clean rebuild, so I guess I'll have to use one of the other ways to fix this. Thanks for helping. – Greg Treleaven Feb 07 '11 at 13:06
  • @Greg Treleaven: The reason to try adding the compiler's bin directory to your PATH is to show (if that makes things work) that the executable is still being built for runtime linkage of the standard libraries (DLLs). I'm thinking that the problem is that and we need to find (if you want to do static linkage of library code) where to put the compiler flags. – hardmath Feb 07 '11 at 14:10
  • @Greg Treleaven: Okay! Please have a look at the links I added above about build options for Code::Blocks. – hardmath Feb 07 '11 at 15:29
  • @hardmath: I tried adding MinGW/bin directory to the global directories like in the link. It didn't stop the error. Was this the correct way to add it? – Greg Treleaven Feb 07 '11 at 15:46
  • @Greg Treleaven: I don't think you need to specify a directory for the static linkage (although this is what you did for PATH to get runtime linkage). See the screenshot halfway down the page for adding the static linker options as Linker Settings/Other Linker Options here: http://www.synthedit.com/sdk3_docs/compiler_codeblocks.htm – hardmath Feb 07 '11 at 16:34
  • @hardmath: I thought that I'd found why it wasn't working, because I had been adding -static-libgcc to the linker settings while the project name was selected, not when debug was selected, but adding it to debug still didn't work. =( And on my current project, I'm planning on distributing my program. Even by switching the new MinGW directory to the older one, the same error occurred. – Greg Treleaven Feb 12 '11 at 15:20
  • @Greg Treleaven: I'm going to suggest you open a new question, putting a link back to this one, explaining you got the runtime linkage to work but not the static linkage. A new question would give us more "elbow room" to post some sample project, etc. unhindered by the character limits of the comment box. What do you think? – hardmath Feb 13 '11 at 13:57
  • 5
    This discussion continues (and is solved) [here](http://stackoverflow.com/questions/4984612/program-cant-find-libgcc-s-dw2-1-dll). – David C Aug 24 '12 at 23:38
  • How to add those flags? I am using CodeBlocks with Mingw. Any help is appreciated. – RogUE Oct 23 '16 at 13:31
  • @RogUE: Did you see the link (**Updated** in Answer) to a screenshot of the CodeBlocks project options settings? – hardmath Oct 23 '16 at 14:22
  • Yes I did. But I can't figure it out how to do, it's a lot of information there. I can't understand it as I have only started to learn. – RogUE Oct 24 '16 at 10:45
  • Including **-static-libgcc -static-libstdc++** on the compiling line, solved the issue – PYK Oct 12 '19 at 16:26
  • The third one also necessary to include **-static**. So you have had three of them: **-static -static-libgcc -static-libstdc++** on the compiling line. – Anatolii Kosorukov Aug 20 '20 at 08:29
  • "you can avoid the problem by adding "-static-libgcc -static-libstdc++" to your compiler flags." But how to do this? – convert Feb 05 '23 at 19:10
  • I am also using Code::Blocks and having exact the same problem. – convert Feb 05 '23 at 20:31
  • 1
    @convert: Have a look at the self-answered Question [How can build options be changed in Code::Blocks?](https://stackoverflow.com/questions/33334229/how-can-build-options-be-changed-in-codeblocks) – hardmath Feb 05 '23 at 21:06
36

In Eclipse, you will find it under the project properties > C/C++ Build > Settings > MinGW C++ Linker > Misc

You must add it to the "linker flags" at the top; nowhere else. Then just rebuild.

Eclipse properties screenshot

I have found that linking those statically explodes the size up to 1,400kb even with optimizations. It's 277kb larger compared to just copying over the shared DLLs. It's 388kb larger as well after UPXing everything. Very lose/lose here. Just include the DLLs as the end-user can decide to delete them or not if they have them installed elsewhere.

vbo
  • 13,583
  • 1
  • 25
  • 33
TommyTom
  • 369
  • 3
  • 2
  • 1
    Is there a way to avoid adding the linker flag in each newly created project? – Roger Ng Dec 07 '12 at 02:10
  • To the readers: Please note the options added in the image. This works. This helps as a reference: https://orfe.princeton.edu/help/article-296 – PALEN Dec 30 '14 at 18:31
16

Code::Blocks: add '-static' in settings->compiler->Linker settings->Other linker options.

user1826947
  • 161
  • 1
  • 2
8

See also. It solved my problem.

By the way, is it definitely compiler flag? Maybe linker is the more suitable term here?

datatype_void
  • 433
  • 5
  • 23
fat
  • 6,435
  • 5
  • 44
  • 70
  • 1
    +1 for correct terminolgy! Yes, my "link" to gcc link options was an inkling of that (for the static libraries). – hardmath Dec 19 '11 at 17:12
7

Find that dll on your PC, and copy it into the same directory your executable is in.

Dave
  • 3,438
  • 20
  • 13
  • why does not this happen with visual studio ? – xRobot Jan 15 '11 at 23:46
  • 1
    It also does, but with other dlls. Examples, if you lack msvcrt90.dll, your visual compiled project won't start (iut's usually installed system wide though) – Bruce Jun 27 '12 at 06:02
6

Copy "libgcc_s_dw2-1.dll" to were make.exe is. (If you are using Msys, copy it to \msys\bin) Make sure that the path to make.exe is set in the env. PATH (if make.exe is in a folder "bin", most likely, and you have msys, it's \msys\bin) Compile, rund, debug, etc. happy.

Blizz
  • 61
  • 1
  • 1
5

Go to the MinGW http sourceforge.net tree. Under Home/MinGW/Base/gcc/Version4(or whatever version use are using)/gcc-4(version)/ you'll find a file like gcc-core-4.8.1-4-mingw32-dll.tar.lzma. Extract it and go into the bin folder where you'll find your libgcc_s_dw2-1.dll and other dll's. Copy and paste what you need into your bin directory.

5

I was able to overcome this by using "gcc" instead of "g++" for my compiler. I know this isn't an option for most people, but thought I'd mention it as a workaround option :)

rogerdpack
  • 62,887
  • 36
  • 269
  • 388
4

Just go to Settings>>Compiler and Debugger, then click the Linker Settings tab and go over to the "Other linker options" edit control and paste: "-static-libgcc -static-libstdc++" to it, there is no compiler flag option in the Compiler Flags options for Code::Blocks so that's the way to solve that problem, I came here looking for a solution also and the one guy that posted about "-static-libgcc -static-libstdc++" gave the right idea, and I sort of figured the rest out by accident but it worked, the file is clickable now from outside Code::Blocks, works right from the desktop.

4

Can't you put it in system32 or something like you do with others dll files, so that every program you try to run won't have that problem on your machine?

I just need the path where to put it.

It is kinda annoying to put it in the directory every time I run a program I just built...

Edit: I found the solution:

Extract libgcc_s_dw2-1.dll to a location on your computer. We recommend you to unzip it to the directory of the program that is requesting libgcc_s_dw2-1.dll.

If that doesn't work, you will have to extract libgcc_s_dw2-1.dll to your system directory. By default, this is:

  • C:\Windows\System (Windows 95/98/Me)
  • C:\WINNT\System32 (Windows NT/2000)
  • C:\Windows\System32 (Windows XP, Vista, 7)

If you use a 64-bit version of Windows, you should also place libgcc_s_dw2-1.dll in C:\Windows\SysWOW64\

Make sure overwrite any existing files (but make a backup copy of the original file). Reboot your computer.

If the problem still occurs, try the following:

  • Open Windows Start menu and select "Run...".
  • Type CMD and press Enter (or if you use Windows ME, type COMMAND)).
  • Type regsvr32 libgcc_s_dw2-1.dll and press Enter.
user229044
  • 232,980
  • 40
  • 330
  • 338
yossi
  • 57
  • 1
  • 12
    Please don't post comments as answers here. That isn't how Stack Overflow works. You also won't get far using "u" instead of "you" and "cos" instead of "because". Chat speak is specifically disallowed here. – user229044 Jan 31 '12 at 14:56
3

Add path to that dll into PATH environment variable.

Bojan Komazec
  • 9,216
  • 2
  • 41
  • 51
  • why does not this happen with visual studio ? – xRobot Jan 15 '11 at 23:49
  • Probably because path to that dll is listed in VisualStudio's Tools-> Options->Projects and Solutions -> VC++ Directories -> Executable Files. Visual Studio looks here and in PATH environment variable when searching for paths to dlls. – Bojan Komazec Jan 15 '11 at 23:59
3

In CodeBlocks you can go to Settings...Compiler... and choose either 1) the two items in the blue box or 2) the one item in the green box

codeblocks compiler settings

raddevus
  • 8,142
  • 7
  • 66
  • 87
3

If you are wondering where you can download the shared library (although this will not work on your client's devices unless you include the dll) here is the link: https://de.osdn.net/projects/mingw/downloads/72215/libgcc-9.2.0-1-mingw32-dll-1.tar.xz/

  • 1
    FINALLY! After decades of searching through a ridiculous number of ridiculously named MinGW download release files! What a save, thanks. – nyov Jan 15 '21 at 11:07
2

Add "-static" to other linker options solves this problem. I was just having the same issue after I tested this on another system, but not on my own, so even if you haven't noticed this on your development system, you should check that you have this set if you're statically linking.

Another note, copying the DLL into the same folder as the executable is not a solution as it defeats the idea of statically linking.

Another option is to use the TDM version of MinGW which solves this problem.

Update edit: this may not solve the problem for everyone. Another reason I recently discovered for this is when you use a library compiled by someone else, in my case it was SFML which was improperly compiled and so required a DLL that did not exist as it was compiled with a different version of MinGW than what I use. I use a dwarf build, this used another one, so I didn't have the DLL anywhere and of course, I didn't want it as it was a static build. The solution may be to find another build of the library, or build it yourself.

Neil Roy
  • 603
  • 5
  • 13
  • 1
    Adding "-static" to linker and compiler options is defenetly the best solution. At least it worked for me. – convert Feb 06 '23 at 13:13
2

Including -static-libgcc on the compiling line, solves the issue

g++ my.cpp -o my.exe -static-libgcc

According to: @hardmath

You can also, create an alias on your profile [ .profile ] if you're on MSYS2 for example

alias g++="g++ -static-libgcc"

Now your GCC command goes thru too ;-)

Remeber to restart your Terminal

PYK
  • 3,674
  • 29
  • 17
2

Step 1: Set the -static-libgcc and -static-libstdc++ flags, or, set the -static flag under settings -> compiler -> global compiler settings | Compiler settings | Compiler Flags

Step 2 (not stressed in the other answers): Clean your build before building and running. Without cleaning, you may think that you are rebuilding the project but you will end up with the same .exe. Cleaning will, amongst other things, delete the files in the bin and obj folder so that when you build, a fresh new set of files are created in bin and obj folder.

Dean P
  • 1,841
  • 23
  • 23
0

Working with msys2 I have obtained the same error trying to execute release version of my project in a debug environment. The solution for my problem is obvious: use executable with debug symbols.

Rubén Pozo
  • 1,035
  • 1
  • 12
  • 23