Page 1 of 1
linking error new operator
Posted: Sun Nov 02, 2014 1:10 am
by chinnambhrt
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
Re: linking error new operator
Posted: Sun Nov 02, 2014 2:08 am
by iansjack
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.
Re: linking error new operator
Posted: Sun Nov 02, 2014 4:02 am
by no92
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?
Re: linking error new operator
Posted: Sun Nov 02, 2014 11:52 am
by chinnambhrt
Does that mean...i cannot use namespaces, classes and contructors untill I define this new
am I right
Re: linking error new operator
Posted: Sun Nov 02, 2014 12:35 pm
by iansjack
You should be able to use namespaces, but not classes as they depend upon constructors which depend upon new which depends upon memory allocation.
Re: linking error new operator
Posted: Sun Nov 02, 2014 1:10 pm
by xenos
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
Posted: Sun Nov 02, 2014 3:47 pm
by sortie
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).
Re: linking error new operator
Posted: Sun Nov 02, 2014 4:00 pm
by no92
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
Posted: Sun Nov 02, 2014 4:33 pm
by iansjack
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.
Re: linking error new operator
Posted: Mon Nov 03, 2014 12:22 am
by no92
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.).
Re: linking error new operator
Posted: Mon Nov 03, 2014 1:35 am
by xenos
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.
Re: linking error new operator
Posted: Mon Nov 03, 2014 2:45 am
by no92
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
Posted: Mon Nov 03, 2014 3:28 am
by mallard
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.
Re: linking error new operator
Posted: Mon Nov 03, 2014 10:00 am
by xenos
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++.
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++.