linking error new operator
-
- Posts: 7
- Joined: Sun Nov 02, 2014 12:58 am
linking error new operator
Hi, Guys..
I wrote my kernel in c++ by defining KERNEL class..compiles and creates objects files sucessfully..but while linking..it says..
undefined reference to operator new( unsigned long )
with following command
i686-elf-g++ -T linker.ld -o demoOs.bin -ffreestanding -nostdlib -O2 boot.o kernel.o
help is greatly appreciated
Thanks in advance
I wrote my kernel in c++ by defining KERNEL class..compiles and creates objects files sucessfully..but while linking..it says..
undefined reference to operator new( unsigned long )
with following command
i686-elf-g++ -T linker.ld -o demoOs.bin -ffreestanding -nostdlib -O2 boot.o kernel.o
help is greatly appreciated
Thanks in advance
Re: linking error new operator
If you want to use C++ features such as memory allocation in your own kernel you will need to write the appropriate support routines. It's probably best to stick with plain C until you are more experienced. And remember that you won't be able to use C library routines (e.g. printf, file functions, string functions, etc.) until you write your own versions.
-
- Member
- Posts: 307
- Joined: Wed Oct 30, 2013 1:57 pm
- Libera.chat IRC: no92
- Location: Germany
- Contact:
Re: linking error new operator
Your problem is that you don't exactly understand what new[] is - it's basically a malloc which ensures that the constructor is called. If you decide to write your own OS, you have esentially nothing to work with, you have to do everything by yourself, including language constructs like new[] and delete[].
Did you read Required Knowledge?
Did you read Required Knowledge?
-
- Posts: 7
- Joined: Sun Nov 02, 2014 12:58 am
Re: linking error new operator
Does that mean...i cannot use namespaces, classes and contructors untill I define this new
am I right
am I right
Re: linking error new operator
You should be able to use namespaces, but not classes as they depend upon constructors which depend upon new which depends upon memory allocation.
- xenos
- Member
- Posts: 1121
- Joined: Thu Aug 11, 2005 11:00 pm
- Libera.chat IRC: xenos1984
- Location: Tartu, Estonia
- Contact:
Re: linking error new operator
Of course you can use classes, but you can't create class objects dynamically, as this would require dynamical memory allocation and the new operator.
Re: linking error new operator
More precisely, namespaces, classes and constructors work out of the box. You need to implement operator new to use it, but instances of classes can be made in other ways, such as on the stack, or as global variables (requires supporr in your init code).
-
- Member
- Posts: 307
- Joined: Wed Oct 30, 2013 1:57 pm
- Libera.chat IRC: no92
- Location: Germany
- Contact:
Re: linking error new operator
This thread basically shows us what a mess C++ tends to be when it comes to using it for OSdeving
Re: linking error new operator
Apologies - wooly thinking on my part.
The truth is that, whatever language you use, you are going to have to write your own memory allocation/deallocation routines. I find it difficult to conceive of a useful operating system that doesn't make use of dynamically allocated data. In the case of C++ this would mean dynamically allocated object instances.
The truth is that, whatever language you use, you are going to have to write your own memory allocation/deallocation routines. I find it difficult to conceive of a useful operating system that doesn't make use of dynamically allocated data. In the case of C++ this would mean dynamically allocated object instances.
-
- Member
- Posts: 307
- Joined: Wed Oct 30, 2013 1:57 pm
- Libera.chat IRC: no92
- Location: Germany
- Contact:
Re: linking error new operator
I was not talking about the memory allocation methods - only about the (relatively) big amount of effort you have to spend to get all the C++ functionality (e.g. STL library, libsupc++ etc.).
- xenos
- Member
- Posts: 1121
- Joined: Thu Aug 11, 2005 11:00 pm
- Libera.chat IRC: xenos1984
- Location: Tartu, Estonia
- Contact:
Re: linking error new operator
If you want to have all the C++ functionality including the STL, that you are right that quite a number of runtime support is necessary. But for writing a kernel in C++ you don't necessary need all of them. In my kernel I needed to implement only the sections on pure virtual functions and operators new and delete (including placement versions) from the C++ wiki article.
-
- Member
- Posts: 307
- Joined: Wed Oct 30, 2013 1:57 pm
- Libera.chat IRC: no92
- Location: Germany
- Contact:
Re: linking error new operator
Well, using C++ without the runtime support is kinda pointless. If you don't include them, it's basically C with classes. C++ without exceptions and Templates wouldn't be C++.
Re: linking error new operator
My kernel is largely written in C++, including the use of templates and (after the memory manager has initialised), the new/delete operators. I even have a few STL-alike templates (vector, map, string*, list). Using C++ without the standard library is very much still C++, just like C without its standard library is still C.
I haven't bothered with support for global constructors/destructors, since they would be unlikely to work anyway (since they'd run before the memory manager) and there's little point in free-ing memory when the system is about to power down anyway.
* My "string" implementation isn't actually a template, unlike the STL version.
I haven't bothered with support for global constructors/destructors, since they would be unlikely to work anyway (since they'd run before the memory manager) and there's little point in free-ing memory when the system is about to power down anyway.
* My "string" implementation isn't actually a template, unlike the STL version.
- xenos
- Member
- Posts: 1121
- Joined: Thu Aug 11, 2005 11:00 pm
- Libera.chat IRC: xenos1984
- Location: Tartu, Estonia
- Contact:
Re: linking error new operator
Using templates doesn't require runtime support, I use them in my kernel as well. Also, I use classes, inheritance and virtual functions. The main advantage here is a clear structure of the code which is easier to achieve if it can be realized by a similarly structured language such as C++.no92 wrote:Well, using C++ without the runtime support is kinda pointless. If you don't include them, it's basically C with classes. C++ without exceptions and Templates wouldn't be C++.