1

In my project, realised in c++ I make use of 2 libraries:

  • Libsodium (for encrypting and decrypting)
  • SFML (for the graphics part)

I know that Libsodium is realised in pure C code, while SFML in C++, whereas my project is in C++.

So, Can I mix C code with C++ code in a safe manner (without the risk of bugs and memory leaks)?

Thank you in advance

Adam
  • 11
  • 4
  • 2
    In principle, yes. But It's your reponsibility to use the C API properly (e.g. manually release resources that in C++ you expect to be managed automatically). Also when you #include the C headers, use `extern "C" { ... }`. – wohlstad Aug 20 '22 at 12:40
  • 1
    Any resource leaks are independent on whether you're writing C or C++ code, but I'd argue that only with C++ you're able to implement RAII, so it should actually be easier to avoid resource leaks that could occur when using the C API improperly: For every resource that requires some action to release it you can either use `std::unique_ptr` with `SomeDeleter` containing the proper logic to free the resource or preferrably write a custom class that provides a default constructor and move semantics to allow for use with C++ containers... – fabian Aug 20 '22 at 13:02
  • There is a difference between inserting C **code** into a project and linking a project against a C **library**. Are you actually building libsodium within your project or just using its headers and linking in a pre-compiled library? – JaMiT Aug 20 '22 at 16:18
  • 1
    Does this answer your question? [Using C Libraries for C++ Programs](https://stackoverflow.com/questions/12066279/using-c-libraries-for-c-programs) – JaMiT Aug 20 '22 at 16:25

1 Answers1

-2

You can, but you shouldn't. Use a different file and use a header with extern "C". C++ is (was or was intended to be) a superset of C, so everything written in C works in C++. But there can be unexpected complications, like namespace collisions, C++ keywords as variable names, and other unexpected stuff. Also C is not clean C++, so some people wont like your code.

Edit, to clear misunderstandings:
You can of course combine both of them, but you should not just copy C code into a C++ project.

Another Edit:
There are exceptions to the superset behavior. Sorry for spreading false information.

Tenobaal
  • 637
  • 1
  • 16
  • 4
    "Everything written in C works in C++." - This is not true. – Karen Baghdasaryan Aug 20 '22 at 13:03
  • Can you give an example of what does not work? (except the restrict keyword) – Tenobaal Aug 20 '22 at 13:04
  • *"You can, but you shouldn't."* A corrolary of this would be that you shouldn't write C++ code at all: Basically every C++ program links libc. Any good quality C library will also contain preprocessor logic provide the the proper interface for both C and C++(`#ifdef __cplusplus extern "C" { #endif ...`), so this may just be a possible source of an error, if you attempt to do this on your own. *"unexpected complications, like namespace collisions"* This is not the result of you using C++ together with C. It's arguably less likely, since in C++ you CAN use namespaces for your symbols. – fabian Aug 20 '22 at 13:09
  • I was expecting him to just copy paste C code into a C++ file. This could lead to variables named "class" or something like this. I am not saying you shouldn't write C++ code, I am saying, that C style code in a C++ project is usually bad practice. – Tenobaal Aug 20 '22 at 13:11
  • 1
    @fabian _A corollary of this would be that you shouldn't write C++ code at all_ Quite honestly, that would save me a lot of trouble! ;) – Paul Sanders Aug 20 '22 at 13:16
  • Just another example: An empty parameter list has different meanings in C and C++. I'm sure you can do some web research to find more. – the busybee Aug 20 '22 at 13:22
  • 1
    `'$'` is an `int` in C a `char` in C++. – pmg Aug 20 '22 at 13:39
  • 1
    @Tenobaal *"I was expecting him to just copy paste C code into a C++ file."* -- maybe read the question body in addition to the question title? (Using a library typically does not involve copy-paste of code.) It's unfortunate, but not uncommon, for the question title and question body to ask different things. – JaMiT Aug 20 '22 at 16:23
  • 1
    Your answer looks like a mess with all the cross-outs and "edit" sections. You may want to consider editing your answer to clean that up. Someone who is interested in the changes you've made can click the link for the answer history. – JaMiT Aug 24 '22 at 05:10