mixed c and c++

This is a discussion on mixed c and c++ within the C++ Programming forums, part of the General Programming Boards category; I have to mix c-sources and c++-sources in a linux project. How can I do it? I had tried to ...

  1. #1
    Registered User
    Join Date
    Jan 2013
    Location
    Europe
    Posts
    2

    mixed c and c++

    I have to mix c-sources and c++-sources in a linux project.
    How can I do it?
    I had tried to compile them seperatly. The c-files with the gcc and the cpp files with g++.
    After that I link all files with g++ together. By that it doesn't find the calls coming from c++-sources to c-functions.
    Any ideas?

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,172
    Depends, to some extent, on the C.

    If all you're looking to do is call some C functions from C++, provide a header file that declares all the C functions you wish to call from C++, qualified as extern "C", and #include that in all of your C++ source files that need to call the C functions. Then you can compile the C files using gcc and the C++ files using g++. You will need to link using the g++ driver, not the gcc driver.

    An alternative approach is to compile all of the c files using g++, and then link (using the g++ driver, just as you would if linking object files compiled as C++). This is likely to work since a large proportion of C is a subset of C++. There are some exceptions (eg C99 features like variable-length arrays are not part of C++) though. Note also that C++ compilers are pickier than C compilers on some things.

    Note that, if you intend to call C++ functions from C code, or make use of C++ classes in C, you cannot compile as C at all, and will need to compile as C++ (any code that uses a C++ feature must be treated as C++ code, even if everything else in the source file is C).
    Right 98% of the time, and don't care about the other 3%.

  3. #3
    Registered User
    Join Date
    Jan 2013
    Location
    Europe
    Posts
    2
    Thanks a lot,
    I just surround the header-includes of the c-headers with
    Code:
     extern "C" {}
    and it works. Thanks a lot

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,422
    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. Mixed Language: C and Asm
    By Eman in forum C Programming
    Replies: 47
    Last Post: 01-03-2011, 01:38 PM
  2. mixed program c/fortran
    By mimi1981 in forum C Programming
    Replies: 39
    Last Post: 08-21-2008, 07:31 AM
  3. mixed success with ShellExecuteEx
    By Gerread in forum Windows Programming
    Replies: 3
    Last Post: 11-27-2007, 02:52 AM
  4. mixed content class, how to
    By TriKri in forum C++ Programming
    Replies: 7
    Last Post: 09-16-2007, 11:03 AM

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