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

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
DevilGladiator
Posts: 11
Joined: Mon Aug 08, 2016 7:56 am

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

Post 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?
gerryg400
Member
Member
Posts: 1801
Joined: Thu Mar 25, 2010 11:26 pm
Location: Melbourne, Australia

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

Post by gerryg400 »

Yes, you can mix c and c++. Why not do it all in C++?
If a trainstation is where trains stop, what is a workstation ?
User avatar
Octacone
Member
Member
Posts: 1138
Joined: Fri Aug 07, 2015 6:13 am

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

Post 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.
OS: Basic OS
About: 32 Bit Monolithic Kernel Written in C++ and Assembly, Custom FAT 32 Bootloader
User avatar
Ch4ozz
Member
Member
Posts: 170
Joined: Mon Jul 18, 2016 2:46 pm
Libera.chat IRC: esi

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

Post 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)
onlyonemac
Member
Member
Posts: 1146
Joined: Sat Mar 01, 2014 2:59 pm

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

Post 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.
When you start writing an OS you do the minimum possible to get the x86 processor in a usable state, then you try to get as far away from it as possible.

Syntax checkup:
Wrong: OS's, IRQ's, zero'ing
Right: OSes, IRQs, zeroing
DevilGladiator
Posts: 11
Joined: Mon Aug 08, 2016 7:56 am

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

Post 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?
User avatar
Roman
Member
Member
Posts: 568
Joined: Thu Mar 27, 2014 3:57 am
Location: Moscow, Russia
Contact:

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

Post by Roman »

Yes, C++ symbol names are mangled and cannot be used in C unless you mark your declarations with 'extern "C"'.
"If you don't fail at least 90 percent of the time, you're not aiming high enough."
- Alan Kay
heat
Member
Member
Posts: 103
Joined: Sat Mar 28, 2015 11:23 am
Libera.chat IRC: heat

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

Post 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.
If some of you people keep insisting on having backwards compatibitity with the stone age, we'll have stone tools forever.
My Hobby OS: https://github.com/heatd/Onyx
User avatar
Roman
Member
Member
Posts: 568
Joined: Thu Mar 27, 2014 3:57 am
Location: Moscow, Russia
Contact:

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

Post 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.
"If you don't fail at least 90 percent of the time, you're not aiming high enough."
- Alan Kay
DevilGladiator
Posts: 11
Joined: Mon Aug 08, 2016 7:56 am

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

Post 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
heat
Member
Member
Posts: 103
Joined: Sat Mar 28, 2015 11:23 am
Libera.chat IRC: heat

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

Post 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/".
If some of you people keep insisting on having backwards compatibitity with the stone age, we'll have stone tools forever.
My Hobby OS: https://github.com/heatd/Onyx
Post Reply