Thread: Can you compile C code within C++?

  1. #1
    Registered User
    Join Date
    Jun 2014
    Posts
    8

    Question Can you compile C code within C++?

    If I have a bunch of C++ code, can I modify it/add to it with C code in any way?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Yes, though why do you want to do so? You might be able to just link to the C code as a separate library.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Please show an example of what you're trying to do. It would probably be easier to go the other way, take your C code and add it to a C++ project.

    Jim

  4. #4
    Registered User
    Join Date
    Jun 2014
    Posts
    8
    Quote Originally Posted by laserlight View Post
    Yes, though why do you want to do so? You might be able to just link to the C code as a separate library.
    Because I know C and not C++.

    Quote Originally Posted by jimblumberg View Post
    Please show an example of what you're trying to do. It would probably be easier to go the other way, take your C code and add it to a C++ project.

    Jim
    Unfortunately I can't. I'm messing around with the UDK which with the latest version apparently allows for engine modifications in C++.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Then perhaps the correct solution is to learn C++
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Jun 2014
    Posts
    8
    Quote Originally Posted by laserlight View Post
    Then perhaps the correct solution is to learn C++
    I plan to, but until then I was hoping to be able to use my C knowledge.

  7. #7
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    C is a lot like C++. As for as variable types, loops, switch, if, do and while statements for example. However C++ allows the programmer more OOP implementation. When I was moving from C to C++ I googled the differences and found a artical that said, a person whom know C++ pretty much knows C but a person who knows C doesn't really know C++. This statement holds a lot of truth and someone can learn the libraries fast in C because they are smaller, C++ is huge but has a bunch of reallz useful tools.

    Using pure C in C++ is rather silly in my opinion because there is a huge change that there is a better way to handle it in C++ with a library or perhaps a framework.

    Hope that helps.

  8. #8
    Registered User
    Join Date
    Jun 2014
    Posts
    8
    Quote Originally Posted by jocdrew21 View Post
    C is a lot like C++. As for as variable types, loops, switch, if, do and while statements for example. However C++ allows the programmer more OOP implementation. When I was moving from C to C++ I googled the differences and found a artical that said, a person whom know C++ pretty much knows C but a person who knows C doesn't really know C++. This statement holds a lot of truth and someone can learn the libraries fast in C because they are smaller, C++ is huge but has a bunch of reallz useful tools.

    Using pure C in C++ is rather silly in my opinion because there is a huge change that there is a better way to handle it in C++ with a library or perhaps a framework.

    Hope that helps.
    It helps and makes a lot of sense, however, until I am able to pick up an adequate amount of knowledge in C++, I'd like to use C within current C++ code.

  9. #9
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    So what's stopping you? Use a C++ compiler.

    C++ allows you to use most of the C functions and libraries with few, if any, changes. But your major problem will probably be interfacing your C mindset with C++.

    Jim

  10. #10
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    Just keep this in mind as you learn other languages and try and solve whatever problem. Think how can you solve this program in C++ not how can I make my C code work in C++. When I was learning Java I tried to get away with just my C and C++ knowledge until someone made that very same comment to me. Here is a small example of something I did not to long ago, it is actually something I learned on here from Lazerlight.

    I was using normal C arrays and trying to return the whole array and call it in a different function. Here is an example:
    Code:
    int foo(int myArray[], int counter )
    {
           //does something with the array here
    
    
            return myArray[counter++]; //I think I tried something like this 
    }
    However in C++ you can just do this

    Code:
     //if you do not understand the private part yet don't worry its not hard, when you get to classes you will pick it up.
    
    class Example
    {
    private: std::array<int,3>& myArray; 
    
    public:
    void setArray()
    {
    for(int i=0; i<3; i++) //you can put the int inside the loop in C++
    {
           myArray[i]=i;
    
          // or you can do something even better to make sure you do not go out of bounds
             myArray.at(i)=i;
    }
    }
    }
    
    std::array<int,3>& getArray()
    {
             return myArray;
    }
    I did not compile this or use a compiler to write it so if there are mistakes, sorry. However I think I got my point across.
    Last edited by jocdrew21; 07-06-2014 at 09:45 AM.

  11. #11
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    If you're learning C++ from C I'd say it's pretty easy. Just study the basics like what jocdrew is pointing out (object-oriented design).

    But more than that, I found the useful things in C++ vs C were constructors and the STL. Pretty much every data structure you've ever thought of is in the STL and even some I would've never guessed.

  12. #12
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    99% of valid C is valid C++, so you could write C code, compile as C++, and it will work.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Cannot Compile Code!
    By ottomated in forum C Programming
    Replies: 10
    Last Post: 04-01-2008, 03:43 PM
  2. I need help to compile this code...
    By wise_ron in forum C Programming
    Replies: 17
    Last Post: 05-07-2006, 12:22 PM
  3. Code will not compile - code from a book!
    By Chubz in forum C++ Programming
    Replies: 19
    Last Post: 09-12-2004, 10:21 PM
  4. Replies: 3
    Last Post: 03-07-2003, 09:06 PM