RSS Forums RSS

Calling C++ function from C code

Please support our C advertiser: Programming Forums
Reply
Posts: 11
Reputation: venomxxl is an unknown quantity at this point 
Solved Threads: 0
venomxxl's Avatar
venomxxl venomxxl is offline Offline
Newbie Poster

Calling C++ function from C code

  #1  
Jan 5th, 2009
Hello,
I'm quite new here, but there's a problem that needs solution. I think the title says exactly what I want to do. I need to use C for a reason I don't think is this important to mention. I tried searching Google, and found the same solution on most of the results.
After some searching I made a test application using solutions I found... but I can't get it working, I get a linker error.
If it's important: I'm using GCC 4.2.4 on a Linux.

Now some code...

main.c
#include <stdio.h>
#include "wrp.h"

int main(){
    printf("wrapper\n");
    wrp();
}

wrp.h
#ifdef __cplusplus
extern "C"
#endif
void wrp();

cppcode.cpp
#include <iostream>
#include "wrp.h"

extern "C" void wrp(){
     std::cout << "wrapper zakonczyl dzialanie!\n";
}

Linker error I get:
/tmp/cc6Irctr.o: In function `main':
main.c:(.text+0x1e): undefined reference to `wrp'
collect2: ld returned 1 exit status

Command I use: gcc main.c -o test

Thanks in advance for Your help.
-Marek

PS. I don't know if this thread belongs to C or C++ forum.
Last edited by venomxxl : Jan 5th, 2009 at 11:47 am.
AddThis Social Bookmark Button
Reply With Quote  
Posts: 5,125
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 633
Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Calling C++ function from C code

  #2  
Jan 5th, 2009
g++ -c cppcode.cpp
gcc -c main.c
g++ -o test main.o cppcode.o
Oh, and that might not work either.

This is how to mix C and C++
http://www.parashift.com/c++-faq-lit...c-and-cpp.html
In particular, the main() needs to be C++ so that global ctors/dtors get called.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
UK Voter? Please send a message to Incapability Brown and the rest of Zanu-Labour
Up to 8Mb PlusNet broadband from only £5.99 a month!
Reply With Quote  
Posts: 11
Reputation: venomxxl is an unknown quantity at this point 
Solved Threads: 0
venomxxl's Avatar
venomxxl venomxxl is offline Offline
Newbie Poster

Re: Calling C++ function from C code

  #3  
Jan 5th, 2009
It worked! I can't believe it was so simple

Output:
wrapper
wrapper zakonczyl dzialanie!

Thanks a lot!
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.



Views: 568 | Replies: 2 | Currently Viewing: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 3:17 pm.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC