Thread: what's a "dynamic buffer"?

  1. #16
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    And combining code compiled with a C compiler with a C++ compiler... can you do that? Or am I misunderstanding what you're saying?
    C++ includes all the libraries that C89 had, so technically you could safely compile C89 as C++. But you would have to conform to C++'s stringent type system, which requires a cast to and from void* for example, and cannot rely on newer C-only features to do it without warnings or errors.

    Further, good IDEs will let you either compile as C or make a C project, and there are still lone C compilers (such as gcc), so that you should be able to choose the language you want instead of mixing and mashing.

    So, yes and no. It depends on what you're doing.

  2. #17
    Registered User
    Join Date
    Mar 2008
    Posts
    43
    Quote Originally Posted by citizen View Post
    C++ includes all the libraries that C89 had, so technically you could safely compile C89 as C++. But you would have to conform to C++'s stringent type system, which requires a cast to and from void* for example, and cannot rely on newer C-only features to do it without warnings or errors.

    Further, good IDEs will let you either compile as C or make a C project, and there are still lone C compilers (such as gcc), so that you should be able to choose the language you want instead of mixing and mashing.

    So, yes and no. It depends on what you're doing.
    I guess I was not very clear. I was saying that he was saying compile as C code like:
    gcc -c test.c -o test.o

    and then follow with a C++ compiler:
    g++ test.o test2.cpp

  3. #18
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by mingerso View Post
    I guess I was not very clear. I was saying that he was saying compile as C code like:
    gcc -c test.c -o test.o

    and then follow with a C++ compiler:
    g++ test.o test2.cpp
    Yes, that is the correct way to do something. You still need to tell the compiler when compiling test2.cpp that the functions in test.c are "C" functions rather than "C++" functions, otherwise the linker would be complaining about not being able to find the functions in test.o, since they are named differently from the ones called by test2.cpp [as I said in the previous post].

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

  4. #19
    Registered User
    Join Date
    Mar 2008
    Posts
    43
    Quote Originally Posted by matsp View Post
    Yes, that is the correct way to do something. You still need to tell the compiler when compiling test2.cpp that the functions in test.c are "C" functions rather than "C++" functions, otherwise the linker would be complaining about not being able to find the functions in test.o, since they are named differently from the ones called by test2.cpp [as I said in the previous post].

    --
    Mats
    I tried a couple of tests on Linux with gcc but could not figure out how to do this. You wouldn't happen to know the options to pass to gcc?

    As far as I have seen using gcc, you cannot do this... but I don't write in C++. A good example supporting this would be using the same library [runtime] but one is compiled for C and the other is for C++.

    If this does not work with gcc/linux does it work on other compilers or systems?

  5. #20
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by mingerso View Post
    I tried a couple of tests on Linux with gcc but could not figure out how to do this. You wouldn't happen to know the options to pass to gcc?

    As far as I have seen using gcc, you cannot do this... but I don't write in C++. A good example supporting this would be using the same library [runtime] but one is compiled for C and the other is for C++.

    If this does not work with gcc/linux does it work on other compilers or systems?
    It should be
    Code:
    extern "C" {
        /* Function prototypes go here */
    }

  6. #21
    Registered User
    Join Date
    Mar 2008
    Posts
    43
    Quote Originally Posted by tabstop View Post
    It should be
    Code:
    extern "C" {
        /* Function prototypes go here */
    }
    Sorry, I'm an idiot and typed:
    Code:
    extern C {
    }
    Forgot the quotes.

  7. #22
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by mingerso View Post
    Sorry, I'm an idiot and typed:
    Code:
    extern C {
    }
    Forgot the quotes.
    Yeah, that would give you some errors, I would think.

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

  8. #23
    Registered User
    Join Date
    Mar 2008
    Posts
    43
    Quote Originally Posted by matsp View Post
    Yeah, that would give you some errors, I would think.

    --
    Mats
    Yes, errors aplenty.
    Anyways, this little tangent on this thread about casting could be summed up as:

    1. If invoking a C++ compiler on C code you should cast mallocs.
    2. If invoking a C compiler don't cast malloc even if you're using the object code with C++ code but you will need (if C++):
    Code:
    #ifdef __cplusplus
    extern "C" {
    #endif
    ...
    #ifdef __cplusplus
    }
    #endif

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. "dynamic array"
    By stormbringer in forum C Programming
    Replies: 4
    Last Post: 01-17-2003, 01:34 PM