| | |
Calling C++ function from C code
![]() |
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
wrp.h
cppcode.cpp
Linker error I get:
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.
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
C Syntax (Toggle Plain Text)
#include <stdio.h> #include "wrp.h" int main(){ printf("wrapper\n"); wrp(); }
wrp.h
C Syntax (Toggle Plain Text)
#ifdef __cplusplus extern "C" #endif void wrp();
cppcode.cpp
C Syntax (Toggle Plain Text)
#include <iostream> #include "wrp.h" extern "C" void wrp(){ std::cout << "wrapper zakonczyl dzialanie!\n"; }
Linker error I get:
C Syntax (Toggle Plain Text)
/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 12:47 pm.
g++ -c cppcode.cpp gcc -c main.c g++ -o test main.o cppcode.o
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.
It worked! I can't believe it was so simple
Output:
Thanks a lot!
Output:
C Syntax (Toggle Plain Text)
wrapper wrapper zakonczyl dzialanie!
Thanks a lot!
![]() |
Similar Threads
- Getting error when calling function from main() (C++)
- calling function name? i need help (PHP)
- passing objects as function parameters (C++)
- Function not returning value (JavaScript / DHTML / AJAX)
- problem with calling a function (C++)
- calling a function without changing scope (Python)
- Calling C# function(code behind) from javascript function (ASP.NET)
- Helping with calling a function (C)
- Calling c function from python (Python)
- Still need info on calling a function from bool! (C++)
Other Threads in the C Forum
- Previous Thread: any one have idea about compiler control directives
- Next Thread: Unhandled exception and Access violation for every program
| Thread Tools | Search this Thread |
* ansi api array arrays binarysearch calculate centimeter changingto char character convert copyanyfile copypdffile creafecopyofanytypeoffileinc createcopyoffile createprocess() csyntax directory dynamic fflush file floatingpointvalidation forloop frequency function getlasterror getlogicaldrivestrin givemetehcodez grade graphics gtkgcurlcompiling gtkwinlinux highest histogram homework i/o inches include infiniteloop input intmain() iso keyboard km linked linkedlist linux linuxsegmentationfault list logical_drives looping loopinsideloop. lowest match matrix microsoft mqqueue mysql oddnumber odf open opendocumentformat openwebfoundation pdf posix power program programming pyramidusingturboccodes radix read recursion recv recvblocked repetition reversing scanf scheduling segmentationfault send shape single socketprogramming stack standard strchr string suggestions test threads turboc unix urboc user variable whythiscodecausesegmentationfault win32api windows.h windowsapi






