Calling C++ function from C code

Reply

Join Date: Jan 2009
Posts: 16
Reputation: venomxxl is an unknown quantity at this point 
Solved Threads: 2
venomxxl's Avatar
venomxxl venomxxl is offline Offline
Newbie Poster

Calling C++ function from C code

 
0
  #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
  1. #include <stdio.h>
  2. #include "wrp.h"
  3.  
  4. int main(){
  5. printf("wrapper\n");
  6. wrp();
  7. }

wrp.h
  1. #ifdef __cplusplus
  2. extern "C"
  3. #endif
  4. void wrp();

cppcode.cpp
  1. #include <iostream>
  2. #include "wrp.h"
  3.  
  4. extern "C" void wrp(){
  5. std::cout << "wrapper zakonczyl dzialanie!\n";
  6. }

Linker error I get:
  1. /tmp/cc6Irctr.o: In function `main':
  2. main.c:(.text+0x1e): undefined reference to `wrp'
  3. 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 12:47 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
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: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Calling C++ function from C code

 
0
  #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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 16
Reputation: venomxxl is an unknown quantity at this point 
Solved Threads: 2
venomxxl's Avatar
venomxxl venomxxl is offline Offline
Newbie Poster

Re: Calling C++ function from C code

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

Output:
  1. wrapper
  2. wrapper zakonczyl dzialanie!

Thanks a lot!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC