This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Makefile Modification for C++ Build

Hello,

I am using nordic sdk and working with C files.

But i would like to work on with C++ files as well.

How will i modify the makefile so that it can build both C and C++ can you show me an example ? Thanks

Parents
  • There is little reason to use two different "compilers". Difficulties usually arise when you are linking a library's object file or archive that was compiled with C linkage (for which you don't have the source) linking with source compiled using C++ linkage. The C++ compiler can compile C code (C code is valid C++). Read about "linkage" conventions and "name mangling." In many projects in embedded development, you have all the source code and compile everything (you don't link in a pre-compiled library's object file) so it can all be compiled using C++ linkage.

    You don't need to name C++ files with .cpp, that is a convention but the C++ compiler will gladly compile a file with the .c suffix, even if it contains class definitions.

    There are other considerations such as calling a method of a dynamic object (on the heap) from a "C" function. See Nordic notes about "wrapping" such methods. But for most simple applications, you can use static objects (usually just one object instance, and not stored on the heap) i.e. class methods and data members. While you could implement such objects in C, using C++ classes might be more understandable.

    Another consideration: since much of the SDK is non-strict C++, you might need to add the compiler flag -fpermissive so that you get warnings instead of errors.

Reply
  • There is little reason to use two different "compilers". Difficulties usually arise when you are linking a library's object file or archive that was compiled with C linkage (for which you don't have the source) linking with source compiled using C++ linkage. The C++ compiler can compile C code (C code is valid C++). Read about "linkage" conventions and "name mangling." In many projects in embedded development, you have all the source code and compile everything (you don't link in a pre-compiled library's object file) so it can all be compiled using C++ linkage.

    You don't need to name C++ files with .cpp, that is a convention but the C++ compiler will gladly compile a file with the .c suffix, even if it contains class definitions.

    There are other considerations such as calling a method of a dynamic object (on the heap) from a "C" function. See Nordic notes about "wrapping" such methods. But for most simple applications, you can use static objects (usually just one object instance, and not stored on the heap) i.e. class methods and data members. While you could implement such objects in C, using C++ classes might be more understandable.

    Another consideration: since much of the SDK is non-strict C++, you might need to add the compiler flag -fpermissive so that you get warnings instead of errors.

Children
Related