The Design of Software (CLOSED)

A public forum for discussing the design of software, from the user interface to the code architecture. Now closed.

The "Design of Software" discussion group has been merged with the main Joel on Software discussion group.

The archives will remain online indefinitely.

Mixing paradigms

I'm doing some programming on linux in c++.  My background is OO java->C++, and this is the first time I've really had to deal with the procedura linux api.

Of course linux is written in c, and the posix api function calls are all in c.  Also, useful stuff like pthreads are also all in c.

How do you manage the area where your c++ uses c directly.  Do you wrap the c up in c++ wrappers?  Do you stick with STL, or use the old c-style library?  How compatible are STL streams with Linux streams?

Thanks.
Vinny
Thursday, November 11, 2004
 
 
This is the purpose of the:

/* Any .H file ... */

#ifdef  __cplusplus
extern "C" {
#endif

// More code ...

#ifdef  __cplusplus
}
#endif

construct.  Declaring the 'More code...' stuff as 'extern "C" {...}' is a *magical* call which removes the C++ name mangling.  The ultimate result of *that* is to allow C++ code to call 'C' library code directly.

I believe all the Linux *.H files have the 'extern "C" ' directive in them.
AllanL5
Thursday, November 11, 2004
 
 
The 'rawest' way to use the 'C' library calls is just to use the 'C' standard library.

Another way is to use a compatibility library (like RogueWave).

Another way is to create your own 'wrapper' objects with better semantics than the raw 'C' library calls.  This has the advantage that *you* define the 'good behavior' you want.

The fastest way is to stay 'raw', I believe.  It's a well-defined, well-debugged API which shouldn't change much in coming years.
AllanL5
Thursday, November 11, 2004
 
 
After all of your #includes (stdio.h, stdlib.h,  ...) add a
"using namespace std;"
then you can call all the system stuff directly, otherwise you need to prepend the namespace to system calls.

I use a mix of STL and traditional C-strings and stuff. We're writing a very high performance C++ app (data rates over 1GB/sec), and there are places where you don't want to dynamically allocate any memory so "static char buff[1024]" works. I use the STL map a lot with a "string" as a key. The C runtime will manage string memory so when they go out of scope they automagically get freed - one less memory management thing to dink around with.

Memory management is one area that you're going to have to worry about, coming from a Java background. I hate that micromanagement of resources most about C++. And I miss developing in an IDE with an integrated debugger.

Another "feature" of C++ is operator overloading - I don't use it and don't miss it, preferring the Java, named methods way to go.
SIGPIPE
Sunday, November 14, 2004
 
 

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics
 
Powered by FogBugz