Thread: how to compile C function inside C-Sharp program?

  1. #1
    Registered User
    Join Date
    Aug 2011

    how to compile C function inside C-Sharp program?


    I do not know how to code in C Sharp. I want to add a C function to an existing, working C Sharp program, and compile it. Let's say the C function is something like this:

    #include "stdio.h" 
    #include "stdlib.h" 
    #include "string.h"
    Test_Function ( )
      /* C codes go here. */
    What do I need to do to make this C Sharp program compile with the above test C function added? I tried adding standard C libraries at the top of the C Sharp program, but it won't compile.

    Please help. Thank you.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Oregon, USA
    You can't. You can compile your C function into a .dll and then call the function from inside C#, although you're probably better off just learning C#. What's the function you're trying to implement?
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Aug 2011
    Thanks for your response. I have only a week to get this done. Learning to write in C-Sharp at this moment is not practical.
    You mentioned about compiling into a .dll and then calling it from C#. Is this the easiest solution? If so, how do you go about doing this?

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Portland, OR
    Just make a normal DLL, export the functions you want to call from C++, then use the [DllImport] attribute to bring the functions into scope in C#.

    See this article: .NET Column: Calling Win32 DLLs in C# with P/Invoke
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);

  5. #5
    Registered User
    Join Date
    Jun 2005
    If learning to write in C# is not practical, I would suggest compiling code into a DLL and interfacing to it from C# is also not particularly practical.

    Compiling into a DLL is not necessarily the easiest solution, as it requires particular techniques in C# to access the DLL function, but it is the most practical solution if you don't want to rewrite C code as C#. It does rely on some knowledge of C# though.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How do you keep sharp?
    By Epy in forum General Discussions
    Replies: 13
    Last Post: 01-12-2011, 05:33 PM
  2. Replies: 9
    Last Post: 08-06-2009, 07:20 AM
  3. Compiling C sharp program
    By pokhara in forum C# Programming
    Replies: 4
    Last Post: 07-29-2009, 01:44 PM
  4. Replies: 7
    Last Post: 04-11-2009, 09:44 AM
  5. I am new to C sharp
    By t3chn0n3rd in forum C# Programming
    Replies: 2
    Last Post: 03-19-2008, 06:08 PM