Message Boards Message Boards

GROUPS:

Trouble with a C++ Code compilation using VS 2017

Posted 6 months ago
1288 Views
|
12 Replies
|
4 Total Likes
|

I am calling a C++compiler from VS2017 . But getting an error.

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]

I am getting the following error

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

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

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

Out[32]= $Failed

Why am not getting the output ? The code compiled in Visual Studio ok.

1st UPDATE

The Visual Studio 2017 C++ SDK option will work only under Windows 10. I was running this under windows 8.1. I updated to W10 and got it to work.

Attachments:
12 Replies
Posted 6 months ago

Jose,

It looks like you need to escape the " and \ symbols (\" and \\)

Posted 6 months ago

I also needed to add Language -> "C++"

Import["!" <> QuoteFile[hellomyfirst], "Text"] (needs " around Text) seems to hang waiting for input.. It finishes executing if I remove the input code.

Do you mean that I need to eliminate the comment lines or something else?

Posted 6 months ago

I had to remove getline and stringstream lines since they were waiting for input and hanging the kernel. I would look at this post for some info on how to open a pipe as an alternative: https://stackoverflow.com/questions/6462275/mathematica-and-c-c-exchanging-data

I am not sure what you mean.. DO you mean to eliminate the comment lines? Or something else?

I had to remove getline and stringstream lines since they were waiting for input and hanging the kernel. I would look at this post for some info on how to open a pipe as an alternative: https://stackoverflow.com/questions/6462275/mathematica-and-c-c-exchanging-data

It be great if you post the code since I am not able follow the changes that you proposed here .

Posted 6 months ago

In[25]:= 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", Language -> "C++"]



Out[26]= {{"Name" -> "Visual Studio", 
  "Compiler" -> 
   CCompilerDriver`VisualStudioCompiler`VisualStudioCompiler, 
  "CompilerInstallation" -> 
   "C:\\Program Files (x86)\\Microsoft Visual \
Studio\\2017\\Professional", 
  "CompilerName" -> Automatic}, {"Name" -> "Visual Studio", 
  "Compiler" -> 
   CCompilerDriver`VisualStudioCompiler`VisualStudioCompiler, 
  "CompilerInstallation" -> 
   "C:\\Program Files (x86)\\Microsoft Visual Studio 12.0\\", 
  "CompilerName" -> Automatic}}

Out[27]= "C:\\Users\\scheren\\AppData\\Roaming\\Mathematica\\\
SystemFiles\\LibraryResources\\Windows-x86-64\\hellomyfirst.exe"

In[30]:= Import["!" <> QuoteFile[hello], "Text"]

Out[30]= " Enter price :  Enter quantity :  Total price : 0"

I am calling a C++compiler from VS2017 . But getting an error.

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]

I am getting the following error

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

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

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

Out[32]= $Failed

Why am not getting the output ? The code compiled in VIsual Studio ok.

This really puzzles me. I am runnig exacty your same code but returns errors in my computer.

In[1]:= Needs["CCompilerDriver`"]
CCompilers[]

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

In[3]:= 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", Language -> "C++"]



During evaluation of In[3]:= CreateExecutable::cmperr: Compile error: C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.13.26128\include\crtdefs.h(10): fatal error C1083: Cannot open include file: 'corecrt.h': No such file or directory

Out[3]= $Failed

In[4]:= Import["!" <> QuoteFile[hello], "Text"]

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

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

Out[4]= $Fail

I notice that you are using the the 2017 Profesional and the VS 2012. Is there anything reported from 2017 COmmunity that might limit this compilation?

I installed build tools only on my surface pro and V11.3. Run the example code without issue.

onlybuildtools

I thanks for all your comments. I tested on Mac OS X and compiles flawlessly under gcc and Clang compilers. Still puzzles why not getting my Windows 10 VS2017 to compile. I have tested setting the options for pointing specific to the BuildTool compiler but still getting an error. It looks like a problem with WM detecting the libraries. but I am not an authority and not able to be sure if the problem is with the VS installation itself.

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", Language -> "C++",
  "Name" -> "Visual Studio", 
  "Compiler" -> 
   CCompilerDriver`VisualStudioCompiler`VisualStudioCompiler, 
  "CompilerInstallation" -> 
   "C:\\Program Files (x86)\\Microsoft Visual \
Studio\\2017\\BuildTools", "CompilerName" -> Automatic
  ]
Import["!" <> QuoteFile[hello], "Text"]

Here is the output:

Out[11]= {{"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}}

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:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\VC\Tools\MSVC\14.13.26128\include\crtdefs.h(10): fatal error C1083: Cannot open include file: 'corecrt.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

Think out of the box about that quoting problem: you likely shouldn't be adding slashes by hand (it might waste your time and leave you with code that later you have to re-convert to being "normal c" that gcc will accept)

In[4]:= hello = CreateExecutable[Import["hello.c", "Text"] , "hello"]

Out[4]= "/users/`whoami`/Library/Mathematica/SystemFiles/\
LibraryResources/MacOSX-x86-64/hello"

In[5]:= Import["!" <> QuoteFile[hello], "Text"]

Out[5]= "hello world"

Import["x.c", "Text"]

"#include <stdio.h>\nint main(void)\n{\n  printf(\"hello world\");\n\
}" 

Had the problem resolve. I was using WIndows 8.1 . The C++ SDK will only operate with the libraries under 10.0

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract