473,385 Members | 1,474 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

calling C++ functions in C

Hi, is anybody could help me to point out the problem:
My problem is I want to call C++ function in C.
My code is as follows. Here it gives In function `main':
: undefined reference to `cpp_function'
I am using GNU Compiler in LINUX.

------------------------------------------------------------------
Expand|Select|Wrap|Line Numbers
  1.  //cpp_file.cpp
  2.  
  3.  #include "cpp_file.h"
  4.  #include <iostream>
  5.  void cpp_function(){
  6.    foo f;
  7.  
  8.    f.see();
  9.    cout<<"Hi, I am in Cpp file and called by C file\n"<<flush;
  10.  }
------------------------------------------------------------------
Expand|Select|Wrap|Line Numbers
  1. //cpp_file.h
  2.  
  3.  #include <iostream.h>
  4.   class foo {
  5.  public:
  6.    int i;
  7.  
  8.    foo() {i=0; }
  9.    ~foo() {}
  10.  
  11.    void see() {
  12.      cout << "--------------I am inside class ------------" << endl;
  13.    }
  14.  
  15.  };
  16.  
  17.  void cpp_function();
------------------------------------------------------------------
Expand|Select|Wrap|Line Numbers
  1. //c_file.h
  2.  
  3. #ifdef __cplusplus
  4. extern "C" {
  5. #endif
  6.  
  7. void cpp_function( void );
  8.  
  9. #ifdef __cplusplus
  10. }
  11. #endif
------------------------------------------------------------------
Expand|Select|Wrap|Line Numbers
  1. //c_file.c
  2.  #include "c_file.h"
  3.  int main(){
  4.    cpp_function();
  5.    return(0);
  6.  }
------------------------------------------------------------------
//Makefile

Expand|Select|Wrap|Line Numbers
  1. exe: c_file.o cpp_file.o
  2.                g++ -o exe c_file.o cpp_file.o c_file.h cpp_file.h
  3.        c_file.o: c_file.c c_file.h
  4.                g++ -c c_file.c
  5.        cpp_file.o: cpp_file.cpp cpp_file.h
  6.                g++ -c cpp_file.cpp
  7.  
  8.        clean:
  9.                rm *.o
Nov 1 '07 #1
4 1582
Banfa
9,065 Expert Mod 8TB
Your mistake is in declaring void cpp_function(); in cpp_file.h.

c_file.h has the correct declaration of void cpp_function(); what you should do is include c_file.h into ccp_file.cpp.



I function should only ever be declared once (and defined once too). If you have declared it more than once (as in you code example) it is almost certainly and error and very likely to cause you problems now or in the future.

Expand|Select|Wrap|Line Numbers
  1. #ifdef __cplusplus
  2. extern "C" {
  3. #endif
  4.  
  5. #ifdef __cplusplus
  6. }
  7. #endif
Bascially states that everything between the { } should use C caling convention. C++ calling convention is different, to allow for function overloading (and is commonly refered to as name mangling). By using the different defintions you told the compiler that the function used C calling convention when you compiled the C file and that it used C++ calling convention when it compiled the C++ file.

These 2 naming conventions produce different names so when the linker runs it can not find the function the code in the C file is requires and you get a link error.
Nov 1 '07 #2
dex
4
You need to wrap both the declaration (prototype) and the definition of your cpp_function in an extern "C" { }
Nov 1 '07 #3
dex
4
Banfa is also quite correct. The declaration of cpp_function in cpp_file.h should be in it's own header file and included in the c_file.c
Nov 1 '07 #4
Banfa
9,065 Expert Mod 8TB
You need to wrap both the declaration (prototype) and the definition of your cpp_function in an extern "C" { }
this isn't right, only the declaration needs to be wrapped.
Nov 2 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: pieter.breed | last post by:
Hi All, Is it possible to export a c# method into a dll in such a way that your "normal" C application can then call this method? To be clear: I am not asking how to use "DllImport" or...
19
by: Ross A. Finlayson | last post by:
Hi, I hope you can help me understand the varargs facility. Say I am programming in ISO C including stdarg.h and I declare a function as so: void log_printf(const char* logfilename, const...
5
by: Dave | last post by:
does calling a regular function cost any cpu time? In other words, is it faster to write the code of two functions into main(), or is it the exact same thing as calling two functions. I know its...
1
by: Mark Jerde | last post by:
Yesterday I posted the message below to microsoft.public.dotnet.languages.vb and microsoft.public.vc.language. The two replies are also posted. I need to write some ISO C++ functions, more...
1
by: Jesse McGrew | last post by:
Hi all, I'm trying to make a plugin DLL for a third-party application, using VC++ .NET 2003. This DLL acts as a bridge between the C++ plugin API of the application, and my actual plugin code...
1
by: H.B. | last post by:
Hi, I need to make a function that can display data on my Managed C++ app and be called by an unmanaged C++ DLL. Something like : void Form1::Form1_Load(System::Object * sender,...
2
by: Daniel Lidström | last post by:
I'm using a library called fyba. This library reads and writes files in a format called sosi. fyba uses the following code to determine if the calling process has own methods to handle errors,...
18
by: John Friedland | last post by:
My problem: I need to call (from C code) an arbitrary C library function, but I don't know until runtime what the function name is, how many parameters are required, and what the parameters are. I...
4
by: Edwin Gomez | last post by:
I'm a C# developer and I'm new to Python. I would like to know if the concept of Asynchronous call-backs exists in Python. Basically what I mean is that I dispatch a thread and when the thread...
10
by: sulekhasweety | last post by:
Hi, the following is the definition for calling convention ,which I have seen in a text book, can anyone give a more detailed explanation in terms of ANSI - C "the requirements that a...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.