Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. C vs. C++ (VS2010)

C vs. C++ (VS2010)

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++visual-studiohelp
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    vesf
    wrote on last edited by
    #1

    I hope I chose an appropriate forum. Why does the test code below build in VS2010 as C code but not as C++ code (linker errors further below). And what's needed to make it compile as C++ code. Thanks. - Vince

    #include

    __declspec(dllimport) int __stdcall SetEnvironmentVariableA(const char *pName, const char *pValue);
    __declspec(dllimport) unsigned long __stdcall GetEnvironmentVariableA(const char *pName, char *pValue, unsigned long nSize);

    int main ( int argc, char **argv )
    {
    char szValue[32768];
    SetEnvironmentVariableA(argv[1], argv[2]);
    GetEnvironmentVariableA(argv[1], szValue, 32768);
    return 0;
    }

    error LNK2001: unresolved external symbol "__declspec(dllimport) unsigned long __stdcall GetEnvironmentVariableA(char const *,char *,unsigned long)" (__imp_?GetEnvironmentVariableA@@YGKPBDPADK@Z)
    error LNK2001: unresolved external symbol "__declspec(dllimport) int __stdcall SetEnvironmentVariableA(char const *,char const *)" (__imp_?SetEnvironmentVariableA@@YGHPBD0@Z)

    V M 2 Replies Last reply
    0
    • V vesf

      I hope I chose an appropriate forum. Why does the test code below build in VS2010 as C code but not as C++ code (linker errors further below). And what's needed to make it compile as C++ code. Thanks. - Vince

      #include

      __declspec(dllimport) int __stdcall SetEnvironmentVariableA(const char *pName, const char *pValue);
      __declspec(dllimport) unsigned long __stdcall GetEnvironmentVariableA(const char *pName, char *pValue, unsigned long nSize);

      int main ( int argc, char **argv )
      {
      char szValue[32768];
      SetEnvironmentVariableA(argv[1], argv[2]);
      GetEnvironmentVariableA(argv[1], szValue, 32768);
      return 0;
      }

      error LNK2001: unresolved external symbol "__declspec(dllimport) unsigned long __stdcall GetEnvironmentVariableA(char const *,char *,unsigned long)" (__imp_?GetEnvironmentVariableA@@YGKPBDPADK@Z)
      error LNK2001: unresolved external symbol "__declspec(dllimport) int __stdcall SetEnvironmentVariableA(char const *,char const *)" (__imp_?SetEnvironmentVariableA@@YGHPBD0@Z)

      V Offline
      V Offline
      vesf
      wrote on last edited by
      #2

      Got it! 'extern "C"' needed. I missed that when tracking down the defines and typedefs in WinBase.h.

      1 Reply Last reply
      0
      • V vesf

        I hope I chose an appropriate forum. Why does the test code below build in VS2010 as C code but not as C++ code (linker errors further below). And what's needed to make it compile as C++ code. Thanks. - Vince

        #include

        __declspec(dllimport) int __stdcall SetEnvironmentVariableA(const char *pName, const char *pValue);
        __declspec(dllimport) unsigned long __stdcall GetEnvironmentVariableA(const char *pName, char *pValue, unsigned long nSize);

        int main ( int argc, char **argv )
        {
        char szValue[32768];
        SetEnvironmentVariableA(argv[1], argv[2]);
        GetEnvironmentVariableA(argv[1], szValue, 32768);
        return 0;
        }

        error LNK2001: unresolved external symbol "__declspec(dllimport) unsigned long __stdcall GetEnvironmentVariableA(char const *,char *,unsigned long)" (__imp_?GetEnvironmentVariableA@@YGKPBDPADK@Z)
        error LNK2001: unresolved external symbol "__declspec(dllimport) int __stdcall SetEnvironmentVariableA(char const *,char const *)" (__imp_?SetEnvironmentVariableA@@YGHPBD0@Z)

        M Offline
        M Offline
        Mauro Leggieri
        wrote on last edited by
        #3

        Put declarations inside

        extern "C" { }

        C & C++ uses different name mangling.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups