2

I've been using a technique for embedding resources (raw binary data) into compiled executables, using a combination of C, C++ and CMake.

The code/configuration I've used is very similar to what's available in this GitHub repository, and I've even reproduced the issue with this particular sample.

Used as is, the resource embedding works great, but as soon as C++ namespacing is introduced, things start breaking left and right.

The issue seems to be that the C-style resources are by default using the global/anonymous namespace when called from within C++, but if they're called from a namespace inside C++, then that namespace gets prefixed to the resource, which results in undefined symbols.

So far I've tried all I can think of, including converting the original C-style code to C++ and using namespaces with it, which yields the same results.

More details on the specific calls/code can be seen in this GitHub issue.

Ps. I realize that there's hardly any code/context in this post, but I figured it would make more sense to link to the original topics/articles that go through this, instead of trying to explain it myself. Also, if it helps, this topic has also been discussed here and even on StackOverflow here.

too honest for this site
  • 12,050
  • 4
  • 30
  • 52
Dids
  • 569
  • 7
  • 24
  • 1
    I did a similar thing with shaders while teaching my son some techniques. Take a look in the `shaders` directory of this project. https://github.com/AlexAndDad/dungeon – Richard Hodges Apr 11 '18 at 12:57
  • Do you expect everyone is familar with this tool ("embed-resource")? Looking into [its code](https://github.com/cyrilcode/embed-resource/blob/master/embedresource.cpp) I see simple `char sym_name[] = { bytes ... };` definition. Not sure how namespaces are related with it. And how CMake is related with all this problem? Yes, the tool uses CMake, but the problem isn't CMake-related, isn't it? Some code examples would definitely improve the question. – Tsyvarev Apr 11 '18 at 13:02
  • That's apparently not a C, but pure C++ problem. – too honest for this site Apr 11 '18 at 16:14
  • @Olaf: From my understanding, the question is about "porting" `C` approach to the `C++`. Or even having approach suitable for both `C` and `C++`. So removed `C` tag was meaningful in some extent. – Tsyvarev Apr 11 '18 at 17:01
  • @Tsyvarev:; It is definitively not. There are no custom name spaces in C not are the required symbol features like name-mangling supported by C. Just using C ABI functions does not justify adding the C tag. Feel free to provide proof I'm wrong, provide the required information **in tthe question** and post a [mcve]. – too honest for this site Apr 11 '18 at 17:12
  • @Olaf: This is not my question, so I can only *guess*. OK, lets wait clarification from the author. – Tsyvarev Apr 11 '18 at 17:21

1 Answers1

2

That extern "C" in C headers included from C++ code is exactly for calling C functions/objects from C++ without namespace (or better: for calling function/objects not by using C++ mangled name, but using a C name).
This embded-rsource tool is cool, just add extern "C" in LOAD_RESOURCE like this:

 #define LOAD_RESOURCE(RESOURCE) ([]() {                      \
    extern "C" const char _resource_##RESOURCE[]; extern "C" const size_t _resource_##RESOURCE##_len;   \
    return Resource(_resource_##RESOURCE, _resource_##RESOURCE##_len);  \
})()

That way you instruct compiler, that __resource##RESOURCE is not a C++ symbol, but a C symbol compiled with a C compiler.

KamilCuk
  • 120,984
  • 8
  • 59
  • 111