21

I'm running Mathematica 9 on a 64 bit Windows 8.1 laptop. I'm having no luck getting C compilation to work.

Needs["CCompilerDriver`"]

CCompilers[Full]
CCompilers[] 

returns

{}  
{}

The package did load successfully, as $Packages shows CCompilerDriver entries and

$ContextPath//First

returns

"CCompilerDriver`"

I have the following compilers installed:

  • Microsoft Visual Studio Professional 2013
  • Intel C++ Composer XE
Szabolcs
  • 234,956
  • 30
  • 623
  • 1,263
user11990
  • 311
  • 2
  • 3
  • Is it possible for you to install MS VS 2012 and try again to set up the compiler ? – Sektor Feb 04 '14 at 14:54
  • AFAIK under Windows the PATH to the Visual Studio compiler is not set system wide. Can you try to add VC to your PATH and restart Mathematica? – halirutan Feb 04 '14 at 14:55
  • @halirutan Oh, I had no trouble running VS 2012 and compile str8 after install :) – Sektor Feb 04 '14 at 14:59
  • @halirutan I don't remember the details, but it does not find the location of VS using PATH. I think it uses another environment variable ... – Szabolcs Feb 04 '14 at 16:28
  • I don't have access to Windows at this moment, but I looked at the VS compiler driver, and it looks like it find the location of the compiler based on the VS110COMNTOOLS environment variable. There are lines like installPath["2012"] := installPathFromEnvironment["VS110COMNTOOLS"] for 2005, 2008, 2010 and 2012, but not 2013. I don't know how your version of VS indicates its location, but you could try CCompilerDriver`VisualStudioCompiler`Private`installPath["2013"] := ... or whatever is appropriate for you compiler, and trying again. – Szabolcs Feb 04 '14 at 16:33
  • Take a look inside the package file AddOns\Applications\CCompilerDriver\VisualStudioCompiler.m and look for the lines I mentioned, possible try to add a new definition suitable for your compiler (after backing up the original version of this package file!!) Let me know if this has worked. As I said, I cannot test it. – Szabolcs Feb 04 '14 at 16:34
  • I am having the same problem with VS2013 in Mathematica 9.01 But when I tried to save the visual studio compiler file, the system did not allow. Any way to go around this? – Jose Enrique Calderon Apr 13 '14 at 04:56
  • @JoseECalderon Right click the file, then Properties -> Security tab -> Edit Permissions Users -> allow Modify – Sjoerd C. de Vries Oct 26 '14 at 22:30

2 Answers2

24

Visual Studio 2013 is not supported out of the box by Mathematica 9.0.1. To fix this,

  • Navigate to AddOns\Applications\CCompilerDriver within the Mathematica installation directory.

  • Back up VisualStudioCompiler.m as we're going to modify it.

  • Open the file and search for the line starting with installPath["2012"]

  • Add the following line below it:

    installPath["2013"] := installPathFromEnvironment["VS120COMNTOOLS"]
    
  • Search for the line $VisualStudioVersions = {"2012", "2010", "2008", "2005"} and add "2013" at the beginning, like this:

    $VisualStudioVersions = {"2013", "2012", "2010", "2008", "2005"}
    
  • Save the file, restart Mathematica, and try again.

Szabolcs
  • 234,956
  • 30
  • 623
  • 1,263
  • Well, it seems that this was a hit and run question. The OP has disappeared. If anyone can test if this solution is working, please comment so we can finalize or delete the answer. – Szabolcs Feb 05 '14 at 18:15
  • I can confirm that this does not fix the issue. Think I will have to reinstall VS2012 now, crap! – RunnyKine Feb 08 '14 at 19:39
  • @RunnyKine Before you do that, let's experiment a bit ... does the VS120COMNTOOLS environment variable exist on your system? What is its value? – Szabolcs Feb 08 '14 at 19:43
  • It exists, I can't see the full value but it's C:\Program Files (x86)\Microsoft Visual Studio 12.0 ... – RunnyKine Feb 08 '14 at 19:50
  • @RunnyKine OK, did you actually edit the file I referenced in the post, the way I described? (Make a backup of the original and put the backup in some other directory.) If not, please try actually editing, and make one more change: look up the line starting with $VisualStudioVersions and add "2013" at the beginning of that list. Let's see if it works when both changes are done. – Szabolcs Feb 08 '14 at 19:54
  • Aha, that does it. Thanks a lot! – RunnyKine Feb 08 '14 at 20:00
  • @RunnyKine Great! Thanks for confirming, than we finally have a solution :-) – Szabolcs Feb 08 '14 at 20:11
  • @Szabolcs it works; I remember discussing this with PlatoManiac in the chat where I proposed the same approach: http://chat.stackexchange.com/transcript/message/13077001#13077001. Apparently there is not much to it for supporting new VS versions. – Oleksandr R. Feb 09 '14 at 00:44
  • Mathematica 10.0.1.0 running under Windows 8.1 automatically recognizes the C compiler loaded with Visual Studio Express 2013 Update 3. It is, however, necessary, to run VS 2013 once in order to allow it to initialize itself. – bbgodfrey Oct 15 '14 at 21:34
  • @bbgodfrey. I have exactly the same configuration, but it doesn't work for me. I did run VS 2013 as you wrote, but no fun so far. Any ideas? – Sjoerd C. de Vries Oct 26 '14 at 22:13
  • @@bbgodfrey. OK, a restart of my laptop did the trick. – Sjoerd C. de Vries Oct 26 '14 at 22:26
20

Here's how to get Mathematica 10.2 to recognize Visual Studio 2015's (Community Edition) C compiler (Windows 8.1).

  • In the Mathematica installation folders, go to SystemFiles / Components / CCompilerDriver
  • Make a backup copy of VisualStudioCompiler.m (disaster recovery)
  • Open VisualStudioCompiler.m
  • Scroll to $VisualStudioVersions and add "2015" to the list
  • Scroll to installPath["2013"] and after it, include this new line

    installPath["2015"] := installPathFromEnvironment["VS140COMNTOOLS"]
    

These hiccups bit me:

  • Although I could edit VisualStudioCompiler.m, it wouldn't save to the above folder
  • Instead, save it elsewhere and then copy it into the above folder
  • It's not enough to simply install VS2015 -- once installed, within it you must yet install the C++ compiler
  • Once the C++ compiler is installed, open Mathematica and run:

    Needs@"CCompilerDriver`"
    
    CCompilers@Full
    

