LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Why low-level libraries uses C ? (https://www.linuxquestions.org/questions/programming-9/why-low-level-libraries-uses-c-4175560550/)

Borneq 12-03-2015 02:14 PM

Why low-level libraries uses C ?
 
why not C++? For example XLib, XCB.
C++ libraries will difficult to link with next level libraries?

Keith Hedger 12-03-2015 02:17 PM

Because C++ is a higher level library than C, and has a lot more overhead when compiling/running.

chrism01 12-03-2015 06:17 PM

... and of course, C came first.

In fact the Unix OS (& derivatives) & most DBs and higher level langs are written in C.
Therefore the libs can be used by the max number of programs.

sundialsvcs 12-03-2015 06:32 PM

Either language can be used, and both have been. There are some libraries that are written in FORTRAN.

Borneq 12-03-2015 07:31 PM

C++ is handy, but how C++ libraries can be used by other programs? Especially inline, templates, name mangling, virtual methods.

NevemTeve 12-03-2015 09:06 PM

If a component (library) is written in C++, its caller has to be written in C++, too. On the other hand, C components can be called from C++ without problems.

a4z 12-05-2015 11:32 AM

C was here first, it was invented to write Unix, so Unix is C.
C it has a standard ABI in opposite for example to C++.
it is relatively easy to include into other languages, in opposite to say C++, which is complex.
in the Unix world C is the lingua franca, you cann connect different programming languages via C interfaces.

Microsoft on the other hand does not even have a real C compiler, there you write your drivers with C++ base on a unbelievable ugly old style Cish interface.

NevemTeve 12-05-2015 11:41 AM

If Microsoft didn't have a C-compiler, how could they compile Windows?

a4z 12-05-2015 12:05 PM

Quote:

Originally Posted by NevemTeve (Post 5460251)
If Microsoft didn't have a C-compiler, how could they compile Windows?

with the compiler they have, since C is a valid subset of C++ (99.9%), they can compile their C like code with their compilers. Thought, I think they have 2 different runtime libraries to link again. And maybe there is a old compiler, but I do not thing anyone uses this, if you compile python ruby whatever on MS they want MSVC


http://herbsutter.com/2012/05/03/rea...ut-vc-and-c99/
Quote:

We recommend that C developers use the C++ compiler to compile C code (using /TP if the file is named something.c). This is the best choice for using Visual C++ to compile C code.
there is no problem with this, Haiku OS is an other example where the kernel is written in C++.
why should this be a problem?

NevemTeve 12-05-2015 12:15 PM

(I have to admit that in Windows I use either BCC4.52 (16-bit) or BCC55 (32-bit) or mingw-gcc (64-bit).)

sundialsvcs 12-06-2015 10:08 AM

Different parts of Microsoft's total software system are obviously written in many different languages.

"C" is ordinarily used in operating-system code because languages like "C++" rely upon services provided by runtime libraries which only exist "in user-land." In this context, "C" is being used as "one small step up from assembler," and in fact is often intermingled with assembler-code sections. (The "trampoline" code in the /arch directory of the Linux source library is an excellent example of this. Some of the files there are pure-assembler files.)

Higher-level languages are used whenever possible, and when a legacy source-code base that was originally started in "C" does not exist. That's because, well, "because the language system is doing more of the niggling detail-work for you," and, "that's what digital computers are for!" :)

There's little difference in the actual object-code that is generated. In the gcc compiler suite, most of the back-end code generation code does not care what source-code language was originally used. It's working with an intermediate representation that is more or less source-language agnostic.

Luridis 12-07-2015 06:31 AM

"C" is the hardware interface language. It's not about catering to the upward needs of the software stack. It's about talking to hardware in a predictable way. It's at this level almost all algorithms need to be checked for efficiency, be the efficiency in power, performance, or heat generation. For every run of your program's main loop, no-ops can happen low in the system millions of times. Hence why they calculus is still required for "Computer Systems Engineering", even though it's typically unnecessary for what most of us would consider programming.

Here is a video of Linus Torvalds discussing just this subject at a Q&A.

Knowing the language can help you a lot. However, writing everyday programs in C is typically unnecessary, or even dangerous if you were to publish code you are unwilling or unable to check for unsafe practices. For that reason, lots of people like to complain about the language's lack of exception handling. The reality is that it is simply not supposed to be there. Handling exceptions creates lots of overhead at the assembly level. It's the high-level application developers job to check input before passing it to some part of the system. I mean, if you're the one asking for input, it's not the libc developer's job to make sure it's well formed before trying to operate on the data, it's yours.


All times are GMT -5. The time now is 08:54 AM.