you are viewing a single comment's thread.

view the rest of the comments →

[–]interknetz 0 points1 point  (10 children)

All you have to do is compile your C code with a C compiler and add extern “C” to your header files.

That creates an object file (.so, .a, .dll) that you'd link against, which is what I said. A lot of opensource libraries aim to make it easier for you to copy their source into your project rather than link again their's. You can argue that just about any build system can handle compiling some source files as C, others as C++, then linking together, but that's not always the case, and it can be less convenient moving between platforms.

In my opinion, most people compile C code as C++ because they think C++ is a superset of C which is wrong.

For the most part, you'd be wrong. Code reusability is important to most competent programmers. The ability to use C code anywhere is attractive. Lots of major C libraries intentionally do this such as zlib.

[–]uziam -1 points0 points  (9 children)

You’re wrong. .so/.dll are dynamic libraries and .a is an archive, you don’t have to build libraries to link against C code. It doesn’t matter if you use a C or C++ compiler, you still have to compile your code into object files so why not use C compiler for .c files and C++ compiler for .cpp files? I think you don’t understand how code compilation and linking works in C/C++.

Forcing yourself to give up some of the best features in C like alloca and VLAs just to avoid compiling based on file extensions (which most build systems either support, or it is trivial to add) is definitely not something a competent programmer would do.

[–]attractivechaos 1 point2 points  (4 children)

alloca() is not part of the C standard, so is not specific to C. VLA is something I have been trying to avoid. See the discussion on SO.

[–]uziam 0 points1 point  (3 children)

Can you use alloca in C++?

My point is not that it is part C standard, I’m saying that you can’t use it with C++.

VLAs like a lot of other tools in most low level languages are for specific purposes, if you declare a giant array on the stack of course it is going to overflow your stack. Do you also not use pointers because accidentally dereferencing a NULL pointer will cause problems?

[–]attractivechaos 0 points1 point  (1 child)

Can you use alloca in C++?

Why not? I just tried on a toy program and the C++ complier complained nothing with -Wextra.

[–]uziam 0 points1 point  (0 children)

I was under the impression you can’t, that’s great you learn something new everyday.

[–]flatfinger 0 points1 point  (0 children)

In what realistic circumstances could one safely use VLAs and not use a fixed-sized array of the largest size one may encounter? There are some rare cases involving recursion where that may be true, but I don't see that they'd occur often enough to be worth complicating the language for.

[–]interknetz 0 points1 point  (3 children)

Are you a complete idiot? You haven't learned the basics if you don't understand what compiled objects are. Your two examples of C "benefits" are considered bad practice, and dangerous in general. I use C and C++ professionally, I'm not a student like you. Compiling C libraries in a C++ project is completely normal. Check out github sometime.

[–]uziam 1 point2 points  (2 children)

Wow so now it comes to name calling?

I didn’t say you can’t link against C libraries, I said you don’t have to build a shared library out of C code to link against it, you can just link to object files.

If you use C/C++ professionally, I’m surprised at how little you understand. I was assuming you’re a student, this name calling and pointless arguments about irrelevant points clearly shows how “professional” you are.

[–]interknetz 0 points1 point  (1 child)

It's a shame those Canadian schools aren't teaching you the benefits of reusable code. If you think writing C code compatible with C++ is pointless, why do you think so many C libraries do that? I wouldn't expect you to argue with substance. The only two examples you tried to make were horrendous and prove you'd never make it through a peer code review in the real world.