simple question about cross linking

Hi~

I’m asking about cross linking with g++ objective file and pgcc objective file.
The sample is too simple.

main.cpp :
#include <fun.h>
int main(){
fun(1,2);
}

fun.c:
void fun(int a, int b){
int c;
c=a+b;
}

fun.h:
#include <stdio.h>
void fun(int,int);

Now, first, I compiled main.cpp like as follows:
g++ -c main.cpp
Second, I compiled fun.c:
pgcc -acc -ta=nvidia -c fun.c:
After that, I tried to link together by g++
g++ -o aaa main.o fun.o

But, there are error
main.cpp(.text+0xf) : undefined reference to ‘fun(int,int)’
collect2:ld returned 1 exit status

These files are compiled well by using g++ alone.
How do I solve that kinds of problem?

Hi 970980hs,

C++ uses mangled symbol names, so you need to tell the C++ compiler that “fun” has a C style symbol name by using extern “C”. Note that this is true for any C++ that supports and C compiler, not just g++ and pgcc.

% cat fun.h
#include <stdio.h>
#ifdef __cplusplus
extern "C" {
#endif
void fun(int,int);
#ifdef __cplusplus
}
#endif
% pgcc -c fun.c
% g++ main.cpp fun.o -I./
%

Hope this helps,
Mat