2
$\begingroup$

I am new at using CCompilerDriver[]. I call the C compiler from W Mathematica using recent WM 11.3 update. It successfully identifies the Visual Studio 2017, But calling compiler returns errors.

Needs["CCompilerDriver`"]
CCompilers[]
hello = CreateExecutable["
  #include <stdio.h>
  int main(){
    printf(\"hello world\\n\");
  }
  ", "hello"]
Import["!" <> QuoteFile[hello], "Text"]

I copy the sample test code from the WM documentation CCompilerDriver/ref/CreateExecutable

The output shows to recognize VS2017 , but fails to compile due to files missing. Please help with how to tell WM to find the missing files.

Out[2]= {{"Name" -> "Visual Studio", "Compiler" ->
CCompilerDriverVisualStudioCompilerVisualStudioCompiler,
"CompilerInstallation" -> "C:\Program Files (x86)\Microsoft Visual \ Studio\2017\Community", "CompilerName" -> Automatic}, {"Name" -> "Visual Studio", "Compiler" ->
CCompilerDriverVisualStudioCompilerVisualStudioCompiler,
"CompilerInstallation" -> "C:\Program Files (x86)\Microsoft Visual \ Studio\2017\BuildTools", "CompilerName" -> Automatic}}

During evaluation of In[1]:= CreateExecutable::cmperr: Compile error: C:\Users\user\AppData\Roaming\Mathematica\SystemFiles\LibraryResources\Windows-x86-64\Working-asus-8568-6984-1\hello.c(2): fatal error C1083: Cannot open include file: 'stdio.h': No such file or directory

Out[3]= $Failed > > During evaluation of In[1]:= StringJoin::string: String expected at > position 2 in !<>QuoteFile[$Failed].

During evaluation of In[1]:= Import::chtype: First argument !<>QuoteFile[$Failed] is not a valid file, directory, or URL > specification. > > Out[4]= $Failed

Additional information about my machine

version 11.3.0 for Microsoft Windows64-bit March 7, 2o18 Release ID 11.3.0.0

I have installed both compilers, the Visual Studio IDE and the Visual Studio Built Tools

1 st UPDATE:::

I forgot to mention that I am also beginner to C++ . I compiled the code above in VS2017 IDE and returned errors. It looks the samples from the WM Documentation do not necessarily compile in the C++ compiler.

Have this said, I replaced the code with a sample code that showed to compile in VS2017 IDE. The result appears to have compiled in WM, but can not the Import line returned error. I can not associate this error to the compilation is related at all. Please , comment.

Needs["CCompilerDriver`"]
CCompilers[]

hello = CreateExecutable["

    // Hello100.cpp : Defines the entry point for the console \
application.
    //

    #include " stdafx.h "
    #include <iostream>
    // i/o example
    // stringstreams

    #include <string>
    #include <sstream>
    using namespace std;

    int main()
    {
        string mystr;
        float price = 0;
        int quantity = 0;
        int next;
        cout << " Enter price : ";
        getline(cin, mystr);
        stringstream(mystr) >> price;
        cout << " Enter quantity : ";
        getline(cin, mystr);
        stringstream(mystr) >> quantity;
        cout << " Total price : " << price * quantity << endl;
        cin >> next;
        return 0;
       }

         ", "hellomyfirst"];

Import["!" <> QuoteFile[hellomyfirst], Text]

The output :

In[36]:= Import["!" <> QuoteFile[hellomyfirst], Text]

During evaluation of In[36]:= StringJoin::string: String expected at position 2 in !<>QuoteFile[hellomyfirst].

During evaluation of In[36]:= Import::chtype: First argument !<>QuoteFile[hellomyfirst] is not a valid file, directory, or URL specification.

Out[36]= $Failed

2 st UPDATE:::

Found the issue.. It is correct all the comments shared by contributors. Need the C++ SDK .. but must important is that in addition must have Windows 10 In my case, my laptop was using Windows 8.1.

$\endgroup$
  • $\begingroup$ What goes in Import should be the path to the compiled executable. $\endgroup$ – ilian May 1 '18 at 15:21
  • $\begingroup$ Also it is questionable that the compilation succeeded, because there are unescaped quotes and backslashes in the source string. $\endgroup$ – ilian May 1 '18 at 15:36
  • $\begingroup$ The remaining question has been cross-posted and answered on Wolfram Community. $\endgroup$ – ilian May 1 '18 at 16:41
2
$\begingroup$

Hm. You can look up the location of stdio.h on your system and add that manually to the IncludeDirectories for CreateExecutable like this (example path is the path on my macos system and will differ from what you need):

includepath = "/usr/include/c++/4.2.1";
SetOptions[
 CreateExecutable,
 "IncludeDirectories" -> 
  Append["IncludeDirectories" /. 
    Options[CreateExecutable, "IncludeDirectories"], 
    includepath
   ]
 ]

Still, something is fishy about the fact that Mathematica does not find it on its own. Did you install Mathematica before VisualStudio?

$\endgroup$
  • $\begingroup$ Schumacker Yes.. I did. including all the several updates . $\endgroup$ – Jose Enrique Calderon May 1 '18 at 14:21
  • 3
    $\begingroup$ I am not sure, but if you installed Mathematica before the compilers, then maybe Mathematica did not find all relevant paths. I don't know how to let Mathematica search after installation. So, reinstalling Mathematica might resolve this problem. But that is only a guess... (like switching the computer off and on again ;) ) $\endgroup$ – Henrik Schumacher May 1 '18 at 14:24
2
$\begingroup$

When installing Visual Studio 2017, make sure that the "Universal CRT SDK" component is selected (see the last checkbox), which should provide the relevant header files.

enter image description here

(Since there appear to be multiple issues involved, this is more of a comment than an answer, and is only intended for the problem in the original version of the question: compilation failing due to a missing system header file.)

$\endgroup$
  • $\begingroup$ I had all these except for WIndows 8.1 SDK. But still same problem. $\endgroup$ – Jose Enrique Calderon May 1 '18 at 15:30
  • $\begingroup$ The point is that UCRT SDK must be installed in order to have headers like stdio.h or stddef.h. $\endgroup$ – ilian May 1 '18 at 15:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.