Marc Tajchman <marc.tajch...@cea.fr> wrote on 09/14/2009 05:33:15 AM:
> Hi, > > I'm trying to call a C++ function from a X10 class, using the following > code. > It's working as expected (note the commented line in Hello.x10). > > //============= Hello.x10 ======================================= > public class Hello { > > @Native("c++", "HelloWorld()") > extern public native def test():void; > > public static def main(a: Rail[String]) : void > { > var H0:Hello = new Hello(); > H0.test(); > > for(var j:int =0; j<Place.MAX_PLACES; j++) > async(here.next(j)) { > var H:Hello = new Hello(); > Console.OUT.println("Hello from place " + here.id > + " ( of "+Place.MAX_PLACES+" )"); > > // H.test(); <<=== commented line > } > } > } > //============= end of Hello.x10 ================================ > > > //============= Hello_x10stub.c ================================= > #include <iostream> > > extern "C" > void HelloWorld() > { > std::cout << "Hello world from C" << std::endl; > } > //============= end of Hello_x10stub.c =========================== > > To compile the code, I type : > > x10c++ -commandlineonly -c Hello.x10 > g++ -c -I/opt/x10/include -I/opt/x10 -I. -g -DTRANSPORT=sockets > -DX10_USE_BDWGC -Wno-long-long -Wno-unused-parameter -U___DUMMY___ > -msse2 -mfpmath=sse Hello.cc > g++ -g -pthread -msse2 -mfpmath=sse Hello.o -L/opt/x10/lib -lgc -lx10 > -lupcrts_sockets -ldl -lm -lpthread -Wl,-export-dynamic -lrt -o Hello.out > > > Now, if I uncomment H.test() in Hello, the first g++ command fails with > the error : > Hello.inc : ‘HelloWorld’ was not declared in this scope > > I think that the problem comes from the generated Hello.cc file. > This file starts with: > > #include <Hello.h> > #include "Hello.inc" > #include <Hello_x10stub.c> > > It would be better to generate: > > #include <Hello.h> > #include <Hello_x10stub.c> > #include "Hello.inc" > > Thanks for comments or suggestions Hi, Marc, The "extern" mechanism currently does not work (and is being phased out). FWIW, you're also not using it correctly. However, since you are using @Native, you don't need to make the method). "extern". If you change your code to use "native" instead of "extern",). the stub file will not be generated, but you will get an invocation of,). HelloWorld() generated in the C++ output file. The other problem is to get the appropriate declaration into the generated code. Unfortunately, there is no way to declare an extern "C". function inline in C++. The following, however, should work: import x10.compiler.Native; public class Hello { @Native("c++", "void HelloWorld();\nHelloWorld()") public native def test():void; ... } Then define HelloWorld in Hello_cppimpl.cc as: #include <iostream> void HelloWorld() { std::cout << "Hello world from C" << std::endl; } (note the absense of extern "C") and compile with: x10c++ -commandlineonly -post 'g++ # Hello_cppimpl.cc #' Hello.x10 Note that the above trick will only work for a void function. Also, if your class is in some named package, you'd need to define HelloWorld() in the appropriate namespace (e.g., for package x.y, the namespace is "x::y"). In the above, I assume you are working with release 1.7.6 of X10, not SVN HEAD. Igor -- Igor Peshansky (note the spelling change!) IBM T.J. Watson Research Center XJ: No More Pain for XML's Gain (http://www.research.ibm.com/xj/) X10: Parallel Productivity and Performance (http://x10.sf.net/) ------------------------------------------------------------------------------ Come build with us! The BlackBerry® Developer Conference in SF, CA is the only developer event you need to attend this year. Jumpstart your developing skills, take BlackBerry mobile applications to market and stay ahead of the curve. Join us from November 9-12, 2009. Register now! http://p.sf.net/sfu/devconf _______________________________________________ X10-users mailing list X10-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/x10-users