moving code from c to C++ gives linker error

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
bartexsz
Posts: 7
Joined: Sat Mar 17, 2012 12:45 pm

moving code from c to C++ gives linker error

Post by bartexsz »

I am learning OS developmebt from JamesM's kernel development tutorial.

Recently i decided to rewite screen handling as object. Unfortunately i can't now link it, linker gives me:

Code: Select all

main.o: In function `main':
main.cpp:(.text+0x7e): undefined reference to `_Unwind_Resume'
main.o:(.eh_frame+0x13): undefined reference to `__gxx_personality_v0'
My makefile:

Code: Select all

SOURCES=boot.o main.o common.o screen.o

CPPFLAGS=-nostdlib -nostdinc -fno-builtin -fno-stack-protector
LDFLAGS=-Tlink.ld
ASFLAGS=-felf

all: $(SOURCES) link 

clean:
	-rm *.o kernel

link:
	ld $(LDFLAGS) -o kernel $(SOURCES)

.asm.o:
	nasm $(ASFLAGS) $<
link.ld

Code: Select all

OUTPUT_FORMAT("binary")
ENTRY(start)
SECTIONS
{
    .text 0x100000 :
    {
    code = .; _code = .; __code = .;
    *(.text)
    . = ALIGN(4096);
    }

    .data :
    {
    data = .; _data = .; __data = .;
    *(.data)
    . = ALIGN(4096);
    }
    
    .bss :
    {
    bss = .; _bss = .; __bss = .;
    *(.bss)
    . = ALIGN(4096);
    }

    end = .; _end = .; __end = .;
}
User avatar
xenos
Member
Member
Posts: 1126
Joined: Thu Aug 11, 2005 11:00 pm
Libera.chat IRC: xenos1984
Location: Tartu, Estonia
Contact:

Re: moving code from c to C++ gives linker error

Post by xenos »

You probably wish to disable exception handling: C++ Bare Bones
Programmers' Hardware Database // GitHub user: xenos1984; OS project: NOS
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: moving code from c to C++ gives linker error

Post by Combuster »

Nor are you using the GCC Cross-Compiler
"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 ]
bartexsz
Posts: 7
Joined: Sat Mar 17, 2012 12:45 pm

Re: moving code from c to C++ gives linker error

Post by bartexsz »

Thanks for help , i had to simply add extern "C" to main function, as in c++ Bare Bones

EDIT: It was enough , but only for main. I started implementing gdt, and these errors are back.
I tried to use gcc-cross compiler, but I don't know how to put $TARGET to makefile to make it works.

EDIT2: Ok, I added -ffreestanding , now it works, however, now i get:

Code: Select all

boot.o main.o common.o screen.o gdt_entry.o descrtables.o gdtptr.o link
make: boot.o: Command not found
It worked earlier. What can be wrong?
invalid
Member
Member
Posts: 60
Joined: Thu Feb 23, 2012 8:39 am

Re: moving code from c to C++ gives linker error

Post by invalid »

You forgot $(CC) in your Makefile and it tries executing "boot.o"?
bartexsz
Posts: 7
Joined: Sat Mar 17, 2012 12:45 pm

Re: moving code from c to C++ gives linker error

Post by bartexsz »

Ok, thx.

One more question, how can I implement c++ class method in external asm file?
bartexsz
Posts: 7
Joined: Sat Mar 17, 2012 12:45 pm

Re: moving code from c to C++ gives linker error

Post by bartexsz »

thx, I used g++ with -S parameter to find mangled name. Is there any way to force compiler to not mangle name?
User avatar
brain
Member
Member
Posts: 234
Joined: Thu Nov 05, 2009 5:04 pm
Location: UK
Contact:

Re: moving code from c to C++ gives linker error

Post by brain »

Yes. extern "C" void funcname() { }
bartexsz
Posts: 7
Joined: Sat Mar 17, 2012 12:45 pm

Re: moving code from c to C++ gives linker error

Post by bartexsz »

It doesn't work for functions which belongs to class
User avatar
bluemoon
Member
Member
Posts: 1761
Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong

Re: moving code from c to C++ gives linker error

Post by bluemoon »

To access class member you either dirty hack the mangling/ABI or write an extern C wrapper function. Either way it is not elegant, and the need to access class member in assembly is usually due to unnecessary optimization or design problem.

We have a thread talk about this recently.
bartexsz
Posts: 7
Joined: Sat Mar 17, 2012 12:45 pm

Re: moving code from c to C++ gives linker error

Post by bartexsz »

Not all can be implemented in C++ :)

Actualy it is GDT implementation, in which now works all, but flushing. Unfortunately flushing hangs all.

If someone could check this, here is my github project: https://github.com/bartexsz/Freeq-OS

descrtables.cpp contains function in which i'm calling flush() and gdt.asm implemetation of this function
User avatar
bluemoon
Member
Member
Posts: 1761
Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong

Re: moving code from c to C++ gives linker error

Post by bluemoon »

As berkus mentioned, you either call assembly function from C++ member, or if speed is critical you do inline assembly.

By hacking into mangling you lock yourself into a specific compiler, and do not surprise if your code breaks on next year's gcc.
User avatar
Owen
Member
Member
Posts: 1700
Joined: Fri Jun 13, 2008 3:21 pm
Location: Cambridge, United Kingdom
Contact:

Re: moving code from c to C++ gives linker error

Post by Owen »

If you're willing to use a GCC extension, you can define the symbol name using the 'asm("SymbolName")' directive
bartexsz
Posts: 7
Joined: Sat Mar 17, 2012 12:45 pm

Re: moving code from c to C++ gives linker error

Post by bartexsz »

I forgot to say, I solved compiling/linkage error. My only problem now is flushing which hangs all, despite fact that it's called nearly exactly like in JamesM's kernel development tutorial, what shows my code on github. (Link in my previous post).
Post Reply