Complie and link C++ code in C

This is a discussion on Complie and link C++ code in C within the C++ Programming forums, part of the General Programming Boards category; Hello people, it's been a long since last time I worked with C and C++. Is it possible to link ...

  1. #1
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712

    Complie and link C++ code in C

    Hello people,
    it's been a long since last time I worked with C and C++. Is it possible to link Cpp code in C?
    What I mean is this:
    In basics, it comes to this...

    I have two files:
    Test_cpp.h
    Code:
    int test_cpp(int);
    Test_cpp.cpp
    Code:
    int test_cpp(int x)
    {
        return x+2;
    }
    and main.c /*in other C project in which Test_cpp.h and Test_cpp.cpp are included*/
    Code:
    #include <stdio.h>
    #include "Test_CPP.h"
    
    int main(int argc, char *argv[])
    {
      printf("&#37;d", test_cpp(4));
      return 0;
    }
    Is it possible to use extern keyword to make this work?

    Thanks
    Last edited by Micko; 02-26-2008 at 11:00 AM.
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  2. #2
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630
    Of course.. Shouldnt be a problem..

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712
    Hmm, I'm still having problems:
    I'm using Dev-Cpp in which I have created C project with the following files:

    Header Test_Cpp.h:
    Code:
    #ifndef  TEST_CPP_H
    #define TEST_CPP_H
    extern "C" int test_cpp(int);
    #endif
    Implementation file Test_Cpp.cpp:
    Code:
    #include "Test_Cpp.h"
    int test_cpp(int x)
    {
        return x+2;
    }
    Main.c:
    Code:
    #include <stdio.h>
    #include "Test_Cpp.h"
    
    int main()
    {
      printf("&#37;d", test_cpp(4));
      return 0;
    }
    I'm getting this error on second line:
    3 C:\Dev-Cpp\C_Cpp_Dev\Test_Cpp.h syntax error before string constant

    What is the possible reason for this? It's like compiler doesn't recognize extern keyword...

    P.S. Test_Cpp.cpp is already compiled with C++ compiler.
    Last edited by Micko; 02-26-2008 at 02:06 PM.
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    A C compiler doesn't know what "extern "C"" means. If you want C++ code to be called from a C program, you have to compile Test_Cpp.cpp with
    Code:
    extern "C" int test_cpp(int x)
    so that the C++ compiler knows to mangle the name differently for export. But your C header file would just have the normal prototype.

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    http://www.parashift.com/c++-faq-lit....html#faq-32.1
    You must use your C++ compiler when compiling main() (e.g., for static initialization)
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    32.4 is more interesting i think
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  8. #8
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,160
    Quote Originally Posted by tabstop View Post
    A C compiler doesn't know what "extern "C"" means. If you want C++ code to be called from a C program, you have to compile Test_Cpp.cpp with
    Code:
    extern "C" int test_cpp(int x)
    so that the C++ compiler knows to mangle the name differently for export. But your C header file would just have the normal prototype.
    But that causes the function to be a true-blue C function, meaning overloading doesn't work any more. You can't just go around in a C++ library declaring things extern "C". You have to write a small wrapper, in C++, which transfers control from C to C++. The wrapper is declared extern "C" so the C code can call it, but because it is a C++ function, it is able to call other C++ functions.

    In this case, instead of making test_cpp(int x) an extern "C", I'd write a wrapper function:

    Code:
    /* In some header common to the C and C++ code... */
    extern "C" int test_cpp_C(int x);
    /* In Test_Cpp.cpp */
    int test_cpp_C(int x)
    {
        // Make sure exceptions can't get up into the C code
        try
        {
            return test_cpp(x);
        }
        catch(...) {}
    }
    Now the C code calls test_cpp_C() and that function transfers control to the C++ function. If test_cpp() is overloaded, you need to write a wrapper for each one.

    If the C++ throws an exception, you have to communicate that to the C code in a way it can understand, but that's a whole other problem.

  9. #9
    Registered User jian2587's Avatar
    Join Date
    Feb 2008
    Location
    NY
    Posts
    11
    question:
    if I want to call C code from C++, apart from adding extern declarations in the C++ code, what else do I do? I gather it's this.
    1. Compile C code with C compiler, creates object code A.
    2. Compile C++ code with C++ compiler, creates object code B.
    3. Use C++ linker to link A and B. <-- how do I do this?
    Is this correct?

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by jian2587 View Post
    question:
    if I want to call C code from C++, apart from adding extern declarations in the C++ code, what else do I do? I gather it's this.
    1. Compile C code with C compiler, creates object code A.
    2. Compile C++ code with C++ compiler, creates object code B.
    3. Use C++ linker to link A and B. <-- how do I do this?
    Is this correct?
    As a concept, that's what you do.

    As to how you do step 3, usually it's simple, just . E.g. in gcc/g++ you do:
    Code:
    $ gcc -c -Wall A.c -o A.o
    $ g++ -c -Wall B.cpp -o B.o
    $ g++ A.o B.o -o myapp
    Other compilers obvious call themeselves different things, and use slightly different flagnames, but otherwise the principle is the same.

    You can of course merge step 2 & 3 in this case by doing:
    Code:
    $ g++ A.o -Wall B.cpp -o myapp
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

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