how to use C++ with classes

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.
User avatar
xDDunce
Member
Member
Posts: 173
Joined: Tue Aug 12, 2008 4:04 pm
Contact:

how to use C++ with classes

Post by xDDunce »

since i cannot recover my old source code, i have decided to start fresh. I want to make an easier programming environment to use but in all my attempts, i still have figured it out!

I'm using DJGPP on Windows XP SP3 on an intel based macbook (if that changes anything??).

whenever i define a class(e.g. in my screen driver) , gcc gives an error saying:

Code: Select all

error: expected '=' , ',  ' , '; ' , 'asm' or '__attribute__' before 'screen'  
is there a switch that must be used with gcc to be able to use classes? or any pre-requisites to enable them?

thanks in advance,

James.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: how to use C++ with classes

Post by Combuster »

In the category required knowledge:

USE A C++ COMPILER AND NOT A C COMPILER.

I seriously doubt you should be developing an OS when you even failed to reach "hello world" level :shock:
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
sebihepp
Member
Member
Posts: 195
Joined: Tue Aug 26, 2008 11:24 am
GitHub: https://github.com/sebihepp

Re: how to use C++ with classes

Post by sebihepp »

hello johnsy2008,

gcc is only a c compiler and c doesn't know classes. You have to use g++ instead.

greetings sebihepp
User avatar
xDDunce
Member
Member
Posts: 173
Joined: Tue Aug 12, 2008 4:04 pm
Contact:

Re: how to use C++ with classes

Post by xDDunce »

thanks sebihepp! ill change it now.

and in reply to combuster:
how would you know how far i have got with my last OS? just because i didnt realise gcc was only a c compiler, does not mean you should start flaming me! my last os got to paging but then i couldnt recover the files i lost so i had to restart so i think that is just a little bit further than just a "hello world!" OS! and besides that, people come here for help! not to be attacked!


but anyway....
which switches and such are needed to compile with g++? i expect them to be different from gcc.

James.
User avatar
Brynet-Inc
Member
Member
Posts: 2426
Joined: Tue Oct 17, 2006 9:29 pm
Libera.chat IRC: brynet
Location: Canada
Contact:

Re: how to use C++ with classes

Post by Brynet-Inc »

johnsy2008 wrote:people come here for help! not to be attacked!
Then people need to change their expectations. :roll:

Nobody here wants teach you how to use a compiler, or how to program in a specific language, you must meet certain requirements before the regulars here are going to be willing to spend time on your specific case.
Image
Twitter: @canadianbryan. Award by smcerm, I stole it. Original was larger.
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Re: how to use C++ with classes

Post by AJ »

Hi,

The reason Combuster replied in that way is that you should really know you toolchain quite well before attempting to program an OS. I though I knew my toolchain quite well until I started OS dev, but OS devving seems to stretch that knowlege to the limit (as you will see from some of my (recent) posts on General Programming).

If you do not know the basics enough to create a "Hello World" C++ program (gcc != g++), OS dev is going to be a lot trickier, so try playing around with application development first.

As for the switches, I use the following in my Makefile for my second stage boot loader:

Code: Select all

CXXFLAGS:=	-b$(TARGET) -V$(CXXVER) -MD \
			-Wall -Wextra -pedantic -Wshadow -Wpointer-arith -Wcast-align \
			-Wwrite-strings -Wredundant-decls -Winline -Wno-long-long \
			-Werror -O0 -fstrength-reduce -nostartfiles \
			-DDEBUG -fno-exceptions -fno-rtti \
			-I../include/ -I../../OSLib/include/
You can pretty much see Solar's Makefile tutorial on the wiki and adapt the flags that don't work with C++. Really, though, you need to read up on G++ and see which switches suit your needs best. Do not copy and paste my switches above because they may not be what you require.

Cheers,
Adam
sebihepp
Member
Member
Posts: 195
Joined: Tue Aug 26, 2008 11:24 am
GitHub: https://github.com/sebihepp

Re: how to use C++ with classes

Post by sebihepp »

holy sh..
Now I have a problem with the same subject. :shock: :lol: :lol:

I am using only one class. I compile with g++ -> everything is okay.
But when I link with ld I am getting the following error:
<<undefined reference to "__gxx_personality_v0">>

Where does this reference come from? Okay it can only come from g++.
But why? And what can I do to solve it? If I link with g++ he can't find
a Library called CRT0.o
I am already using -ffreestanding.

This is my Makefile

Code: Select all

bin/GCOS.img: bin/GCOS.bin bin/BootLoader.bin
	cat bin/BootLoader.bin bin/GCOS.bin > bin/GCOS.img
	cp bin/GCOS.img /cygdrive/d/programmieren/engines/bochs/GCOS.img

bin/GCOS.bin: obj/Kernel.obj obj/driver/video/video.obj
	i586-elf-ld -T link.txt -o bin/GCOS.bin obj/Kernel.obj obj/driver/video/video.obj

obj/Kernel.obj: Kernel/Kernel.cpp
	i586-elf-g++ -ffreestanding -c -o obj/Kernel.obj Kernel/Kernel.cpp

obj/driver/video/video.obj: Kernel/driver/video/video.cpp
	i586-elf-g++ -ffreestanding -c -o obj/driver/video/Video.obj Kernel/driver/video/video.cpp

bin/BootLoader.bin: BootLoader/BootLoader.asm
	nasm.exe -f bin -o bin/BootLoader.bin BootLoader/BootLoader.asm
Sebihepp
User avatar
xDDunce
Member
Member
Posts: 173
Joined: Tue Aug 12, 2008 4:04 pm
Contact:

Re: how to use C++ with classes

Post by xDDunce »

ok. i have been programming C++ for a while but had been using dev-cpp so i have not used command line compiling much. but now that i know to use gcc, we can all carry on as normal!

and thanks for the make file example. i dont generally use make files (i didn't on my last os but i guess new things wouldn't hurt) but i could change my ways. is it easier to use a make file? at the moment im just using chained batch programs.

thanks,

James.
sebihepp
Member
Member
Posts: 195
Joined: Tue Aug 26, 2008 11:24 am
GitHub: https://github.com/sebihepp

Re: how to use C++ with classes

Post by sebihepp »

@johnsy2008:

Makefiles can make a lot of work for you. If you set up everthing correctly
you only have to type "make" and the make-tool does everything you specified
in the makefile.

You can specify, that for all *.cpp Files, a *.o File with "gcc -c -o {Target} {File}"
should be generated. Only the syntax of a makefile is weird. ^^
For the current target you must write "$@" and for a File "$<".

Sebihepp
User avatar
xDDunce
Member
Member
Posts: 173
Joined: Tue Aug 12, 2008 4:04 pm
Contact:

Re: how to use C++ with classes

Post by xDDunce »

you see, now that is why i did not know to use g++!
You can specify, that for all *.cpp Files, a *.o File with "gcc -c -o {Target} {File}"
should you not use g++? not gcc?

and you were all saying you need to meet pre-requisites. well what about continuity? talk about double standards!

ok. so if i want to compile using g++, what switches would i use?
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: how to use C++ with classes

Post by Combuster »

I tend to be rather annoyed when people break forum rules (and you broke two of them).

But seriously, you should not start OS development in C++ given your apparent knowledge of that language.

As for the flaming, I didn't put down any direct insults, I did tell you what the problem was, and I did give you a piece of advice. Being very unfriendly is not the same as an insult, and I regret that you had to interpret the hello world remark in relation to your kernel (which wasn't mentioned) rather than your skill points in said language.

@sebihepp:
I am already using -ffreestanding.
C_PlusPlus
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
sebihepp
Member
Member
Posts: 195
Joined: Tue Aug 26, 2008 11:24 am
GitHub: https://github.com/sebihepp

Re: how to use C++ with classes

Post by sebihepp »

thanks Combuster. This helps me.
albeva
Member
Member
Posts: 42
Joined: Thu Aug 21, 2008 8:31 pm

Re: how to use C++ with classes

Post by albeva »

sebihepp wrote:holy sh..
Now I have a problem with the same subject. :shock: :lol: :lol:

I am using only one class. I compile with g++ -> everything is okay.
But when I link with ld I am getting the following error:
<<undefined reference to "__gxx_personality_v0">>
Add "-fno-exceptions" to your g++ flags.

johnsy2008 - Take time and learn the tools. Google gor gcc tutorials. Linker and makefiles. It will only come in handy.

Here are few useful links:

General help
http://developer.apple.com/documentatio ... c-3.3/gcc/

Command line options.
https://www.wideopen.com/docs/manuals/e ... g-gcc.html
sebihepp
Member
Member
Posts: 195
Joined: Tue Aug 26, 2008 11:24 am
GitHub: https://github.com/sebihepp

Re: how to use C++ with classes

Post by sebihepp »

Thanks albeva. I solved it.

But now my Kernel works, but I always getting a Virtual Function Fail.
I implemented <<extern "C" void __cxa_pure_virtual()>> wich only print the
error message.

But now I had no classes and the only function, besides main, is to print text.
But this function can be called. This indicated the output of the Failure.
I thought virtual functions only exists in classes and only if they are specified
as "virtual"? What went wrong?

Sebihepp
albeva
Member
Member
Posts: 42
Joined: Thu Aug 21, 2008 8:31 pm

Re: how to use C++ with classes

Post by albeva »

Post Reply