PGI User Forum
 SearchSearch   MemberlistMemberlist     RegisterRegister   ProfileProfile    Log inLog in 

Free OpenACC Webinar

simple question about cross linking

 
Post new topic   Reply to topic    PGI User Forum Forum Index -> Programming and Compiling
View previous topic :: View next topic  
Author Message
970980hs



Joined: 01 Jan 2014
Posts: 10

PostPosted: Tue Mar 11, 2014 12:06 pm    Post subject: simple question about cross linking Reply with quote

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?
Back to top
View user's profile
mkcolg



Joined: 30 Jun 2004
Posts: 6215
Location: The Portland Group Inc.

PostPosted: Wed Mar 12, 2014 10:17 am    Post subject: Reply with quote

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.

Code:
% 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
Back to top
View user's profile
Display posts from previous:   
Post new topic   Reply to topic    PGI User Forum Forum Index -> Programming and Compiling All times are GMT - 7 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © phpBB Group