I would like to reference a non-Module external (extern
) function from within a C Module. Unfortunately using the normal method pre-Modules doesn't appear to work:
extern uint8_t* test(uint8_t*);
Produces this issue in Visual Studio 2022:
error LNK2019: unresolved external symbol "unsigned char * __cdecl test(unsigned char *)" (?test@@YAPEAEPEAE@Z::<!Module>)'
Obviously I want it to look for ?test@@YAPEAEPEAE@Z
instead of ?test@@YAPEAEPEAE@Z::<!Module>
.
It appears Modules here name mangles differently. How do I convince the linker to look for a regular non-Module function from inside a Module?
CodePudding user response:
This has nothing to do with extern
specifically.
If you declare a function inside of a module unit (unless it is in the global module fragment), then you're declaring that the function exists within the purview of that module. All declarations of such a function (including the definition of the function) must also be within the purview of that module. A module cannot export something to its interface which it does not own (unless it is owned by some other module which it export import
s).
If you merely want to use that function within a module unit, then you should put that declaration in the global module fragment of the file.
CodePudding user response:
It is possible to directly declare such a function with
extern "C " uint8_t* test(uint8_t*);
That said, this is mostly a curiosity and doesn’t work for functions declared by other (named) modules. The standard solution is to #include
(in the global module fragment) or import
the declaration from elsewhere.