You should see Visual Studio in the output list -- you're good to go

Szabolcs
  • 234,956
  • 30
  • 623
  • 1,263
bcolletti
  • 379
  • 2
  • 8
  • 1
    I fail to see how that's different than the previous answer. – Sektor Aug 19 '15 at 05:28
  • My reply was posted per another's request elsewhere. It addresses VSP 2015 and not 2013, and the environment variable is different. It also points out the secondary installation that makes Mathematica recognize VSP. – bcolletti Aug 19 '15 at 06:09
  • @Sektor I asked bcolletti over on Wolfram Community to post information if he gets this working. One difference that might trip up some users is that VS2015 uses VS140COMNTOOLS because they skipped version number 13. I didn't update my answer because I didn't have VS2015 to test with. – Szabolcs Aug 19 '15 at 09:22
  • @Sektor Another difficulty that's addressed is editing write-protected files on Windows. While editing the other (community wiki) answer would have been a better solution, perhaps the system won't allow new members or low-rep members to edit? Generally, "community wiki" means that editing is welcome (and I thought it also means that the system allows all users to do so). – Szabolcs Aug 19 '15 at 09:30
  • @Szabolcs Yes, I read the answer. For me -- it brings nothing new to the table assuming knowledge of your previous answer. That is probably not the case for everyone as I had to set up MinGW(GCC) and VS15 compilers (close to) a month ago on Windows machine. About the write-protected files -- That's not related to Mathematica, I think. – Sektor Aug 19 '15 at 10:38
  • @Szabolcs I think that this is no longer necessary in most recent versions. (We already have the latest FunctionCompile, which will probably replace the traditional Compile in some future release!) – user688486 Oct 16 '23 at 05:40