linking error new operator

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
chinnambhrt
Posts: 7
Joined: Sun Nov 02, 2014 12:58 am

linking error new operator

Post 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
User avatar
iansjack
Member
Member
Posts: 4707
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: linking error new operator

Post 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.
no92
Member
Member
Posts: 307
Joined: Wed Oct 30, 2013 1:57 pm
Libera.chat IRC: no92
Location: Germany
Contact:

Re: linking error new operator

Post 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?
chinnambhrt
Posts: 7
Joined: Sun Nov 02, 2014 12:58 am

Re: linking error new operator

Post by chinnambhrt »

Does that mean...i cannot use namespaces, classes and contructors untill I define this new
am I right
User avatar
iansjack
Member
Member
Posts: 4707
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: linking error new operator

Post 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.
User avatar
xenos
Member
Member
Posts: 1121
Joined: Thu Aug 11, 2005 11:00 pm
Libera.chat IRC: xenos1984
Location: Tartu, Estonia
Contact:

Re: linking error new operator

Post 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.
Programmers' Hardware Database // GitHub user: xenos1984; OS project: NOS
User avatar
sortie
Member
Member
Posts: 931
Joined: Wed Mar 21, 2012 3:01 pm
Libera.chat IRC: sortie

Re: linking error new operator

Post 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).
no92
Member
Member
Posts: 307
Joined: Wed Oct 30, 2013 1:57 pm
Libera.chat IRC: no92
Location: Germany
Contact:

Re: linking error new operator

Post by no92 »

This thread basically shows us what a mess C++ tends to be when it comes to using it for OSdeving :D
User avatar
iansjack
Member
Member
Posts: 4707
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: linking error new operator

Post 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.
no92
Member
Member
Posts: 307
Joined: Wed Oct 30, 2013 1:57 pm
Libera.chat IRC: no92
Location: Germany
Contact:

Re: linking error new operator

Post 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.).
User avatar
xenos
Member
Member
Posts: 1121
Joined: Thu Aug 11, 2005 11:00 pm
Libera.chat IRC: xenos1984
Location: Tartu, Estonia
Contact:

Re: linking error new operator

Post 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.
Programmers' Hardware Database // GitHub user: xenos1984; OS project: NOS
no92
Member
Member
Posts: 307
Joined: Wed Oct 30, 2013 1:57 pm
Libera.chat IRC: no92
Location: Germany
Contact:

Re: linking error new operator

Post 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++.
mallard
Member
Member
Posts: 280
Joined: Tue May 13, 2014 3:02 am
Location: Private, UK

Re: linking error new operator

Post 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.
Image
User avatar
xenos
Member
Member
Posts: 1121
Joined: Thu Aug 11, 2005 11:00 pm
Libera.chat IRC: xenos1984
Location: Tartu, Estonia
Contact:

Re: linking error new operator

Post 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++.
Programmers' Hardware Database // GitHub user: xenos1984; OS project: NOS
Post Reply