Like Tree1Likes
  • 1 Post By grumpy

how to compile C function inside C-Sharp program?

This is a discussion on how to compile C function inside C-Sharp program? within the C# Programming forums, part of the General Programming Boards category; Hi, I do not know how to code in C Sharp. I want to add a C function to an ...

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    6

    how to compile C function inside C-Sharp program?

    Hi,

    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:

    Code:
    #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
    Location
    Oregon, USA
    Posts
    3,163
    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
    Posts
    6
    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
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    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
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,255
    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.
    iMalc likes this.
    Right 98% of the time, and don't care about the other 3%.

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, 04: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21