CppSharp
CppSharp copied to clipboard
error: unknown type name 'namespace'
Brief Description
I have issues with generating code for file which includes a lot of MSVC things. I'm going to have a C# binding for my C++ class.
I'm basing on https://github.com/mono/CppSharp/blob/master/examples/Parser/Parser.cs (just a POC without creating a custon ILibrary implementation).
What I'm doing wrong?
Environment
OS: Windows 10 CppSharp 0.11.2 from nuget VisualStudio 2019, c++17 projects
Used headers
Used settings
using var options = new CppSharp.Parser.ParserOptions
{
LanguageVersion = CppSharp.Parser.LanguageVersion.CPP17,
Verbose = true,
MicrosoftMode = true,
};
options.SetupMSVC(VisualStudioVersion.VS2019);
Target: MSVC
Stack trace or incompilable generated code
Compiler argument: -fms-extensions
Compiler argument: -fms-compatibility
Compiler argument: -fdelayed-template-parsing
Target triple: x86_64-pc-windows-msvc
#include "..." search starts here:
#include <...> search starts here:
// My private include paths
D:\nuget\packages\Microsoft.Gsl.2.0.0\build\native\include
lib\clang\12.0.0\include
C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC\Tools\MSVC\14.25.28610\include
C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC\Tools\MSVC\14.25.28610\atlmfc\include
C:\Program Files (x86)\Windows Kits\10\Include\10.0.18362.0\ucrt
C:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\shared
C:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\um
C:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\winrt
End of search list.
C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC\Tools\MSVC\14.25.28610\include\cstdint(21,1): error: unknown type name 'namespace'
C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC\Tools\MSVC\14.25.28610\include\cstdint(21,1): error: expected ';' after top level declarator
C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC\Tools\MSVC\14.25.28610\include\cstdlib(23,62): error: expected function body after function declarator
C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC\Tools\MSVC\14.25.28610\include\cstdio(33,1): error: unknown type name 'namespace'
C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC\Tools\MSVC\14.25.28610\include\cstdio(33,1): error: expected ';' after top level declarator
C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC\Tools\MSVC\14.25.28610\include\cwchar(22,1): error: unknown type name 'using'
C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC\Tools\MSVC\14.25.28610\include\cwchar(22,19): error: unexpected type name 'mbstate_t': expected expression
C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC\Tools\MSVC\14.25.28610\include\cwchar(24,1): error: unknown type name 'namespace'
C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC\Tools\MSVC\14.25.28610\include\cwchar(24,1): error: expected ';' after top level declarator
C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC\Tools\MSVC\14.25.28610\include\initializer_list(20,1): error: unknown type name 'namespace'
C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC\Tools\MSVC\14.25.28610\include\initializer_list(20,1): error: expected ';' after top level declarator
C:\Program Files (x86)\Windows Kits\10\Include\10.0.18362.0\ucrt\malloc.h(67,48): error: unknown type name '_HEAPINFO'
It seems like for some reasons the parser is in C mode, not being able to recognize C++ keywords.
Is that all the verbose output?
Do you have some "Compiler arguments" lines (from https://github.com/mono/CppSharp/blob/master/src/CppParser/Parser.cpp#L255) and if so, can you post the full log?
I've updated my previous post with the log messages
Compiler argument: -fms-extensions
Compiler argument: -fms-compatibility
Compiler argument: -fdelayed-template-parsing
Target triple: x86_64-pc-windows-msvc
#include "..." search starts here:
#include <...> search starts here:
-- edit: You are right, the parser is in C mode, but I have no idea why
I've added the options.AddArguments("-std=c++17"); line before setup and the error appeared: error: invalid argument '-std=c++17' not allowed with 'C'
As far as I can see:
in https://github.com/mono/CppSharp/blob/0.11/src/Parser/ParserOptions.cs#L147 there is no setting of -xc or -xc++ like in https://github.com/mono/CppSharp/blob/0.11/src/Parser/ParserOptions.cs#L327
As a result:
Adding the
options.AddArguments("-std=c++17"); resolves the issue of "namespace" even the error: invalid argument '-std=c++17' not allowed with 'C' exists.
Adding the options.AddArguments("-xc++"); libe before adding the -std=c++17 option makes the output like:
Compiler argument: -xc++
Compiler argument: -std=c++17
Compiler argument: -fms-extensions
Compiler argument: -fms-compatibility
Compiler argument: -fdelayed-template-parsing
Target triple: x86_64-pc-windows-msvc
#include "..." search starts here:
#include <...> search starts here:
// My private includes
D:\nuget\packages\Microsoft.Gsl.2.0.0\build\native\include
lib\clang\12.0.0\include
C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC\Tools\MSVC\14.25.28610\include
C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC\Tools\MSVC\14.25.28610\atlmfc\include
C:\Program Files (x86)\Windows Kits\10\Include\10.0.18362.0\ucrt
C:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\shared
C:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\um
C:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\winrt
End of search list.
with following errors from kind of broken Microsoft Gsl (there are some conditions for clang and msvc, but since clang acts here as a msvc then the code is not so correct)
D:\nuget\packages\Microsoft.Gsl.2.0.0\build\native\include\gsl/gsl_util(65,5): error: use of undeclared identifier 'f'
D:\nuget\packages\Microsoft.Gsl.2.0.0\build\native\include\gsl/gsl_util(91,1): error: use of undeclared identifier 'type'
D:\nuget\packages\Microsoft.Gsl.2.0.0\build\native\include\gsl/gsl_util(112,1): error: use of undeclared identifier 'type'
D:\nuget\packages\Microsoft.Gsl.2.0.0\build\native\include\gsl/gsl_util(113,1): error: use of undeclared identifier 'f'
D:\nuget\packages\Microsoft.Gsl.2.0.0\build\native\include\gsl/gsl_util(127,1): error: use of undeclared identifier 'bounds'
D:\nuget\packages\Microsoft.Gsl.2.0.0\build\native\include\gsl/gsl_util(128,1): error: use of undeclared identifier 'bounds'
D:\nuget\packages\Microsoft.Gsl.2.0.0\build\native\include\gsl/gsl_util(136,1): error: use of undeclared identifier 'bounds'
D:\nuget\packages\Microsoft.Gsl.2.0.0\build\native\include\gsl/gsl_util(137,1): error: use of undeclared identifier 'bounds'
D:\nuget\packages\Microsoft.Gsl.2.0.0\build\native\include\gsl/gsl_util(146,1): error: use of undeclared identifier 'bounds'
D:\nuget\packages\Microsoft.Gsl.2.0.0\build\native\include\gsl/span(154,9): error: use of undeclared identifier 'bounds'
D:\nuget\packages\Microsoft.Gsl.2.0.0\build\native\include\gsl/span(275,9): error: use of undeclared identifier 'bounds'
D:\nuget\packages\Microsoft.Gsl.2.0.0\build\native\include\gsl/span(290,9): error: use of undeclared identifier 'con'; did you mean 'cos'?
C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC\Tools\MSVC\14.25.28610\include\cmath(618,1): note: 'cos' declared here