开发者

Hiding C++ symbols in iOS static library?

I'v开发者_C百科e made an iOS code library in pure C++, which I intend to publish to a lot of people. As far as I know, at the time of writing, the only way to share compiled code on iOS is through a static library.

Unfortunately, there are some clever bits in the code which I'd like to hide from public view. What I'd like to do is only expose the symbols for a single (simple) interface class, and hide everything else. Is this possible?

Any other ideas on how to hide my symbols are also very welcome. Thanks for your time!


Solved it! I ended up with first of all including all the source in a single cpp. Secondly I put all non-external stuff in an unnamed namespace. Then to get rid of the remaining symbols I added this script to the build process:

echo MyGlobalFunctionToInstantiateMainClass > "${BUILT_PRODUCTS_DIR}/save_symbols"
strip -s "${BUILT_PRODUCTS_DIR}/save_symbols" -u "${BUILT_PRODUCTS_DIR}/lib${PRODUCT_NAME}.a"

Which strips all global symbols except MyGlobalFunctionToInstantiateMainClass.


It depends on what you consider to be "public view".

If you have the code spread over multiple source files, then you are largely out of luck if you want to hide the internal symbols for the linker. A static library is really an archive of individual object files, so they have all to be processed by the linker when linking the library to an application.
If the library is heavily template based, then you are out of luck as well. The compiler for the final program has to be able to see all the relevant templates, so there is no way you can hide them.
If the library consists of a single source file, you can use an anonymous namespace to give all the internal symbols such an unutterable name that they are not usable for all practical purposes.

If you are afraid that people will learn too much from the names of internal symbols, then either don't use such descriptive names or run the code through an obfuscator before building a release.


I've found that compiling all the source files as a single compilation unit by including them in a composite .cpp, and putting everything but one public class into an unnamed namespace hides most of the symbols.

However, inline and static method names still appear in the static library. Is there any way to prevent this?


Best way to resolve duplicate symbol or hiding is to obfuscate c/c++ sources.


We had the same issue and solved it without having to combine all our code into a single source file, see the answer I wrote here for a related question: https://stackoverflow.com/a/16492468/1086196

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