Skip to content

"Unresolved symbols remain" when using in a C++ project #19

@natevw

Description

@natevw

As I started using this library in a C++ project I got some surprising error messages at first about unresolved symbols. Turns out that the compiler assumed the Cifra functions were C++ and would be name-mangled accordingly, but of course the implementation files are all C instead and so it wasn't finding the functions under the right names.

It would be convenient if this project added the usual #ifdef __cplusplus boilerplate to each header meant to be part of the public API. See https://isocpp.org/wiki/faq/mixing-c-and-cpp#include-c-hdrs-personal for some discussion.

Meanwhile the workaround isn't so bad but may not be obvious at first. Instead of e.g.

#include <lib/cifra/src/sha2.h>

wrap the #include itself in the block when including anything from cifra, which in the end does the same thing as if they were in the headers:

extern "C" {
    #include <lib/cifra/src/sha2.h>
}

Note this simple wrapping assumes a header file used only from C++. For a header file that itself saw mixed usage it would need the full boilerplate:

#ifdef __cplusplus
  extern "C" {
#endif
    #include <lib/cifra/src/sha2.h>
#ifdef __cplusplus
  }
#endif

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions