Adding C++ support to C thought library?

This is a discussion on Adding C++ support to C thought library? within the C Programming forums, part of the General Programming Boards category; Is there an library for C compiler which will add C++'s features?...

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    88

    Adding C++ support to C thought library?

    Is there an library for C compiler which will add C++'s features?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Not really, since C++ is a different language. Some compilers (and the C99 standard) has cherry picked a few things that they find handy out of the C++ language (variable sized arrays and "variable declarations anywhere" being the most obvious ones).

    On the one hand, you could say that all C++ features can actualy be achieved in C. On the other hand, C++ _IS_ a different language, and to add those features to C would require changing the compiler - you can't just use a library as replacement.

    But bear in mind that the first C++ compiler actually produced C code, and used a standard C compiler to generate machine code.

    --
    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.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,029
    Is there a reason you can't use C++ instead?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    I don't know of such a library, but if you need to implement an object orientated design in C, then there are different strategies for this. Some links that might be useful:

    ldeniau.web.cern.ch/ldeniau/html/oopc/oopc.html
    www.planetpdf.com/codecuts/pdfs/ooc.pdf
    www.bolthole.com/OO-C-programming.html

  5. #5
    Registered User
    Join Date
    Jun 2008
    Posts
    127
    >>Not really, since C++ is a different language. Some compilers (and the C99 standard) has cherry picked a few things that they find handy out of the C++ language (variable sized arrays and "variable declarations anywhere" being the most obvious ones).

    When did C++ get variable sized arrays?

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by herWter View Post
    >>Not really, since C++ is a different language. Some compilers (and the C99 standard) has cherry picked a few things that they find handy out of the C++ language (variable sized arrays and "variable declarations anywhere" being the most obvious ones).

    When did C++ get variable sized arrays?
    I actually meant "non-compile-time size arrays", such as:
    Code:
    int func(const int n)
    {
        int arr[n];
        ... 
    }
    
    int main()
    {
       int n;
       cout << "Enter number of elements:";
       cin >> n;
    
       int x = func(n);
       ...
    --
    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.

  7. #7
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    You can do that in C99, so it's not really a C++ feature

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by zacs7 View Post
    You can do that in C99, so it's not really a C++ feature
    Yes, but that's what I said:
    Quote Originally Posted by me
    Some compilers (and the C99 standard) has cherry picked a few things that they find handy out of the C++ language (variable sized arrays and "variable declarations anywhere" being the most obvious ones).
    --
    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.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,029
    But since when did C++ allow arrays with a non-compile time constant arrays?
    Your example doesn't compile, mats!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Ooops my bad . Sorry matsp.

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Sorry, C++ doesn't technically support that, but gcc does as an extension - I got myself confused [again]. I'm not sure if C99 does support it or not - gcc -std=c99 -pedantic does compile this:
    Code:
    #include <stdio.h>
    
    double func(const int n)
    {
        int arr[n];
        double d = 0;
        for(int i = 0; i < n; i++)
        {
    	arr[i] = 1 << i;
        }
        for(int j = 0; j < n; j++)
        {
    	d += 1.0 / arr[j];
        }
        return d;
    }
    
    int main()
    {
       int n;
       printf("Enter number of elements:");
       scanf("%d", &n);
    
       printf("Result=%f\n", func(n));
       return 0;
    }
    whilst g++ -std=c++98 -pedantic does not. Remove pedantic and it does.

    --
    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.

  12. #12
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    VLAs were a GCC extension and were adopted in C99. They're not part of any C++ standard, and won't be part of C++0x, AFAIK.
    GCC's documentation says, however, that their implementation of VLAs is not 100&#37; compliant with the C99 definition.
    http://gcc.gnu.org/c99status.html

    A good library for getting object-oriented functionality into C is glib/gobject.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  13. #13
    Registered User
    Join Date
    Jul 2007
    Posts
    88
    Quote Originally Posted by Elysia View Post
    Is there a reason you can't use C++ instead?
    Yes, that's the whole point asking here.

    A library I am using compiles only with a C compiler, but not with a C++ compiler. My platform and compiler is outdated. Also no C++ compiler available with full (compared to recently compilers) standard support.

    C++ as "addon" for a standard C compiler would have been a nice thing, but if it doesn't exists I am out of luck.

  14. #14
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    That would be quite impossible. Different syntax means the compiler itself must change.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  15. #15
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    I'm not sure if I understand your problem well, but you can mix C and C++ code. In short you compile your C code with the C compiler and C++ code with the C++ compiler. By supplying linkage specifications you're able to use C code within C++ code.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Programming library with built in manager
    By cyreon in forum C Programming
    Replies: 5
    Last Post: 06-26-2009, 01:49 PM
  2. standrad library
    By sarahr202 in forum C++ Programming
    Replies: 11
    Last Post: 05-18-2009, 09:50 PM
  3. Shared library design problem
    By p1r0 in forum C++ Programming
    Replies: 9
    Last Post: 03-23-2009, 01:36 PM
  4. static data structure in a library
    By melight in forum C Programming
    Replies: 6
    Last Post: 01-10-2009, 11:12 AM
  5. university library
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 09-10-2003, 04:05 PM

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