Type visibilty for a header Share a header file sh

2020-05-06 10:17发布

问题:

I have a header file that is included by both a native cpp file and a managed cpp file(compiled with /clr). It includes only native types, but I want to specify that the native types are visible outside the assembly
(see http://msdn.microsoft.com/en-us/library/4dffacbw(VS.80).aspx).

Essentially, I want:

public class NativeClass  // The public makes this visible outside the assembly.
{

};

If I include this code from a native cpp, I get the following error:

error C3381: 'NativeClass' : assembly access specifiers are only available in code compiled with a /clr option

Attempted solution:

I'm currently using a preprocessor solution that causes the public to appear when compiling with the managed client, but it does not appear for the native client:

#ifdef __cplusplus_cli
#define CLR_ASSEMBLY_ACCESS_SPECIFIER__Public public
#else
#define CLR_ASSEMBLY_ACCESS_SPECIFIER__Public 
#endif 

CLR_ASSEMBLY_ACCESS_SPECIFIER__Public
class NativeClass      
{

};

Question:

Is this the appropriate way to achieve this, or is there a better way?

回答1:

Have you tried the make_public pragma listed on the MSDN page you linked to?

Otherwise, the solution you have is perfectly valid. I'm curious to know why you want to export native types from a CLR assembly though.