Page 1 of 1

Newbie question: Can I use both C and C++ ar the same time?

Posted: Mon Aug 08, 2016 8:02 am
by DevilGladiator
Hi, I'm pretty new to the os development world and I was wondering if I could mix c and c++ for the developmento of my kernel, so I can use object oriented programming in the filesystem by example.
If it can be done, would you recommend doing it or should I just stick with C programming?

Re: Newbie question: Can I use both C and C++ ar the same ti

Posted: Mon Aug 08, 2016 8:11 am
by gerryg400
Yes, you can mix c and c++. Why not do it all in C++?

Re: Newbie question: Can I use both C and C++ ar the same ti

Posted: Mon Aug 08, 2016 8:14 am
by Octacone
It is possible, I don't see a reason why not.
I would suggest you to stick with the language you know better.
C++ is good because it has classes and different types of variable access levels. In my opinion C is better and more used.

Re: Newbie question: Can I use both C and C++ ar the same ti

Posted: Mon Aug 08, 2016 8:51 am
by Ch4ozz
To fully use C++ features you have to code alot of stuff first (like memory management, paging, etc) so you can use the "new" command for example.
AFAIK C++ needs a runtime for all its features which you have to offer by yourself (compiler shouldnt add it because it is ofc OS dependent)

Re: Newbie question: Can I use both C and C++ ar the same ti

Posted: Mon Aug 08, 2016 10:49 am
by onlyonemac
Ch4ozz wrote:To fully use C++ features you have to code alot of stuff first (like memory management, paging, etc) so you can use the "new" command for example.
AFAIK C++ needs a runtime for all its features which you have to offer by yourself (compiler shouldnt add it because it is ofc OS dependent)
Yes, that is true.

Re: Newbie question: Can I use both C and C++ ar the same ti

Posted: Thu Aug 11, 2016 7:36 am
by DevilGladiator
Another question, if i mix c and c++ code, do i have to use the extern "c" keyword to call c++ code from c?

Re: Newbie question: Can I use both C and C++ ar the same ti

Posted: Thu Aug 11, 2016 1:13 pm
by Roman
Yes, C++ symbol names are mangled and cannot be used in C unless you mark your declarations with 'extern "C"'.

Re: Newbie question: Can I use both C and C++ ar the same ti

Posted: Thu Aug 11, 2016 5:58 pm
by heat
Roman wrote:Yes, C++ symbol names are mangled and cannot be used in C unless you mark your declarations with 'extern "C"'.
That also means that you cannot use classes or namespaces(not really sure of this one) from C code, so you have to supply a C interface for classes.

Re: Newbie question: Can I use both C and C++ ar the same ti

Posted: Thu Aug 11, 2016 7:06 pm
by Roman
Yep, namespaces are done through mangling and therefore cannot be used in C. Classes can be imported through a layer consisting of wrapper global functions and opaque types.

Re: Newbie question: Can I use both C and C++ ar the same ti

Posted: Sat Aug 13, 2016 11:53 am
by DevilGladiator
So I'm trying to use both C and C++ following the meaty skeleton tutorial, but when i use i686-elf-g++ as a linker in replacement of i686-elf-gcc so i can use c++ code i get this error:

Code: Select all

i686-elf-g++ -T arch/i386/linker.ld -o XeonOS.kernel -O2 -g -ffreestanding -fbuiltin -Wall -Wextra -Werror  arch/i386/crti.o /opt/cross/lib/gcc/i686-elf/5.4.0/crtbegin.o arch/i386/boot.o  arch/i386/portio.o  arch/i386/tty.o    kernel/kernel.o   /opt/cross/lib/gcc/i686-elf/5.4.0/crtend.o arch/i386/crtn.o     -nostdlib -lk -lgcc 
/opt/cross/lib/gcc/i686-elf/5.4.0/../../../../i686-elf/bin/ld: cannot find -lk
It seems like a library is missing but i didn't find any other references to lk in the source

Re: Newbie question: Can I use both C and C++ ar the same ti

Posted: Sat Aug 13, 2016 2:07 pm
by heat
DevilGladiator wrote:So I'm trying to use both C and C++ following the meaty skeleton tutorial, but when i use i686-elf-g++ as a linker in replacement of i686-elf-gcc so i can use c++ code i get this error:

Code: Select all

i686-elf-g++ -T arch/i386/linker.ld -o XeonOS.kernel -O2 -g -ffreestanding -fbuiltin -Wall -Wextra -Werror  arch/i386/crti.o /opt/cross/lib/gcc/i686-elf/5.4.0/crtbegin.o arch/i386/boot.o  arch/i386/portio.o  arch/i386/tty.o    kernel/kernel.o   /opt/cross/lib/gcc/i686-elf/5.4.0/crtend.o arch/i386/crtn.o     -nostdlib -lk -lgcc 
/opt/cross/lib/gcc/i686-elf/5.4.0/../../../../i686-elf/bin/ld: cannot find -lk
It seems like a library is missing but i didn't find any other references to lk in the source
You have to set your library paths to the sysroot folder's. "lk" is the C library for the kernel, only with freestanding stuff. It's in sysroot/usr/lib/libk.a. I think you need to do something like "--sysroot ../sysroot/".