none
Underscore prefix on extern "C" link

    Question

  • From a C++ project I am trying to link to a third party DLL whose interface is defined in this sort of format:

    extern "C"
    {
        int SomeRoutine(int SomeParam);
    }

    In my C++ project I have a call along the lines of:     int x = SomeRoutine(42);

    At link time I get an "unresolved external symbol _SomeRoutine" error.  Note the leading underscore.  Where did that come from and how do I prevent it?

    Regards
    Keith


    Monday, September 15, 2008 10:03 AM

Answers

  • The underscore is normal, what you are missing is a __declspec(dllexport) in your export.

    extern "C"
    {
        __declspec(dllexport) int SomeRoutine(int SomeParam);
    }

    then when using it you need to use __declspec(dllimport)

    so I usually have a define that switches between the two depending on whether the DLL is being built or used.
    • Marked as answer by Yan-Fei Wei Wednesday, September 17, 2008 6:21 AM
    Monday, September 15, 2008 11:35 AM
  • If it's definitely exported (you can see it using the dependency walker tool or dumpbin -exports)  then I suspect that you aren't linking in the lib file in your project settings.  The underscore should make no difference to your link.  If you call the function and have it declared with extern "C" then it should find it.  Try this, in one of your source or header files put the following (where libname.lib is the name (with full path if necessary) of the library you're linking to)

    #pragma comment(lib, "libname.lib")

    • Marked as answer by Yan-Fei Wei Wednesday, September 17, 2008 6:21 AM
    Monday, September 15, 2008 7:40 PM

All replies

  • The underscore is normal, what you are missing is a __declspec(dllexport) in your export.

    extern "C"
    {
        __declspec(dllexport) int SomeRoutine(int SomeParam);
    }

    then when using it you need to use __declspec(dllimport)

    so I usually have a define that switches between the two depending on whether the DLL is being built or used.
    • Marked as answer by Yan-Fei Wei Wednesday, September 17, 2008 6:21 AM
    Monday, September 15, 2008 11:35 AM
  • >>The underscore is normal, what you are missing is a __declspec(dllexport) in your export.
    >>
    >>extern "C"
    >>{
    >>    __declspec(dllexport) int SomeRoutine(int SomeParam);
    >>}

    Thanks Ted

    I understand what you are saying but still have a minor practical problem.  The dll isn't mine, it's a third party product.  OK, the commercial arrangements are such that I do have the source so I _could_ change and rebuild it from source.  I'd rather avoid doing that if at all possible.

    The DLL is from a reputable supplier (Oracle, no less - it's the Berkeley database) so I find it hard to believe they have made such a basic mistake in their released package.  Looking at the dll, the functions I require are exported without a leading underscore.  Is there anything I can do to make my C++ component link to them without having to rebuild the dll?

    Keith

     
    Monday, September 15, 2008 4:05 PM
  • If it's definitely exported (you can see it using the dependency walker tool or dumpbin -exports)  then I suspect that you aren't linking in the lib file in your project settings.  The underscore should make no difference to your link.  If you call the function and have it declared with extern "C" then it should find it.  Try this, in one of your source or header files put the following (where libname.lib is the name (with full path if necessary) of the library you're linking to)

    #pragma comment(lib, "libname.lib")

    • Marked as answer by Yan-Fei Wei Wednesday, September 17, 2008 6:21 AM
    Monday, September 15, 2008 7:40 PM
  • >>The underscore is normal, what you are missing is a __declspec(dllexport) in your export.
    >>
    >>extern "C"
    >>{
    >>    __declspec(dllexport) int SomeRoutine(int SomeParam);
    >>}

    Thanks Ted

    I understand what you are saying but still have a minor practical problem.  The dll isn't mine, it's a third party product.  OK, the commercial arrangements are such that I do have the source so I _could_ change and rebuild it from source.  I'd rather avoid doing that if at all possible.

    The DLL is from a reputable supplier (Oracle, no less - it's the Berkeley database) so I find it hard to believe they have made such a basic mistake in their released package.  Looking at the dll, the functions I require are exported without a leading underscore.  Is there anything I can do to make my C++ component link to them without having to rebuild the dll?

    Keith

     

    Hi Keith, I'm runnin on the same problem, How did you finally fix it?
    Amar por amar es agua que no conocen los hombres.
    Monday, October 18, 2010 2:06 PM
  • OK, I found that 'extern "C"' was used only on my importing module, and it wasn't declared that way on the exporting module, because of a conditional precompiler condition, so I use the same on both modules and that fixed it.
    Amar por amar es agua que no conocen los hombres.
    Monday, October 18, 2010 2:39 PM