Jump to content

Is C++ necessary for some libraries over C?

I am learning a bit of Windows API, and almost all the environment is related to C++. I am not that good with C++, and I got onto C to first master that before jumping to C++. Do I need to learn C++ to fully use all of the Windows API or is just C fine? 

 

Looks like I am in confusion. Code written in C is directly accessible through C++ as C++ is C and more. But I think in the API, there are several C++ exclusive stuff like many functions inside classes, and there are namespaces and stuff. Is all that code accessible through C as well? 

Microsoft owns my soul.

 

Also, Dell is evil, but HP kinda nice.

Link to comment
Share on other sites

Link to post
Share on other sites

AFAIK the WinAPI itself is C-only (with wrappers to other langs). Aren't you talking about other APIs that are also on windows?

 

5 minutes ago, Gat Pelsinger said:

C++ is C and more. But I think in the API, there are several C++ exclusive stuff like many functions inside classes, and there are namespaces and stuff.

This implies that C is kind of a subset of C++, and that's not the case. Most of the stuff under C is able to compile in a Cpp compiler, but not everything.

8 minutes ago, Gat Pelsinger said:

Is all that code accessible through C as well?

No, but I bet there's a C FFI/wrapper for those APIs.

FX6300 @ 4.2GHz | Gigabyte GA-78LMT-USB3 R2 | Hyper 212x | 3x 8GB + 1x 4GB @ 1600MHz | Gigabyte 2060 Super | Corsair CX650M | LG 43UK6520PSA
ASUS X550LN | i5 4210u | 12GB
Lenovo N23 Yoga

Link to comment
Share on other sites

Link to post
Share on other sites

I suggest you pick up Rust instead of C++ as your next language. The people behind rust compilers and its standards had decades to learn from the mistakes and best features/practices of other major languages and so are designed with all of these in mind. It doesn't use a garbage collector like what many people misunderstood but rather moves all the memory safety enforcement checks to the compiler. It just feels a lot more modern and if you ever work on any rust project, you can be sure you won't be maintaining legacy code from the early 1990s built with outdated anti-patterns and legacy syntaxes/libraries from way back. 

 

Edit: this is all you need. 

https://learn.microsoft.com/en-us/windows/dev-environment/rust/rust-for-windows

 

https://github.com/microsoft/windows-rs

Sudo make me a sandwich 

Link to comment
Share on other sites

Link to post
Share on other sites

I can't say too much about the Windows API, but to answer the question in your title: Yes, libraries written in C++ generally will not work with C code (unless you do things like writing wrappers for the C++ code).

 

In contrast, most "plain" C code can be used in C++ just fine. There are some C features/syntax that aren't valid in C++ though: https://en.wikipedia.org/wiki/Compatibility_of_C_and_C%2B%2B

 

If you want to learn C++, go ahead. I certainly find C++ nicer to use than C for building larger programs, although there's a lot of added complexity for someone new-ish to programming. It also has a much larger standard library compared to C (hooray, generic data structures). As for the Rust vs. C++ argument, it's really up to you to decide. Rust is newer, popular, and has builtin memory safety while C++ is a lot more mature. C++ is my choice because of how common it is in my fields of interest, but I'll get around to learning Rust too someday.

Computer engineering grad student, cybersecurity researcher, and hobbyist embedded systems developer

 

Daily Driver:

CPU: Ryzen 7 4800H | GPU: RTX 2060 | RAM: 16GB DDR4 3200MHz C16

 

Gaming PC:

CPU: Ryzen 5 5600X | GPU: EVGA RTX 2080Ti | RAM: 32GB DDR4 3200MHz C16

Link to comment
Share on other sites

Link to post
Share on other sites

On 3/2/2024 at 2:34 PM, Gat Pelsinger said:

I am learning a bit of Windows API, and almost all the environment is related to C++. I am not that good with C++, and I got onto C to first master that before jumping to C++. Do I need to learn C++ to fully use all of the Windows API or is just C fine? 

Can you be more specific in what you are referring to?

 

Like which API are you using that you think requires the C++ stuff.  I mean you could pretty much do everything in C if you wished to (there might be some things like com stuff that has C++'s involved...but I thought most have some form of wrappers as well for C)

3735928559 - Beware of the dead beef

Link to comment
Share on other sites

Link to post
Share on other sites

  • 4 weeks later...
On 3/3/2024 at 3:25 AM, wasab said:

I suggest you pick up Rust instead of C++ as your next language. The people behind rust compilers and its standards had decades to learn from the mistakes and best features/practices of other major languages and so are designed with all of these in mind. It doesn't use a garbage collector like what many people misunderstood but rather moves all the memory safety enforcement checks to the compiler. It just feels a lot more modern and if you ever work on any rust project, you can be sure you won't be maintaining legacy code from the early 1990s built with outdated anti-patterns and legacy syntaxes/libraries from way back. 

 

Edit: this is all you need. 

https://learn.microsoft.com/en-us/windows/dev-environment/rust/rust-for-windows

 

https://github.com/microsoft/windows-rs

- Rust is so much slower to compile

- the build system is even more a mess

- 20 game engines yet no games, says a lot about the community

- Yes it was built in the 1990's but people haven't miraculously become smarter

And it's offtopic, so am i.

 

On 3/2/2024 at 10:34 PM, Gat Pelsinger said:

I am learning a bit of Windows API, and almost all the environment is related to C++. I am not that good with C++, and I got onto C to first master that before jumping to C++. Do I need to learn C++ to fully use all of the Windows API or is just C fine? 

 

Looks like I am in confusion. Code written in C is directly accessible through C++ as C++ is C and more. But I think in the API, there are several C++ exclusive stuff like many functions inside classes, and there are namespaces and stuff. Is all that code accessible through C as well? 

I don't really get what you are saying, but yes with a simple :

#ifdef __cplusplus
extern c {
#endif

/* Your C code here */

#ifdef __cplusplus
}
#endif

You can include C code in a C++ project, for example to use C++ libs. For the rest (namespaces and all) it's pretty easily googleable but a bit long so i will let you search it up.

Link to comment
Share on other sites

Link to post
Share on other sites

On 3/2/2024 at 5:34 PM, Gat Pelsinger said:

almost all the environment is related to C++

Most if not all of windows core api are now converted to Rust. I haven't check recently but i don't think it's fully completed as they would have most likely posted something in insider devblogs that i follow every week https://devblogs.microsoft.com/

 

Most if not all are interopable so most language that support interopability can call them pretty easily no matter if it's one of the C, C++ or Rust api. You can use C++ as well as C#, Java, even VB 6.0 and they all interop just fine.

Link to comment
Share on other sites

Link to post
Share on other sites

2 hours ago, Franck said:

Most if not all of windows core api are now converted to Rust

Source? 

FX6300 @ 4.2GHz | Gigabyte GA-78LMT-USB3 R2 | Hyper 212x | 3x 8GB + 1x 4GB @ 1600MHz | Gigabyte 2060 Super | Corsair CX650M | LG 43UK6520PSA
ASUS X550LN | i5 4210u | 12GB
Lenovo N23 Yoga

Link to comment
Share on other sites

Link to post
Share on other sites

it's on DevBlogs somewhere but here a more recent article on it https://www.theregister.com/2023/04/27/microsoft_windows_rust/

The article is last year but change is way older than that. IIRC it started early covid. Strangely US military and or secret services recently stated they will only deal with application that use safe language therefore c++ isn't good for them anymore and if you look at that Microsoft is doing the switch. I feel like they had the info that it was coming a long time ago

Link to comment
Share on other sites

Link to post
Share on other sites

6 minutes ago, Franck said:

it's on DevBlogs somewhere but here a more recent article on it https://www.theregister.com/2023/04/27/microsoft_windows_rust/

The article is last year but change is way older than that. IIRC it started early covid. Strangely US military and or secret services recently stated they will only deal with application that use safe language therefore c++ isn't good for them anymore and if you look at that Microsoft is doing the switch. I feel like they had the info that it was coming a long time ago

None of that implies that "Most if not all of windows core api are now converted to Rust", but rather just small parts of it.

FX6300 @ 4.2GHz | Gigabyte GA-78LMT-USB3 R2 | Hyper 212x | 3x 8GB + 1x 4GB @ 1600MHz | Gigabyte 2060 Super | Corsair CX650M | LG 43UK6520PSA
ASUS X550LN | i5 4210u | 12GB
Lenovo N23 Yoga

Link to comment
Share on other sites

Link to post
Share on other sites

2 hours ago, igormp said:

but rather just small parts of it.

Well we already have all file io, registry, GDI, direct 2d done for desktop.

They have a rule where If GC is required they must use C/C++ otherwise Rust.

 do believe most Azure tools are therefor using Rust including Office 365.

 

Since OP was talking about windows api which mean desktop IO and GDI is pretty much 99% of use case i can see except if you talk solely of gaming. I have not yet seen and update in DevBlogs about DirectX conversion being completed.

 

Link to comment
Share on other sites

Link to post
Share on other sites

16 minutes ago, Franck said:

Well we already have all file io, registry, GDI, direct 2d done for desktop.

They have a rule where If GC is required they must use C/C++ otherwise Rust.

 do believe most Azure tools are therefor using Rust including Office 365.

 

Since OP was talking about windows api which mean desktop IO and GDI is pretty much 99% of use case i can see except if you talk solely of gaming. I have not yet seen and update in DevBlogs about DirectX conversion being completed.

 

Any sources for file io, direct 2d and registry?

All I've seen is the rewrite of a minor part of GDI (which is also just a minor part of the Win32 API), with no news of it being the default one yet (last year it was already shipping, but disabled behind a feature flag), and their font parsing stuff (dwrite).

 

The link you gave refers to this video, where I got the above from:

 

I do believe there are tons of part using rust, but that rust is still really small in the overall code base, and far from your initial claim that most of the stuff has been ported yet.

FX6300 @ 4.2GHz | Gigabyte GA-78LMT-USB3 R2 | Hyper 212x | 3x 8GB + 1x 4GB @ 1600MHz | Gigabyte 2060 Super | Corsair CX650M | LG 43UK6520PSA
ASUS X550LN | i5 4210u | 12GB
Lenovo N23 Yoga

Link to comment
Share on other sites

Link to post
Share on other sites

To make langauges interporable, all you need is to map some xyz libary into your own programs memory space. At the end of day, computers execute machine code which are 1s and 0s regardless what higher langauges these instructions got compiled/interpreted from.

 

Higher level programming langauge is simply an abstraction of these lower level code. In fact, thats the definition of a compiler vs like say transpiler. E.g. a compiler translates languages with higher abstraction into a lower abstraction while transpiler translates between languages of similar abstraction so at the end of the day, they all end up speaking in 1s and 0s. 

 

In short, you just need a compiled dynamic library that exposes an application binary interface for your own program process to map to and invoke at runtime. They are all 1s and 0s at the end of the day. Any compiled module(libary module mind you, if you load a full program, you are actually very close to doing the equivalent of a C execve, btw check this out lol, hilarious) from any programming language can be loaded and mapped into the runtime of an executing process. 

 

So yeah, c++ can call c code just fine. Also, still going off tangent, for rust, you have to use unsafe keyword in combination with extern keyword to invoke the C libary because all of the safety features of rust go out the window when you call external code, coded in languages that are not type safe, memory safe, whatever safe. 

(Ps, currently doing a hobbyist rust project. Come join me if any of you are rust enthusiasts like me)

Sudo make me a sandwich 

Link to comment
Share on other sites

Link to post
Share on other sites

On 3/27/2024 at 10:11 AM, Attilavsq said:

- Rust is so much slower to compile

Nope 

 

On 3/27/2024 at 10:11 AM, Attilavsq said:

 the build system is even more a mess

Nope

 

On 3/27/2024 at 10:11 AM, Attilavsq said:

20 game engines yet no games, says a lot about the community

 

Nope

 

On 3/27/2024 at 10:11 AM, Attilavsq said:

Yes it was built in the 1990's but people haven't miraculously become smarter

Compilers become smarter babysitting human errors even if *some* humans remain clueless. 

Sudo make me a sandwich 

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, wasab said:

Nope 

 

Gotta agree with them on that, first time to compile stuff is really painful, and the tons of libraries that most projects need to pull is almost on the level of npm projects, which only makes things worse.

FX6300 @ 4.2GHz | Gigabyte GA-78LMT-USB3 R2 | Hyper 212x | 3x 8GB + 1x 4GB @ 1600MHz | Gigabyte 2060 Super | Corsair CX650M | LG 43UK6520PSA
ASUS X550LN | i5 4210u | 12GB
Lenovo N23 Yoga

Link to comment
Share on other sites

Link to post
Share on other sites

19 hours ago, igormp said:

Gotta agree with them on that, first time to compile stuff is really painful, and the tons of libraries that most projects need to pull is almost on the level of npm projects, which only makes things worse.

Hot reload greatly cut down on the need to recompiled during development and testing. 

 

Javascript hot reloads out of the box tho since it is interpreted so you won't need to compile at all unless you are working with typescript. 

Sudo make me a sandwich 

Link to comment
Share on other sites

Link to post
Share on other sites

On 3/27/2024 at 6:11 PM, Attilavsq said:

- Rust is so much slower to compile

 

And eats more RAM.

Write in C.

Link to comment
Share on other sites

Link to post
Share on other sites

On 3/2/2024 at 11:34 PM, Gat Pelsinger said:

I am learning a bit of Windows API, and almost all the environment is related to C++.

 

Microsoft is not entirely unbiased when it comes to C vs. C++. The most obvious example is that the Visual Studio compiler still only supports C89 reasonably cleanly, but is one of the first to support new ISO proposals for C++ standards. Of course, the Windows API can be completely mapped in C. You can also map it completely in COBOL. The code examples from Microsoft are C++ and sometimes C#-focussed because Microsoft wants to sell its C++ and C# compilers. However, Microsoft does not have a good C compiler 😉

Write in C.

Link to comment
Share on other sites

Link to post
Share on other sites

2 hours ago, Dat Guy said:

 

And eats more RAM.

unused ram is wasted ram. i paid for my ram so I will used all of my ram. 

Sudo make me a sandwich 

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, wasab said:

unused ram is wasted ram.

 

Who told you that rubbish? A lazy programmer who didn't feel like optimising his software?

 

2 minutes ago, wasab said:

i paid for my ram so I will used all of my ram. 

 

The main problem with this approach is that in 2024 "all your RAM" can easily be utilised by launching a web browser and a text editor (and nothing else). Let me put it this way: in 1996, somehow more software fitted into less RAM. Don't you see a problem with that?

Write in C.

Link to comment
Share on other sites

Link to post
Share on other sites

9 minutes ago, Dat Guy said:

Who told you that rubbish? A lazy programmer who didn't feel like optimising his software?

Everyone who does not work on embedded and know to not optimize prematurely? Choosing a compiler that eats up more ram is not exactly what you call a contributor to technical debt you know? 

Sudo make me a sandwich 

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, wasab said:

Everyone who does not work on embedded

 

Ah, sorry. I forgot: Once you work on a non-embedded system, wasting as many resources as you can grab is what you absolutely need to do.

(Why?)

Write in C.

Link to comment
Share on other sites

Link to post
Share on other sites

4 minutes ago, Dat Guy said:

Ah, sorry. I forgot: Once you work on a non-embedded system, wasting as many resources as you can grab is what you absolutely need to do.

(Why?)

Because anyone who relies on a compiler instead of coding in raw assembly probably already leaves out a lot of memory and performance optimization on the table? I mean, seriously, why would you care working on a regular consumer devices that are not embedded? 

 

I am pretty sure java virtual machine already gobbles up more ram compare to like say any other compiled program that does the identical thing. Did people choose to write in Java for its ram benefits?

Sudo make me a sandwich 

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, wasab said:

why would you care working on a regular consumer devices that are not embedded?

 

Because almost every day I come across people whose computers are fully utilised with a web browser and two other open applications that are essentially in idle mode. We need more and more computing power for fewer and fewer tasks. How much longer do you think this can go on?

Write in C.

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, Dat Guy said:

 

Because almost every day I come across people whose computers are fully utilised with a web browser and two other open applications that are essentially in idle mode. We need more and more computing power for fewer and fewer tasks. How much longer do you think this can go on?

Cpu and ram have gotten so good nowadays that people can literally keep their old computer and not upgrade for 10 years so you tell me. 

Sudo make me a sandwich 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×