Re: Using function from .cpp in .ispc

242 views
Skip to first unread message

Matt Pharr

unread,
Apr 17, 2013, 11:22:21 AM4/17/13
to ispc-...@googlegroups.com
The trick is that functions called from ispc should have "C" linkage, and not have C++ name mangling applied.

So, if you add this line to the start of a.cpp:

extern "C" { int foo(); }

Then foo() will be C-callable and everything should work as expected.

Thanks,
Matt




On Tue, Apr 16, 2013 at 4:02 PM, Martin Šošić <sosic....@gmail.com> wrote:
Hi everybody, I have a problem trying to use function from a.cpp in b.ispc.
I read from guide about using extern for this but I keep getting errors.
I did following:
a.cpp:
    int foo() {return 1};
    int main() {
        ispc::simple();
    }

b.ispc:
    extern int foo();
    export void simple() {
        print("%d\n", foo());
    }

This is the error message i get:
simple_ispc.o: In function `simple___':
simple.ispc:(.text+0x939): undefined reference to `foo___'
simple_ispc.o: In function `simple':
simple.ispc:(.text+0x979): undefined reference to `foo___'
collect2: error: ld returned 1 exit status

What am I doing wrong, maybe this is not possible? If not what do you suggest, what are other options?
Thank you

--
You received this message because you are subscribed to the Google Groups "Intel SPMD Program Compiler Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ispc-users+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Dmitry Babokin

unread,
Apr 17, 2013, 11:42:14 AM4/17/13
to ispc-...@googlegroups.com
Martin,

Here's an example:

> cat a.cpp 
#include "b.h"

extern "C" int foo() {return 1;}
int main() {
    ispc::simple();
}
> cat b.cpp 
extern "C" uniform int foo();
export void simple() {
    print("%\n", foo());
}
> ispc b.cpp --target=sse4 -o b.o -h b.h
> g++ -c a.cpp
> g++ a.o b.o
> a.out 
1

Also note that formatting of print string is slightly different from printf strings: http://ispc.github.io/ispc.html#output-functions 

Martin Šošić

unread,
Apr 18, 2013, 7:03:49 AM4/18/13
to ispc-...@googlegroups.com
Thank you both, that works!
Do you maybe have any advice how to export functions that are private methods of class? I guess that is not possible?

Dmitry Babokin

unread,
Apr 18, 2013, 7:17:20 AM4/18/13
to ispc-...@googlegroups.com
I'm not sure that you can have "extern "C"" linkage for any class member (regardless it's private or public). So you have to have C-style interface to interract with ispc code.

Matt Pharr

unread,
Apr 18, 2013, 11:29:28 AM4/18/13
to ispc-...@googlegroups.com
You could do this with a forwarding function--e.g.

class Foo {
private:
  void bar();
  friend void FooBar(Foo *);
};

extern "C" void FooBar(Foo *f) {
  f->bar();
}

and then you can call FooBar() with a Foo * uniform pointer from ispc...

Thanks,
Matt
Reply all
Reply to author
Forward
0 new messages