-1

I have the C++ project in which i added some pure C gnu library. I was tasked to change some of the functions, that is used there for the ones, that are needed to be loaded dynamically from dll, so what i did is, i declared the prototype, and made an extern variable:

file prototypes.h:

typedef char *( __cdecl * _ReParseHtml ) ( DOM * data )

file func_defs.h

extern _ReParseHtml DllReParseHtml;

then in my, main file program.cpp i declared:

_ReParseHtml DllReParseHtml;

int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
    _In_opt_ HINSTANCE hPrevInstance,
    _In_ LPWSTR    lpCmdLine,
    _In_ int       nCmdShow)
{
    HINSTANCE hDLL = LoadLibrary("somelib.dll");
    if(hDll != 0){
         DllReParseHtml = (_ReParseHtml)GetProcAddress(hDLL,
                                       "ReParseHtml");
         //etc
    }
}

So now i want to use this in the pure .c file which i added: parser.c:

#include "func_defs.h"  // here is the extern _ReParseHtml DllReParseHtml;

 int create_source(char * buf)
 {
      char * modified = DllReParseHtml(buf);
      // etc..
 }

But i get the error LNK2001 unresolved external symbol DllReParseHtml.

The function in purce C source int create_source(char * buf) from which this functions suppose to be called is not in the extern "C" { } block, or anything like that.

Vlad
  • 369
  • 4
  • 16
  • 1
    Needs, more, commas, to, be, readable. – EOF Aug 17 '16 at 11:57
  • Possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – too honest for this site Aug 17 '16 at 12:01
  • You probably need to add `extern "C"` in your program.cpp, because there you *will* need it. Also you could move `_ReParseHtml DllReParseHtml;` to your parser.c rather than your program.cpp – Elijan9 Aug 17 '16 at 12:01
  • @Elijan9 i need to use it elsewhere on the project in a few other C files. Thats why i declared like that – Vlad Aug 17 '16 at 12:05
  • @KlasLindbäck its not the dll. I just have the `c++` project in which i use some pure C code. In that pure C code i need to access the global variables which is declared in my project. In my post, the global variable is the function that is dynamically loaded from the dll in `WinMain`. – Vlad Aug 17 '16 at 12:09
  • Did you include the header in your C++ file? If you didn't, the symbol you defined there is a distinct symbol, with internal linkage. – molbdnilo Aug 17 '16 at 13:33
  • The header with the `extern _ReParseHtml DllReParseHtml;` ? – Vlad Aug 17 '16 at 13:33

2 Answers2

0

If I understand you, I do not think your problem comes from the mixing of C and C ++ languages. In fact, a global variable must be declared in a single file without the keyword extern and any other files with that keyword. Do not change anything in your file "func_def.h" but make sure the typedef is recognized. Take all the same care with C ++ makes a mangle. Place the beginning of the file directive

#ifdef __cplusplus
    extern "C" {
#endif

and at end of file directive

#ifdef __cplusplus
    }
#endif

In one of your .c files, use the directive

define extern

This will effectively remove the keyword in a single file.

Regards: Bernard.

Community
  • 1
  • 1
BTA
  • 1
  • 1
0

I solved this problem, by making all the prototype function declarations as in a block "extern "C" { }" . I had to modify the C code, to the c++ standart, removing some constructions, but at least it helped

Vlad
  • 369
  • 4
  • 16