Skip to content

CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS does not work for "external return decl" ignored on Windows (MSVC) #1585

@qingfengxia

Description

@qingfengxia
Brief Description

I had fairly big C++ project to wrap into C#, it works mostly on Linux with dotnet 3.1, but not on Windows
https://bitbucket.org/fathomteam/moab/src/master/

Because the target c++ headers do not use __declspec(dllexport) at all, and there are too much work to add them.
I wonder whether CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS could be a solution, if not why.

Instead of CppSharp user manual's solution

If you're exposing C++ functions on Windows, you'll have to add the __declspec(dllexport) directive, otherwise the symbols won't be found when calling them from the managed world. You could also add the directive to a class directly, like this:

class __declspec(dllexport) ExposedClass
{
  // class definition
}

a quick solution by CMake is tried

    if(MSVC)
        set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS TRUE)
    endif()

Symbols are exported into dll, but got lots of warning, almost half API are not generated. E.g.

CppSharp warning:

Function 'get_coords' was ignored due to external return decl

The target C++ API virtual ErrorCode get_coords( const Range& entity_handles, double* coords ) const;

Actually, the return type enum has been correctly generated as
public enum ErrorCode: uint on Linux, public enum ErrorCode on Windows

===
Related CppSharp code

// Generator/Passes/CheckIgnoreDecls.cs
private bool HasInvalidType(Type type, Declaration decl, out string msg)
{
    // ... skipped 
    var module = decl.TranslationUnit.Module;
    if (Options.DoAllModulesHaveLibraries() &&
        module != Options.SystemModule && ASTUtils.IsTypeExternal(module, type))
    {
        msg = "external";
        return true;
    }
}
   
// ASTUtils.IsTypeExternal
public static bool IsTypeExternal(Module module, Type type)
{
    var typeModule = type.GetModule();
    return typeModule != null && typeModule.Dependencies.Contains(module);
}

a hack to disable/skip this return type check failed with assertion

                if (HasInvalidType(ret.Type, function, out msg) && false) // tmp diasable this
                {
                    function.ExplicitlyIgnore();
                    Diagnostics.Debug("Function '{0}' was ignored due to {1} return decl type {2}",
                        function.Name, msg, ret);
                    return false;
                }

Vtable assertion in ClassLayout.cs failed Debug.Assert(Kind == VTableComponentKind.FunctionPointer);

OS: Windows

Used headers

https://bitbucket.org/fathomteam/moab/src/master/src/moab/Core.hpp
https://bitbucket.org/fathomteam/moab/src/master/src/moab/Types.hpp

Used settings

Target: MSVC/

Other settings

Stack trace or incompilable generated code

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions