Pergunta

I have slight problem or misunderstanding with hiding symbols in xcode 4.6. I have searched everywhere in the web and cant find any posts having the same issue.

Currently I have created a framework project with a simple header file containing so:

class Test
{
public:

    Test(){}
    Test(){}
};

int a(int n) {return n;}

__attribute__((visibility("hidden"))) int b(int n) {return n;}

__attribute__((visibility("default"))) int c(int n) {return n;}

class X
{
public:
    virtual ~X();
};

class __attribute__((visibility("hidden"))) Y
{
public:
    virtual ~Y();
};

class __attribute__((visibility("default"))) Z
{
public:
    virtual ~Z();
};

X::~X() { }
Y::~Y() { }
Z::~Z() { }

In the project settings i have made sure that "Symbols hidden by default" is switched to YES and therefore only the functions int c and the class z will be exported or visible to other projects.

I build the framework with no errors or warnings and then copy/add it to a cocoa application for testing.

I am able to include the header file but I am still able to access all classes and functions.

I hope someone can explain why or where i am going wrong or have encountered this problem before?

regards

Foi útil?

Solução

Your problem is, you are declaring everything in the header file. That means even if the symbols are not exported, when you import the header file into another file, these symbols are re-created in the project where you imported the header and thus they are of course available within this project.

I'm trying to explain this to you by a simpler sample. Assume you have two files, MyLib.h and MyLib.c.

MyLib.h:

int add(int a, int b);

MyLib.c:

int add(int a, int b) { return a + b };

If you now give add a hidden visibility and compile everything to a library (MyLib), the library will have no symbol for add (as it is hidden). As a result, if you include MyLib.h into another file, make a call to the add function there and finally link this file against MyLib, you will get a linker error, since the linker will complain that it cannot find an implementation for add (it only has a declaration from the H file).

However, if you pack the function into the header itself, so MyLib.h is:

int add(int a, int b) { return a + b };

and you give add a hidden visibility, the library will also have no symbol for add, but when you include the header into another file, this header will create a symbol for add in exactly the file that imported it and thus of course you will not get a linker error and be able to call add.

Importing a H file just means copying the whole content of the H file to the location where the import instruction has been found. Now think about it: If you copy the whole content of the H file in your question to the top of the file where you are including it, of course all symbols declared there are visible and usable in the current project. Why wouldn't they be visible or useable? They are not in the library, yes, but they are in the header and thus do get compiled when your other project is compiled.

Symbols that are supposed to not be visible outside your current project ("hidden" symbols) should never appear in any public header file of your framework. Why would you announce the existence of symbols in a header files that cannot be used anyway? Actually, in most cases not exposing the symbols in the header file is already enough to prevent people from using them. The reason for not exporting them either is only that if a symbol exists, it can be used, even if it is not exposed by any header file (therefor you just need to write your own header file or declaring it as external symbol directly in your code). So making symbols hidden is just a safeguard to make sure that it cannot be used under any circumstances.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top