Group Abstract Group Abstract

Message Boards Message Boards

0
|
14.9K Views
|
12 Replies
|
4 Total Likes
View groups...
Share
Share this post:

Trouble with a C++ Code compilation using VS 2017

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:
POSTED BY: Jose Calderon
12 Replies

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

POSTED BY: Jose Calderon
Anonymous User
Anonymous User
Posted 8 years ago

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\
}" 
POSTED BY: Anonymous User
POSTED BY: Jose Calderon

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

onlybuildtools

POSTED BY: Shenghui Yang
POSTED BY: Jose Calderon

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.

POSTED BY: Jose Calderon
Posted 8 years 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"
POSTED BY: Sean Cheren
Posted 8 years 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.

POSTED BY: Sean Cheren

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

POSTED BY: Jose Calderon
Posted 8 years 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

POSTED BY: Sean Cheren
POSTED BY: Jose Calderon
Posted 8 years ago

Jose,

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

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