Message Boards Message Boards

C++ pre-compiler headers will not run with CreateExecutable

Why the Pre header compilations do not run from the execute command in WM? The code run smoothly in Visual Studio 2017. It runs fine when commenting out the #include \"pch.h\" . But this will reader dead most of my C++ work.

Needs["CCompilerDriver`"]
CCompilers[]
hello = CreateExecutable["
  // ConsoleApplication5.cpp : Defines the entry point for the \
console application.
  //
  #include \"pch.h\"
  #include <iostream>;
  using namespace std;
  int main()
  { 
  int numbers[5];
  int * p;
  p = numbers; *p = 10;
  p++;  *p = 20;
  p = &numbers[2]; *p = 30;
  p = numbers + 3; *p = 40;
  p = numbers; *(p + 4) = 50;
  for (int n = 0; n < 5; n++)
  cout << numbers[n]  <<  \" , \" ;
  return 0;
  }
    ", "hellomyfirst", Language -> "C++", 
  "Compiler" -> 
   CCompilerDriver`VisualStudioCompiler`VisualStudioCompiler, 
  "CompilerInstallation" -> 
   "C:\\Program Files (x86)\\Microsoft Visual \
Studio\\2017\\Community", "CompilerName" -> Automatic
  ]
Import["!" <> QuoteFile[hello], "Text"]

But the run fails

Out[138]= {{"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 14.0\\", 
  "CompilerName" -> Automatic}}

During evaluation of In[137]:= CreateExecutable::cmperr: Compile error: C:\Users\jecalderon\AppData\Roaming\Mathematica\SystemFiles\LibraryResources\Windows-x86-64\Working-desktop-jvm8477-5036-12984-15\hellomyfirst.c(4): fatal error C1083: Cannot open include file: 'pch.h': No such file or directory

Out[139]= $Failed

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

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

Out[140]= $Failed
POSTED BY: Jose Calderon
5 Replies

Not sure why I have not been able to see this earlier. IS this in the documentation?

I guess the expectation is that people using this functionality would first learn how to compile C programs from the command line (a terminal). The CreateExecutable syntax is basically a mirror of that. This is one of the reasons why I suggested observing the compilation command with ShellCommandFunction" -> Print.

Also what will be the format if there are multiple libraries locations?

Quoting from my previous message, "The list ({...}) is needed because there may be more than one include directory." If there is more than one include location, put them in a list.

POSTED BY: Szabolcs Horvát

Thanks..!! Not sure why I have not been able to see this earlier. IS this in the documentation? I foundabout the IncludeDirectories option but nothing about the format. Also what will be the format if there are multiple libraries locations?

POSTED BY: Jose Calderon

The include directory should be set to wherever pch.h is located. Assuming it is in C:\mystuff\include, you would use

"IncludeDirectories" -> {"C:\\mystuff\\include"}

The list ({...}) is needed because there may be more than one include directory.

POSTED BY: Szabolcs Horvát

You have to tell the compiler where to find pch.h---see the "IncludeDirectories" option of CreateExecutable. You are already doing this in VS through the GUI. Here you must do it with a compiler option.

Also, whenever compilation fails, be sure to add "ShellCommandFunction" -> Print, "ShellOutputFunction" -> Print to see the compiler command and its output.

POSTED BY: Szabolcs Horvát

Thank you for the hint. I appended the CreateExecutable with what found in the user manual. But still no result

Needs["CCompilerDriver`"]
CCompilers[]
hello = CreateExecutable["
  // ConsoleApplication5.cpp : Defines the entry point for the \
console application.
  //
  #include \"stdafx.h\"
  #include <iostream>;
  using namespace std;
  int main()
  { 
  int numbers[5];
  int * p;
  p = numbers; *p = 10;
  p++;  *p = 20;
  p = &numbers[2]; *p = 30;
  p = numbers + 3; *p = 40;
  p = numbers; *(p + 4) = 50;
  for (int n = 0; n < 5; n++)
  cout << numbers[n]  <<  \" , \" ;
  return 0;
  }
    ", "hellomyfirst", Language -> "C++", 
  "Compiler" -> 
   CCompilerDriver`VisualStudioCompiler`VisualStudioCompiler, 
  "CompilerInstallation" -> 
   "C:\\Program Files (x86)\\Microsoft Visual \
Studio\\2017\\Community", "CompilerName" -> Automatic, 
  "IncludeDirectories" -> $UserBaseDirectory
  ]
Import["!" <> QuoteFile[hello], "Text"]

But still getting errors.

During evaluation of In[5]:= CreateExecutable::cmperr: Compile error: C:\Users\jecalderon\AppData\Roaming\Mathematica\SystemFiles\LibraryResources\Windows-x86-64\Working-desktop-jvm8477-14964-256-3\hellomyfirst.c(4): fatal error C1083: Cannot open include file: 'stdafx.h': No such file or directory

Out[7]= $Failed

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

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

Out[8]= $Failed
POSTED BY: Jose Calderon
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