Problem calling C++ from C code

This is a discussion on Problem calling C++ from C code within the C Programming forums, part of the General Programming Boards category; Hi, I have been understanding how to call C/C++ code from C++/C. Firstly I tried calling a C function from ...

  1. #1
    Registered User poornaMoksha's Avatar
    Join Date
    Sep 2011
    Location
    India
    Posts
    40

    Problem calling C++ from C code

    Hi,

    I have been understanding how to call C/C++ code from C++/C. Firstly I tried calling a C function from C++ code :

    Code:
    // cpp code
    #include<iostream>
    
    extern "C" {
    void func();
    }
    
    int main(void)
    {
        func();
        return 0;
    }
    I compiled the above code using the following command :

    g++ -c main.cpp

    Now, here is the C code :
    Code:
    #include<stdio.h>
    
    void func()
    {
        printf("\n Inside func()\n");
    }
    Compiled the above code using :
    gcc -c func.c

    Finally, I linked the C and c++ code using :

    g++ main.o func.o -o out

    Now, when I execute 'out', I could see the output 'Inside func()'.


    As a second step, I defined a function 'fun()' in cpp file and tried to call it from C code..

    Code:
    //C++ code
    #include<iostream>
    #include<stdio.h>
    
    void fun();
    
    extern "C" {
    void func();
    }
    
    void fun()
    {
        printf("\n inside fun()\n");
    }
    
    int main(void)
    {
        func();
        return 0;
    }
    Code:
    // C code
    #include<stdio.h>
    
    extern void fun();
    
    void func()
    {
        printf("\n Inside func()\n");
        fun();
    }
    Now when I run the following command :

    g++ -Wall main.o func.o -o out

    I get the following error :
    Code:
    ~/practice $ g++ main.o func.o -o func
    func.o: In function `func':
    func.c:(.text+0x14): undefined reference to `fun'
    collect2: ld returned 1 exit status
    I am not able to understand the reason????

  2. #2
    Registered User poornaMoksha's Avatar
    Join Date
    Sep 2011
    Location
    India
    Posts
    40
    I got it working by doing some modifications :

    Code:
    //cpp code
    #include<iostream>
    #include<stdio.h>
    
    
    extern "C" {
    void func();
    }
    
    extern "C" void fun()
    {
        printf("\n inside fun()\n");
    }
    
    int main(void)
    {
        func();
        return 0;
    }

  3. #3
    and the hat of mystery Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    31,433
    It might work for now, but it isn't a long term solution.
    [32] How to mix C and C++ ..Updated!.., C++ FAQ
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. code for calling the .NET framework version
    By Nyoud33 in forum C# Programming
    Replies: 1
    Last Post: 01-12-2009, 05:53 AM
  2. Code help, calling a set of equations...
    By Invertalon in forum C Programming
    Replies: 0
    Last Post: 03-18-2008, 05:58 PM
  3. Error code 1813 after calling HttpOpenRequest
    By jmd15 in forum Networking/Device Communication
    Replies: 1
    Last Post: 09-10-2005, 10:37 AM
  4. Calling Manged code from Unmanged code.
    By subdene in forum C++ Programming
    Replies: 0
    Last Post: 02-11-2005, 08:59 AM
  5. calling functions & assembly code
    By Micko in forum C++ Programming
    Replies: 1
    Last Post: 02-25-2004, 02:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21