Thread: Combining C and C++ code together (Linux)

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    11

    Combining C and C++ code together (Linux)

    Hi,

    I am wondering if it is possible to compile C code in a C++ program? What I am trying to do is to build a parser created from ANTLR V3 with as C being the target language. I am wondering if it is possible for me to combine ANTLR's C code with my C++ program?

    I tried looking for a similar C and C++ combined code compilation thread but cannot seem to find on here -- hence this new topic.

    Thanks in advanced!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If the generated C code doesn't use any of the C++ keywords (like class), and doesn't use VLAs, then you can have the compiler pretend it's C++ code (either by renaming the extension or by setting compiler flags) and everybody will be happy. If you want to compile it separately actually in C, then you'll have to do (a bit) more work.

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    11
    Thanks for the quick response.

    Well I am compiling this entirely through g++ compiler ... Someone told me that I can only mix C/C++ code in Windows programming only -- although I am starting to believe this is a myth.

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    You can -llink to an object (compiled from "C" code with gcc) from "C++" code compiling with g++, but you may encounter problems revolving around the use of void*. AFAICT pretty much the same rules apply to including a source file during compilation but they may be a little (more)strict.
    Last edited by MK27; 05-10-2010 at 08:27 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    GCC (g++ et al) will compile C and C++ just fine for any of the platforms that GCC supports. I find it amusing that someone would try to convince you that only Windows programming would allow it. I see a lot of C code wrapped in files with a .cpp extension that GCC/G++ compiles just dandy everyday. Whether you can compile C and C++ code together is more a function of the compiler, not the platform.
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    This is how
    [32] How to mix C and C++ Updated! , C++ FAQ Lite

    This is why you need to be careful
    Incompatibilities Between ISO C and ISO C++

    Just compiling your C code as C++ would be a mistake in the long run. You can still compile the C parts as C, and the C++ parts as C++ and mix everything together at the end.
    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.

Popular pages Recent additions subscribe to a feed