LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-03-2012, 07:19 AM   #1
Samuel_Minirobots
LQ Newbie
 
Registered: Oct 2012
Posts: 2

Rep: Reputation: Disabled
Example of calling C++ library from C# code?


Do you know an example of how to call a C++ library function (for example, print "hello world") from C# code in Linux?

I know how to create a shared library, but all the attempts to call it from C# (with DllImport) were unsuccessful. Is it necessary to compile the shared library in an special way?

Thank you.
 
Old 10-03-2012, 07:33 AM   #2
414N
Member
 
Registered: Sep 2011
Location: Italy
Distribution: Slackware
Posts: 640

Rep: Reputation: 186Reputation: 186
How are you using DllImport? Are there any relevant error messages?
 
Old 10-03-2012, 07:41 AM   #3
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 15,627

Rep: Reputation: 5123Reputation: 5123Reputation: 5123Reputation: 5123Reputation: 5123Reputation: 5123Reputation: 5123Reputation: 5123Reputation: 5123Reputation: 5123Reputation: 5123
have you tried this: http://stackoverflow.com/questions/9...e-from-c-sharp ?
or this:
http://www.moythreads.com/wordpress/...call-c-from-c/

Last edited by pan64; 10-03-2012 at 07:42 AM.
 
1 members found this post helpful.
Old 10-03-2012, 10:35 AM   #4
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 9,078
Blog Entries: 4

Rep: Reputation: 3182Reputation: 3182Reputation: 3182Reputation: 3182Reputation: 3182Reputation: 3182Reputation: 3182Reputation: 3182Reputation: 3182Reputation: 3182Reputation: 3182
There are four major considerations:

(a) The so-called "calling convention" must be the same. In addition, the parameters must be compatible and correctly-described.

(b) C++ uses "name mangling" to encode the parameter-list into the linker-supplied name string. Both parties must do this in the same way.

(c) The runtime environments must be compatible. If one party allocates an object, that party must also dispose of it. The correct behavior of called objects must be tested thoroughly, especially if they involve dynamic memory allocation.

(d) The appropriate declarations must be used: the serving party generally must be told to export the function (and how), and the client party must also have a compatible external declaration. At link-time or runtime, the two parties must actually be linked-together and in the correct manner.
 
1 members found this post helpful.
Old 10-05-2012, 03:28 AM   #5
Samuel_Minirobots
LQ Newbie
 
Registered: Oct 2012
Posts: 2

Original Poster
Rep: Reputation: Disabled
Thank you very much for the responses.

It worked doing the following:

In lib_mylibrary.cpp, define the functions with extern "C":
extern "C" bool InitializeUsb()

In main.cs, import the functions with:
[DllImport("./lib_mylibrary.so", EntryPoint="InitializeUsb", CharSet=CharSet.Auto)]
public static extern bool InitializeUsb();

This library uses libusb, so that in order to compile it and run it:
g++ -shared -o lib_mylibrary.so lib_mylibrary.cpp -I/usr/local/include/libusb-1.0 -lusb-1.0
mcs main.cs
mono main.exe
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Why the thread stack size can not be changed when calling in a dynamic library? xhx321 Programming 10 10-09-2012 02:38 AM
problem calling fftw3 library function while using gfortran compiler. Please help. buddyamit Programming 5 07-08-2011 01:07 AM
Calling an external library from an other thread. When it is safe? sylvaticus Programming 2 12-04-2009 03:11 AM
Calling Fortran 95 code in C srunni Programming 2 04-15-2009 09:15 AM
Bug in c code calling bash code Linh Programming 11 08-12-2003 05:27 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:16 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration