Jump to content

Is C++ competely separated from it's standard library?

Trinopoty

C (not C++) as a language is pure and is completely separated from the standard library that comes with it. You can write a C program and it will compile and run fine without any standard library in place.

Can the same be said for C++? I know the the STL is almost integrated into the C++ language itself by means of the new and delete keywords at least.

Is it possible to disable the STL completely and still be able to compile C++ code?

Link to comment
Share on other sites

Link to post
Share on other sites

9 minutes ago, Trinopoty said:

Can the same be said for C++?

Yes, so long as you don't use anything in the STL...

Don't ask to ask, just ask... please聽馃え

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, Sauron said:

Yes, so long as you don't use anything in the STL...

So as long as you don't use the STL, you can remove it and the compiler won't complain?

Is there a bare minimum amount of runtime support that would be required for C++ even without the STL? Apart from the stack.

Link to comment
Share on other sites

Link to post
Share on other sites

10 minutes ago, Trinopoty said:

So as long as you don't use the STL, you can remove it and the compiler won't complain?

Is there a bare minimum amount of runtime support that would be required for C++ even without the STL? Apart from the stack.

No, that's only a thing with shared libraries afaik.

Don't ask to ask, just ask... please聽馃え

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

9 hours ago, Trinopoty said:

So as long as you don't use the STL, you can remove it and the compiler won't complain?

You have to declare that you're using "stuff" from the STL for the "compiler" to even "bring it in".

That's what聽#include聽statements are.

For example:

int main()
{
  std::cout << "Hello World!" << std::endl;
}

Will give you an error about not knowing what "std::cout" is, but:

#include <iostream>

int main()
{
  std::cout << "Hello World" << std::endl;
}

will compile.

ENCRYPTION IS NOT A CRIME

Link to comment
Share on other sites

Link to post
Share on other sites

10 hours ago, Trinopoty said:

C (not C++) as a language is pure and is completely separated from the standard library that comes with it. You can write a C program and it will compile and run fine without any standard library in place.

Can the same be said for C++? I know the the STL is almost integrated into the C++ language itself by means of the new and delete keywords at least.

Is it possible to disable the STL completely and still be able to compile C++ code?

The STL is header only, perhaps you mean the runtime libraries?

Link to comment
Share on other sites

Link to post
Share on other sites

3 hours ago, Unimportant said:

The STL is header only, perhaps you mean the runtime libraries?

Whatever it is that provides stuff like new, delete, etc.

Link to comment
Share on other sites

Link to post
Share on other sites

9 minutes ago, Trinopoty said:

Whatever it is that provides stuff like new, delete, etc.

new, delete, ->, and things such as that are part of the C++ definition.

Just like how in c vs ansi c, // is valid for c, but // isn't valid for ansi c (different standards).

3735928559 - Beware of the dead beef

Link to comment
Share on other sites

Link to post
Share on other sites

8 hours ago, wanderingfool2 said:

new, delete, ->, and things such as that are part of the C++ definition.

Just like how in c vs ansi c, // is valid for c, but // isn't valid for ansi c (different standards).

But doesn't new and delete require runtime support since they operate on a heap which is an external thing.

Link to comment
Share on other sites

Link to post
Share on other sites

4 hours ago, Trinopoty said:

But doesn't new and delete require runtime support since they operate on a heap which is an external thing.

Yes, they are provided by the runtime library (often delegated to malloc under the hood).聽 Most platforms combine the runtime and standard library into one. If you compile without it you lose the bootstrapping and proper exit code along with the standard library functions.

The STL is a template library and because the compiler needs to be able to see the full implementation of templates at the point they're invoked, the headers ARE the full implementation, there should be no separate binaries for the STL.

Link to comment
Share on other sites

Link to post
Share on other sites

2 hours ago, Unimportant said:

Yes, they are provided by the runtime library (often delegated to malloc under the hood).聽 Most platforms combine the runtime and standard library into one. If you compile without it you lose the bootstrapping and proper exit code along with the standard library functions.

The STL is a template library and because the compiler needs to be able to see the full implementation of templates at the point they're invoked, the headers ARE the full implementation, there should be no separate binaries for the STL.

So it's not possible to completely disable the runtime library?

Link to comment
Share on other sites

Link to post
Share on other sites

4 hours ago, Trinopoty said:

So it's not possible to completely disable the runtime library?

So, is there a specific reason for asking these questions.聽 I am genuinely curious, as even embedded systems can run standard c++ stuff.

3735928559 - Beware of the dead beef

Link to comment
Share on other sites

Link to post
Share on other sites

20 hours ago, Trinopoty said:

Whatever it is that provides stuff like new, delete, etc.

11 hours ago, Trinopoty said:

But doesn't new and delete require runtime support since they operate on a heap which is an external thing.

Do you really just want to program in C?

If you disable the runtime library you're going to lose support for most features that differentiate C++ from C anyway.

C also requires a runtime library for some features to work correctly, so I guess I really should be asking:聽Do you really just want to program in assembly?

ENCRYPTION IS NOT A CRIME

Link to comment
Share on other sites

Link to post
Share on other sites

You can setup your compiler to adhere to certain C++ standards, which won't require any runtime libraries. But as some have pointed out, you will get much closer to pure C, but use classes and certain functions that are C++ specific.

Link to comment
Share on other sites

Link to post
Share on other sites

2 hours ago, wanderingfool2 said:

So, is there a specific reason for asking these questions.聽 I am genuinely curious, as even embedded systems can run standard c++ stuff.

Just curious as to how C++ in the embedded world works with where a runtime does not exist.

Link to comment
Share on other sites

Link to post
Share on other sites

24 minutes ago, Trinopoty said:

Just curious as to how C++ in the embedded world works with where a runtime does not exist.

I haven't really done anything in the world of embedded systems, so I could be wrong and maybe someone can correct me...but from my knowledge, STL exists on most embedded system compilers

3735928559 - Beware of the dead beef

Link to comment
Share on other sites

Link to post
Share on other sites

  • 2 weeks later...
On 5/6/2020 at 3:21 AM, Trinopoty said:

C (not C++) as a language is pure and is completely separated from the standard library that comes with it. You can write a C program and it will compile and run fine without any standard library in place.

Can the same be said for C++? I know the the STL is almost integrated into the C++ language itself by means of the new and delete keywords at least.

Is it possible to disable the STL completely and still be able to compile C++ code?

Yes, simply pass聽-nostdlib -fno-exceptions -fno-rtti聽to your compiler and enjoy.

As a side note, I believe you're mistaking what the C stdlib is, since you'd go through the same pain with both C and C++ if you're passing the above mentioned flags (you can read more about it here聽for C and here for C++).

Most embedded devices have a C stdlib (take newlib, as an example), which is fundamental for basic tasks such as calling malloc and whatnot, and since the C++ std library only makes calls to to the functions on the C stdlib, it's not hard to have support for it.

On 5/7/2020 at 1:33 PM, straight_stewie said:

Do you really just want to program in C?

If you disable the runtime library you're going to lose support for most features that differentiate C++ from C anyway.

You still get classes and templates聽馃槤

When you're really tight on program memory, including a fat and somewhat generic stdlib may not be useful, so you might want to roll your own.

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

Someone please correct me if I'm wrong or missing something.

For many operations, no you don't need any standard libraries, but once you start dealing with entities outside of your program (i.e. files, display output, get keyboard/mouse data), that's there things start getting tricky.

As I recall there is a difference between programming for Unix/Unix like systems and programming for Windows.聽 With Unix, you can use interrupts to communicate with the OS, open files, display text, get keyboard data, etc.聽 But Windows doesn't allow you to do that.聽 Instead you're program links to the CRT Library, which has the actual code in DLL's.聽

Link to comment
Share on other sites

Link to post
Share on other sites

25 minutes ago, JacobFW said:

As I recall there is a difference between programming for Unix/Unix like systems and programming for Windows.聽 With Unix, you can use interrupts to communicate with the OS, open files, display text, get keyboard data, etc.聽 But Windows doesn't allow you to do that.聽 Instead you're program links to the CRT Library, which has the actual code in DLL's.聽

My understanding is nowhere near expert level, but that's not quite correct.

The CRT is just the聽C RunTime Library.

You can use interrupts and even register your own ISRs if you're writing something like a driver.

Otherwise for Win32 apps you get a collection of classes, functions, and services, and windows sends you messages through the聽WinProc聽callback function.

I've experimented with it a bit, but for nothing major or useful.

ENCRYPTION IS NOT A CRIME

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, JacobFW said:

For many operations, no you don't need any standard libraries, but once you start dealing with entities outside of your program

You still need something to set up memory and whatnot for you, the stdlib takes care of that.

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

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