how to use a c++ function in c code, also how to link two languages?

This is a discussion on how to use a c++ function in c code, also how to link two languages? within the C++ Programming forums, part of the General Programming Boards category; Could anybody help me in solving the problem of using c++ function/class in C code. Given below is my code ...

  1. #1
    Registered User
    Join Date
    Apr 2013
    Posts
    1

    how to use a c++ function in c code, also how to link two languages?

    Could anybody help me in solving the problem of using c++ function/class in C code.
    Given below is my code
    Code:
    //C code
    #include <stdio.h>
    //#include <code_cpp.cc>
    extern int value(int, char**);
    int main(int argc, char* argv[])
    {
      //main(int);
      //int c;
      return value(argc, argv);
      int a = 1;
      printf("value of a is %d \n", a);
      return 0;
    }
    Code:
    //C++ code
    #include<iostream>
    #ifdef __cplusplus
    extern "C" int value()
    {
    #endif
      int b = 2;
      return b;
    #ifdef __cplusplus
     } // extern "C"
     #endif
    
    extern "C" int main ()
    {
      //std::cout << "Hello, world!\n";
      int main_b;
      main_b = value();
      std::cout<<"value of main_b is "<<main_b<<std::endl;
      return main_b;
    }
    On compiling c code I got the following error
    Code:
    gcc -Wall code_c.c -o code_c
    /tmp/cctXTHYe.o: In function `main':
    code_c.c:(.text+0x17): undefined reference to `value'
    collect2: ld returned 1 exit status
    I think there is some linking problem.

    Thanks in advance!

  2. #2
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    907
    Look at your preprocessor commands. You exclude the whole function. Instead, you want to exclude the extern only.
    Code:
    #ifdef __cpluplus
    extern "C"
    {
    #endif
    
    int value()
    {
        // ...
    }
    
    #ifdef __cplusplus
    }
    #endif
    There are two sets of brackets here: those that include the extern "C" and those that include the function body. Equivalently, you could have conditionally excluded only the symbols extern "C" .
    Code:
    #ifdef __cplusplus
    extern "C"
    #endif
    
    int value()
    {
        // ...
    }
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,687
    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.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    2,510
    in your C++ file you're declaring it to take no parameters

    Quote Originally Posted by vermaric View Post
    Code:
    extern "C" int value()
    and then in your C code, you're declaring it to take two parameters. that is also a problem. it is likely to cause all sorts of undefined behavior.

    Quote Originally Posted by vermaric View Post
    Code:
    extern int value(int, char**);

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Actually, in C if you declare a function prototype's argument list as (), this means "I don't know and I don't care what the argument list is", which is different from saying (void), which means "This function takes no parameters". And in C's case the code will still compile and link either way, since name-mangling doesn't occur (the symbol name will be the same regardless of which arguments the function takes).

    So the worst that will happen is you get undefined arguments in the function. Which is pretty bad, really. But you probably won't get errors about it, just warnings, which is another reason to pay attention to warnings.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    Apr 2013
    Posts
    1,314
    Quote Originally Posted by dwks View Post
    Actually, in C if you declare a function prototype's argument list as (), this means "I don't know and I don't care what the argument list is", which is different from saying (void), which means "This function takes no parameters". And in C's case the code will still compile and link either way, since name-mangling doesn't occur (the symbol name will be the same regardless of which arguments the function takes).
    Depending on the compiler and the call options (how many input parameters are register based), you get name mangling in C as well (usually the name is suffixed by @xx, where xx is the number of parameter bytes pushed onto the stack). I sometimes combine assembly and C, and to get the names, I have the compiler generate assembly code for the C code to get the function names.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 12-23-2012, 03:52 AM
  2. How to link c++ code with Ms Access
    By Mustanser Iqbal in forum C++ Programming
    Replies: 2
    Last Post: 11-30-2012, 01:17 PM
  3. Complie and link C++ code in C
    By Micko in forum C++ Programming
    Replies: 9
    Last Post: 02-27-2008, 06:38 AM
  4. how to integrate code in different languages?
    By nefysno1 in forum C Programming
    Replies: 3
    Last Post: 06-09-2006, 01:30 AM
  5. what languages are fading ? what languages remain ?
    By dot_rain in forum Tech Board
    Replies: 32
    Last Post: 03-04-2004, 08:25 AM

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