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. Calling C++ functions from C

Calling C++ functions from C

Scheduled Pinned Locked Moved C / C++ / MFC
c++question
5 Posts 3 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.
  • S Offline
    S Offline
    Stormwind
    wrote on last edited by
    #1

    I am creating an internal app framework (for company apps). Using C++ will facilitate my task but I have to make the framework / library usable for C programmers. Any ideas?

    T 1 Reply Last reply
    0
    • S Stormwind

      I am creating an internal app framework (for company apps). Using C++ will facilitate my task but I have to make the framework / library usable for C programmers. Any ideas?

      T Offline
      T Offline
      Tibor Blazko
      wrote on last edited by
      #2

      use extern "C" into fn prototypes (of course they can't be class members) t! .h: #ifdef __cplusplus extern "C" { #endif /* __cplusplus */ int my_fn(void); #ifdef __cplusplus } #endif /* __cplusplus */

      S 1 Reply Last reply
      0
      • T Tibor Blazko

        use extern "C" into fn prototypes (of course they can't be class members) t! .h: #ifdef __cplusplus extern "C" { #endif /* __cplusplus */ int my_fn(void); #ifdef __cplusplus } #endif /* __cplusplus */

        S Offline
        S Offline
        Stormwind
        wrote on last edited by
        #3

        Thats it! Thanks. I thought I had to specify function call argument passing (__cdecl, __fastcall, etc) just as Dr John Maddock's Regex++ library.

        M 1 Reply Last reply
        0
        • S Stormwind

          Thats it! Thanks. I thought I had to specify function call argument passing (__cdecl, __fastcall, etc) just as Dr John Maddock's Regex++ library.

          M Offline
          M Offline
          Mike Dimmick
          wrote on last edited by
          #4

          It's often a good idea to be specific about the calling convention when exporting from a DLL; if the client program is compiled with a different default convention (using the /Gd, /Gr or /Gz switches) from your DLL it either won't link correctly or will potentially crash at runtime, due to erroneous stack manipulations. __stdcall is marginally smaller (typically one ADD instruction) at the call site than __cdecl, but __cdecl can handle variable argument lists and is marginally smaller in the function implementation. The ADD instruction is used to reset the stack pointer after the call back to where it was before the compiler started PUSHing arguments. More info in John Robbins' excellent Debugging Applications books (I just bought the most recent one, Debugging Applications for Microsoft .NET and Microsoft Windows - there's a mouthful). This only applies to desktop Win32 running on IA32 processors; Windows CE on IA32 always uses __cdecl, while other processors only have a single calling convention. Most RISC processors use a calling convention sort of like __cdecl but with the first n arguments passed in registers. This is also true of AMD64 (first 4 arguments passed in RCX, RDX, R8, R9 or XMM0-3 if floating point) and IA64 (first 8 arguments passed in the rotating portion of the register file). [Yes, I know the processor names look a little unfamiliar; specifying 'x86' isn't very helpful any more because 64-bit 'x86-64' code works differently. AMD now wish Opteron/Athlon64-compatible code to be known as AMD64.] -- Mike Dimmick

          S 1 Reply Last reply
          0
          • M Mike Dimmick

            It's often a good idea to be specific about the calling convention when exporting from a DLL; if the client program is compiled with a different default convention (using the /Gd, /Gr or /Gz switches) from your DLL it either won't link correctly or will potentially crash at runtime, due to erroneous stack manipulations. __stdcall is marginally smaller (typically one ADD instruction) at the call site than __cdecl, but __cdecl can handle variable argument lists and is marginally smaller in the function implementation. The ADD instruction is used to reset the stack pointer after the call back to where it was before the compiler started PUSHing arguments. More info in John Robbins' excellent Debugging Applications books (I just bought the most recent one, Debugging Applications for Microsoft .NET and Microsoft Windows - there's a mouthful). This only applies to desktop Win32 running on IA32 processors; Windows CE on IA32 always uses __cdecl, while other processors only have a single calling convention. Most RISC processors use a calling convention sort of like __cdecl but with the first n arguments passed in registers. This is also true of AMD64 (first 4 arguments passed in RCX, RDX, R8, R9 or XMM0-3 if floating point) and IA64 (first 8 arguments passed in the rotating portion of the register file). [Yes, I know the processor names look a little unfamiliar; specifying 'x86' isn't very helpful any more because 64-bit 'x86-64' code works differently. AMD now wish Opteron/Athlon64-compatible code to be known as AMD64.] -- Mike Dimmick

            S Offline
            S Offline
            Stormwind
            wrote on last edited by
            #5

            Thanks for the clarification mike.

            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