Thread: Running C++ in C language

  1. #1
    Registered User
    Join Date
    Sep 2020
    Posts
    2

    Running C++ in C language

    Hello everyone. I have a watch code working with arduino. However, the code is written in C. When I want to add a code written in C ++, Arduino IDE gives an error. What are your suggestions?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,079
    A few options come to mind:
    • Convert the C++ code that you apparently found to C.
    • Convert the C watch code that you wrote to C++ (if you're lucky it might already be valid C++)
    • Write a C interface to the C++ code. This could mean using the pimpl/opaque pointer idiom with extern "C" so you can call the C interface from your own code, oblivious to the fact that the actual implementation is in C++.

    Generally, it is easier to use a C library from C++ code than vice versa since C is somewhat a subset of 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

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    455
    Quote Originally Posted by laserlight View Post
    A few options come to mind:
    • Convert the C++ code that you apparently found to C.
    • Convert the C watch code that you wrote to C++ (if you're lucky it might already be valid C++)
    • Write a C interface to the C++ code. This could mean using the pimpl/opaque pointer idiom with extern "C" so you can call the C interface from your own code, oblivious to the fact that the actual implementation is in C++.

    Generally, it is easier to use a C library from C++ code than vice versa since C is somewhat a subset of C++.
    Yes, you can call C from C++ because there is the "extern C" mechanism. You can't call C++ from C, with the exception of C++ functions marked "extern C". So usually the best thing to do is to write "extern C" wrappers for the C++, then call from C.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


  4. #4
    Registered User
    Join Date
    Sep 2020
    Posts
    2
    Quote Originally Posted by Malcolm McLean View Post
    Yes, you can call C from C++ because there is the "extern C" mechanism. You can't call C++ from C, with the exception of C++ functions marked "extern C". So usually the best thing to do is to write "extern C" wrappers for the C++, then call from C.
    But the code has a lot of classes, structers, datastructers and members. It's so difficult.

  5. #5
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,883
    Quote Originally Posted by eale View Post
    But the code has a lot of classes, structers, datastructers and members. It's so difficult.
    This should not be a problem on an Arduino, as it is not purely C: It implements parts of C++.

    What code are you trying to run?

    What error codes are you getting?
    Fact - Beethoven wrote his first symphony in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Importance of english language in programming language
    By Lea Pi in forum General Discussions
    Replies: 10
    Last Post: 04-17-2015, 07:43 AM
  2. Is C++ or C language is a high Level language?
    By uthmankhale in forum C++ Programming
    Replies: 5
    Last Post: 08-25-2011, 06:00 PM
  3. Replies: 13
    Last Post: 12-09-2008, 11:09 AM
  4. What's the Difference Between a Programming Language and a Scripting Language?
    By Krak in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 07-15-2005, 04:46 PM
  5. Computer Language VS Spoken Language
    By Isometric in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 02-04-2002, 03:47 PM

Tags for this Thread