Sublime Forum

How compile C programs saved in any folder using Sublime Text 3?

#1

Hey! I can compile any C program saved in any folder with Code Blocks, but when I tried the same with ST3 I couldn’t. Using ST3 I can only compile and run programs that are saved in the folder C:\MinGw\bin. But that is very unpractical. Can someone tell me how to compile and run programs from any folder?

0 Likes

#2

Are you using a custom build system? This should work by default, although I admit I’ve never tried it under Windows. Can you share what sort of errors you’re getting?

0 Likes

#3

Yeap. My actual C build is:

{
 "cmd": ["gcc", "${file}", "-o", "${file_base_name}.exe"],
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
"working_dir": "${file_path}",
"selector": "source.c",
"shell": true,

"variants":
[
    {
        "name": "Run",
        "cmd": ["start", "cmd", "/k", "${file_path}/${file_base_name}.exe"],
        "shell": true
    }
]
}

If I try to run a file that is not in the folder C:\MinGW\bin appears this:
*** " name.exe - Error del sistema: El programa no puede iniciarse porque falta libgcc_s_dw2-1.dll en el equipo. Intente reinstalar el programa para corregir este problema."***

But this file “libgcc_s_dw2-1.dll” exists.

So if I try to run the same file but this time from de folder C:\MinGW\bin: It works fine.

0 Likes

#4

That’s indicating that everything compiled OK, but it can’t run the executable because it can’t find the DLL file it needs which presumably is in the directory you mentioned above. This is sort of weird because as far as I know (although I am by no means an expert and it’s been a while) Windows will search for DLL files that it needs along the path, so if it can find gcc it should be able to find the DLL that it needs.

There are a few solutions to this particular problem. You could:

  • Modify your gcc line to statically link your application so that it doesn’t need the DLL at all. Offhand I don’t know how to do that with MinGW, but under Linux something like gcc test.c -Wl,-Bstatic -o test would do it (assuming you have a static libc). The downside to this is that it makes your executable bigger and require more memory to run.
  • Copy the dll file from the location it’s in to the directory where the exe file ends up when you build it. That requires more setup for new projects however.
  • Copy the dll file from the location it’s in to someplace that it will be found when the system looks for DLL’s. As I mentioned above, I would have thought that it would search the path, but in a pinch if you put the DLL in the windows directory or windows system directory, that should work.
1 Like

#5

yeap. The last opcion works, but is not very “clean”.
I was thinking that maybe is posible to specify something in the build. I did not know very much of all this yet.
Ill keep this library in the proyects folder.
Thank you OdaNurd.

0 Likes

#6

I agree, it’s not a very clean solution. I tried to replicate your results on my machine (Windows 7 x64) where I also have MinGW installed in C:\MinGW as you do.

However, your build system doesn’t work for me (as I would have expected) since I don’t have C:\MinGW\bin in the path, so windows can’t find gcc to run it. Trying to hard code the path to gcc directly to C:/MinGw/bin/gcc causes gcc to fail due to the same error you’re having regarding not being able to find the DLL it needs.

In this case I was able to get gcc to work by adding the following to your build system to augment the system path:

   "path": "c:/mingw/bin",

Of course, this makes gcc work just fine; however once the code is built, the executable can be executed without this in place, so it’s only gcc that has the problem and not the compiled code itself, which is the opposite of the situation you’re facing.

I don’t know if that means that you’re running a newer version of windows that changes how DLL’s are searched or if you perhaps have a batch file named gcc.bat in the path that’s hard coding the path to gcc?

Might be something to try, though.

0 Likes

#7

I been trying some things, i check the path, copied and delete the dll from de proyects folder,…even i decided to left the files in C:/MinGW… I dont know why, but now is working. I can save anywhere, compile and run. I dont understand what happened. Later I will try to configure all this in my other pc and see.

When I put the absolute path (“path”: “C:\MinGw\bin”) in the build it did not work.
I readed something about bach files but I don’t know what they do yet.

I hope all this still working fine.

Thank you so much

0 Likes