tags:

views:

288

answers:

5

Quite a newbie question as is.

A: 

The answer is yes. Take a look at this:

http://www.cplusplus.com/reference/clibrary/

Louis Rhys
+6  A: 

Yes. There is no reason you cannot use C libraries in C++. Things change if you want to compile C in a C++ compiler. The C ABI is fully supported from C++, however things are not necessarily so neat from an API perspective. Certain C additions such as restrict are not in the C++ standard, and must be dealt with carefully.

Matt Joiner
Beat me by 30 seconds w/ the same answer :)
Merlyn Morgan-Graham
@Merlyn Morgan-Graham: The stairs are long in your tower :)
Matt Joiner
A more extreme case might be a function which takes a vla-type argument, for instance `int foo(int n, char x[][n]);` - hope I got that right.
R..
Can you explain when is the `extern "C"` stuff necessary?
wamp
@R..: Yeah, interesting! I guess all that array syntax doesn't *really* do anything, but there's a huge possibility for unusable headers.
Potatoswatter
Yeah. Keep focused on the ABI and calling convention. Fortunately C has a rock solid, and extremely well supported calling convention, it's one of C's many great features.
Matt Joiner
It does do something - it declares that you're passing a multidimensional array/pointer to an array of variable length.
R..
Restrict isn't part of the C++ standard, but nearly every compiler supports it anyway.
Crashworks
What if the library has functions called `new` or `delete` or `class`, etc...
dreamlax
@dreamlax: A C library won't have those things. Even using a C++ library from C, you can work around these things. I always claim vehemently, (and this is why I detest "C++ blogs",) that C++ is just a ton of nasty metadata. There is NOTHING you can do from C++ that you can't do in C.
Matt Joiner
@Matt Joiner: Why can't a C library have a function called `new`, `delete`, or `class`. What if the C library has a function definition `int func(size_t n, char buf[n])` (variable length array parameter)? Some things in C cannot be used in C++ code.
dreamlax
@dreamlax: Yes obviously those are exceptions. Keywords aren't exactly a revelation of something you can't use from C. Even so you can still work around them, just modify the header to use new names, and use dlsym() or LoadLibrary() to get at the function names if they're at the link level.
Matt Joiner
@dreamlax: Also I've never heard of VLA as a parameter, I'm going to have to check that out. Is this what you refer to? http://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html
Matt Joiner
@Matt Joiner: "There is NOTHING you can do from C++ that you can't do in C." What argument is that? In the same spirit you could say "There's nothing in C that can't be done in assembler, so let's all get back to assembler!" That's nonsense. C++ is a lot more than meta-data added to C. First and foremost it's a different _programming philosophy_ (or rather, a whole bunch of it if you count the supported paradigms) and a lot of syntactical additions (plus a very few syntactical changes) to support this...
sbi
...Despite the similar syntax - just that philosophy thing by itself make C++ more different from C than from, say Delphi. Due to it, many of the good programming practices in C are actually bad programming practices in C++.
sbi
@sbi: There is more to the difference than just features. However without trolling, the only tangible thing that C++ adds that isn't metadata is at the linker level. A new calling convention, name mangling, and the implicit vptr tables. When in C++ one should definitely not program C style, but I vehemently protest any suggestion that preferring C over C++ is a bad thing.
Matt Joiner
@Matt: You could, with the same reasoning, vehemently protest any suggestion to write C instead of assembler. Everything that can be done in C can be done in assembler, after all. And, when you get to the heart of things, C is just a macro assembler on steroids. Of course this wouldn't do C justice. But the reasoning is just as strong (or weak, FTM) as yours facing C++. (What do you mean, these aren't a rational arguments? It's you who brought such to the table.)
sbi
+2  A: 

If the headers are properly protected with extern "C" { ... }, then yes.

Stephen Canon
If they aren't, you can either make your own headers, or you may be able to wrap the existing ones.
Merlyn Morgan-Graham
I wouldn't call putting `extern "C" { ... }` in a header for C-language code "proper", but Stephen's answer could be interpreted as putting the `extern "C" { ... }` around the `#include` lines.
R..
@R..: Of course, one either protects the `extern "C"` with `#if defined __cplusplus`, or wraps the include instead of the header itself. Thought that was a given.
Stephen Canon
+1 for mentioning the extern issue. For the "newbie" poster - C++ normally "mangles" identifiers into a subset of characters that the "linker" tool supports, whereas C doesn't have to (no namespaces, overloading etc). So, extern "C" tells the C++ compiler that a function name isn't/shouldn't be mangled (when defined externally/internally to the compilation unit respectively).
Tony
I tried `#include <stdio.h>` without `extern "C"` ,also works, why?
wamp
@wamp: `#include <cstdio>` is the c++ way and `#include <stdio.h>` is provided for backwards-compatibility, but it's deprecated. (In other words, it works because the standard says it needs to.)
GMan
Can you explain when is the `extern "C"` stuff necessary?
wamp
@wamp: Most standard C headers assume the worst and cater for C++ compilers also. Look for `#ifdef __cplusplus` in your C headers, they'll wrap pretty much everything with `extern "C"`.
Matt Joiner
@wamp: `extern "C"` is necessary for C linkage, I highly recommend making a new question, or digging up an existing one for this.
Matt Joiner
A: 

Yes, and no.

The questionables are...

  • Compund literals

  • Native complex number datatypes

  • "restrict" keyword

  • Variadic macros

  • "long long int" datatype

Some of those features from C are included in C++0x and some are available as library extensions in many newer compilers for "normal" C++.

So it depends on what level of C you are talking about, what level of the C++ standard, and what platform of what compiler since compiler implementations always have varied support for the standards and bugs of course.

And then there are keywords used in C++ which weren't defined in C, and are therefore available to be used as variable names in C but make a C++ compiler throw up. In C it is perfectly legal to use the following words as variables or function names, but they will obviously make C++ throw a hissy fit...

  • template
  • new
  • class

Oh and "goto" behaves differently in C++ and C. In C++ "goto" cannot be used to jump over a variable's initialization, but that's ok for C. Same goes for switch statements. In C you can write a switch statement or a set of goto's which will not compile in C++.

What else? "strchr" works differently in C vs C++. In C it returns a char pointer. In C++ it returns a const char pointer. If you use that output from strchr a certain way in C, it might blow chunks in C++ because of C++'s const correctness.

Inline functions are handled differently. In C they are scoped to the file, but in C++ they have external linkage by default.

C++ code needs function prototypes defined with extern "C" to call in to a C function.

C++ mangles symbols of function names but C does not.

"In theory there is no difference between theory and practice. In practice there is." - Yogi Berra

Allbite
Why are some downvoting? Did I make a mistake?
Allbite
A: 

I think my contrived example will show you why it isn't always possible:

#ifndef HEADER_H
#define HEADER_H
int class(int a, int b);
int private(int a);
#endif

Perfectly valid C but it won't compile in C++, even with an extern "C" block. As far as I know the only way to use a C library like that is to create another C library which calls those functions and then use that wrapper library in your C++ code.

That said, I think stumbling upon something like this in the "real world" is pretty rare.

Niki Yoshiuchi
To be fair, nobody should really naming their functions like that, but this does prove that not all C libraries are compatible.
dreamlax
But if you compiled it into a lib with a c compiler you could use the library in c++ - the name mangling takes care of it
Martin Beckett
@Martin - how would you go about using those functions in C++?
Niki Yoshiuchi